Example usage for com.lowagie.text Image getInstance

List of usage examples for com.lowagie.text Image getInstance

Introduction

In this page you can find the example usage for com.lowagie.text Image getInstance.

Prototype

public static Image getInstance(Image image) 

Source Link

Document

gets an instance of an Image

Usage

From source file:questions.images.OverlappingImages.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.POSTCARD);
    try {//from   w w  w  .j  a v a2s.  com
        // step 2:
        // we create a writer
        PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document
        Image img1 = Image.getInstance(RESOURCE1);
        img1.scaleToFit(PageSize.POSTCARD.getWidth(), 10000);
        img1.setAbsolutePosition(0, 0);
        document.add(img1);
        Image img2 = Image.getInstance(RESOURCE2);
        img2.setTransparency(new int[] { 0x00, 0x10 });
        img2.setAbsolutePosition(PageSize.POSTCARD.getWidth() - img2.getScaledWidth(),
                PageSize.POSTCARD.getHeight() - img2.getScaledHeight());
        document.add(img2);
        Image img3 = Image.getInstance(RESOURCE3);
        img3.setTransparency(new int[] { 0xF0, 0xFF });
        img3.setAbsolutePosition((PageSize.POSTCARD.getWidth() - img3.getScaledWidth()) / 2,
                (PageSize.POSTCARD.getHeight() - img3.getScaledHeight()) / 2);
        document.add(img3);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();

}

From source file:questions.images.PostCard.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.POSTCARD);
    try {/* w  w  w.  ja v a 2s  .c om*/
        // step 2:
        // we create a writer
        PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), 10000);
        img.setAbsolutePosition(0, 0);
        document.add(img);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
}

From source file:questions.images.PostCardExtra.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.POSTCARD);
    try {/* www . jav a 2s .c o m*/
        // step 2:
        // we create a writer
        PdfWriter writer = PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), 10000);
        img.setAbsolutePosition(0, 0);
        PdfImage stream = new PdfImage(img, "", null);
        stream.put(new PdfName("MySpecialId"), new PdfName("123456789"));
        PdfIndirectObject ref = writer.addToBody(stream);
        img.setDirectReference(ref.getIndirectReference());
        document.add(img);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
}

From source file:questions.images.ResizeImage.java

public static void createPdf(String filename) throws IOException, DocumentException {
    Document document = new Document(PageSize.A2);
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    document.open();/*from  w ww  .  j a  va2  s .c  om*/
    Image img = Image.getInstance(RESOURCE);
    document.add(img);
    document.close();
}

From source file:questions.images.TransparentEllipse1.java

public static void main(String[] args) {
    Document document = new Document(PageSize.POSTCARD);
    try {//from  w w  w.j av  a2 s.c  o  m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        Image img = Image.getInstance(RESOURCE);
        byte[] bytes = new byte[2500];
        int byteCount = 0;
        int bitCount = 6;
        for (int x = -50; x < 50; x++) {
            for (int y = -50; y < 50; y++) {
                double rSquare = Math.pow(x, 2) + Math.pow(y, 2);
                if (rSquare <= 1600) { // 40
                    bytes[byteCount] += (3 << bitCount);
                } else if (rSquare <= 2025) { // 45
                    bytes[byteCount] += (2 << bitCount);
                } else if (rSquare <= 2500) { // 50
                    bytes[byteCount] += (1 << bitCount);
                }
                bitCount -= 2;
                if (bitCount < 0) {
                    bitCount = 6;
                    byteCount++;
                }
            }
        }
        Image smask = Image.getInstance(100, 100, 1, 2, bytes);
        smask.makeMask();
        img.setImageMask(smask);
        img.setAbsolutePosition(0, 0);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
        writer.getDirectContent().addImage(img);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.images.TransparentEllipse2.java

public static void main(String[] args) {

    Document document = new Document(PageSize.POSTCARD);
    try {/*from   w  w  w .  j  a  v a  2  s . c o m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        // clipped image
        cb.ellipse(1, 1, PageSize.POSTCARD.getWidth() - 2, PageSize.POSTCARD.getHeight() - 2);
        cb.clip();
        cb.newPath();
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
        cb.addImage(img, PageSize.POSTCARD.getWidth(), 0, 0, PageSize.POSTCARD.getHeight(), 0, 0);

        //Prepare gradation list
        int gradationStep = 40;
        float[] gradationRatioList = new float[gradationStep];
        for (int i = 0; i < gradationStep; i++) {
            gradationRatioList[i] = 1 - (float) Math.sin(Math.toRadians(90.0f / gradationStep * (i + 1)));
        }

        //Create template
        PdfTemplate template = cb.createTemplate(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());

        //Prepare transparent group
        PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
        transGroup.put(PdfName.CS, PdfName.DEVICEGRAY);
        transGroup.setIsolated(true);
        transGroup.setKnockout(false);
        template.setGroup(transGroup);

        //Prepare graphic state
        PdfGState gState = new PdfGState();
        PdfDictionary maskDict = new PdfDictionary();
        maskDict.put(PdfName.TYPE, PdfName.MASK);
        maskDict.put(PdfName.S, new PdfName("Luminosity"));
        maskDict.put(new PdfName("G"), template.getIndirectReference());
        gState.put(PdfName.SMASK, maskDict);
        cb.setGState(gState);

        //Create gradation for mask
        for (int i = 1; i < gradationStep + 1; i++) {
            template.setLineWidth(gradationStep + 1 - i);
            template.setGrayStroke(gradationRatioList[gradationStep - i]);
            template.ellipse(0, 0, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
            template.stroke();
        }

        //Place template
        cb.addTemplate(template, 0, 0);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    document.close();
}

From source file:questions.images.TransparentPng.java

public static final void main(String[] args) throws IOException, DocumentException {
    Image img = Image.getInstance(IMAGE);
    Rectangle rectangle = new Rectangle(img);
    rectangle.setBackgroundColor(Color.YELLOW);
    Document document = new Document(rectangle);
    PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    document.open();//from ww w .j  ava2 s  . c o  m
    img.setAbsolutePosition(0, 0);
    document.add(img);
    document.close();
}

From source file:questions.importpages.HelloWorldImportedPages.java

public static void main(String[] args) {
    // we create a PDF file
    createPdf(SOURCE);//from  w  w w . ja v  a  2  s  .  c  o m
    // step 1
    Document document = new Document(PageSize.A4);
    try {
        // we create a PdfReader object
        PdfReader reader = new PdfReader(SOURCE);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        PdfImportedPage page;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            page = writer.getImportedPage(reader, i);
            Image image = Image.getInstance(page);
            image.scalePercent(15f);
            image.setBorder(Rectangle.BOX);
            image.setBorderWidth(3f);
            image.setBorderColor(new GrayColor(0.5f));
            image.setRotationDegrees(-reader.getPageRotation(i));
            document.add(image);
            document.add(new Paragraph("This is page: " + i));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    // step 5
    document.close();
}

From source file:questions.importpages.NameCard.java

public static void createOneCard() throws DocumentException, IOException {
    Rectangle rect = new Rectangle(Utilities.millimetersToPoints(86.5f), Utilities.millimetersToPoints(55));
    Document document = new Document(rect);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(CARD));
    writer.setViewerPreferences(PdfWriter.PrintScalingNone);
    document.open();/*from w ww.  j a  v a2s .c om*/
    PdfReader reader = new PdfReader(LOGO);
    Image img = Image.getInstance(writer.getImportedPage(reader, 1));
    img.scaleToFit(rect.getWidth() / 1.5f, rect.getHeight() / 1.5f);
    img.setAbsolutePosition((rect.getWidth() - img.getScaledWidth()) / 2,
            (rect.getHeight() - img.getScaledHeight()) / 2);
    document.add(img);
    document.newPage();
    BaseFont bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, 12);
    font.setColor(new CMYKColor(1, 0.5f, 0, 0.467f));
    ColumnText column = new ColumnText(writer.getDirectContent());
    Paragraph p;
    p = new Paragraph("Bruno Lowagie\n1T3XT\nbruno@1t3xt.com", font);
    p.setAlignment(Element.ALIGN_CENTER);
    column.addElement(p);
    column.setSimpleColumn(0, 0, rect.getWidth(), rect.getHeight() * 0.75f);
    column.go();
    document.close();
}

From source file:questions.ocg.AddOptionalWatermark.java

public static void main(String[] args) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(RESOURCE);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    Image image1 = Image.getInstance(IMAGE_PRINTED);
    Image image2 = Image.getInstance(IMAGE_NOT_PRINTED);
    PdfLayer watermark_printed = new PdfLayer("printed", stamper.getWriter());
    watermark_printed.setOn(false);/*from  ww  w . jav a 2 s.com*/
    watermark_printed.setOnPanel(false);
    watermark_printed.setPrint("print", true);
    PdfLayer watermark_not_printed = new PdfLayer("not_printed", stamper.getWriter());
    watermark_not_printed.setOn(true);
    watermark_not_printed.setOnPanel(false);
    watermark_not_printed.setPrint("print", false);
    for (int i = 0; i < stamper.getReader().getNumberOfPages();) {
        PdfContentByte cb = stamper.getUnderContent(++i);
        Rectangle rectangle = stamper.getReader().getPageSizeWithRotation(i);
        cb.beginLayer(watermark_printed);
        float AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image1.getPlainWidth()) / 2;
        float AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image1.getPlainHeight()) / 2;
        image1.setAbsolutePosition(AbsoluteX, AbsoluteY);
        cb.addImage(image1);
        cb.endLayer();
        cb.beginLayer(watermark_not_printed);
        AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image2.getPlainWidth()) / 2;
        AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image2.getPlainHeight()) / 2;
        image2.setAbsolutePosition(AbsoluteX, AbsoluteY);
        cb.addImage(image2);
        cb.endLayer();
    }
    stamper.close();
}