Example usage for com.lowagie.text Image scaleToFit

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

Introduction

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

Prototype

public void scaleToFit(float fitWidth, float fitHeight) 

Source Link

Document

Scales the image so that it fits a certain width and height.

Usage

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 {/*from   ww  w . j  a v a  2  s.  c  o  m*/
        // 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 {/*from ww  w  .j a  v  a  2  s . 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.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 {/* ww  w .jav a 2 s .  co 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.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 av  a2  s  . c  o  m*/
    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:za.co.equalpay.web.utils.PDFExportUtility.java

/**
 * Perform the standard PDF PreProcessing: <br>
 * Add Customer logo image and Phrase as header to the first page, <br>
 * Add Line Separator to the bottom of the Image <br>
 * and add the standard footer message to the PDF document<br>
 *
 * @throws MalformedURLException/*from www . j  a v  a 2s .  c o  m*/
 * @throws IOException
 * @throws DocumentException
 */
public void preProcess() throws MalformedURLException, IOException, DocumentException {
    document.setMargins(50f, 50f, 10f, 20f);

    BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false);
    // Font font = new Font(bf_helv, 8);

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();

    String fontPath = LogoPathFinder.getFontPath(servletContext, "Tahoma");
    BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, true);
    Font tahoma = new Font(bf, 16, Font.BOLD);
    tahoma.setColor(Color.GRAY);

    Font fontBold = new Font(bf, 8, Font.BOLD);
    fontBold.setColor(Color.GRAY);

    Font f = new Font(bf, 8, Font.NORMAL);
    f.setColor(Color.GRAY);

    Font sf = new Font(bf, 6, Font.NORMAL);
    sf.setColor(Color.LIGHT_GRAY);

    // image.setIndentationLeft(360f);
    // PdfPCell imgCell = new PdfPCell(image, false);
    // imgCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    // imgCell.setBorder(0);
    //
    // PdfPCell emptyCell = new PdfPCell(new Phrase("TESTING...", tahoma));
    // emptyCell.setBorder(0);
    // PdfPTable footerTable = new PdfPTable(1);
    // footerTable.setWidthPercentage(100);
    //
    //
    // phrase = new Phrase(PDF_FOOTER_MESSAGE, f);
    //
    // PdfPCell cell = new PdfPCell(phrase);
    // cell.setBorder(0);
    // footerTable.addCell(cell);
    // table.setWidths(new int[] { 1, 2 });
    // table.addCell(emptyCell);
    // footerTable.addCell(emptyCell);
    // phrase.add(footerTable);
    // phrase.add(new Chunk(image, 475f, 0));
    // Phrase subPhrase = new Phrase();
    // subPhrase.add(new Phrase("\nwww.meddev.co.za\n", sf));
    // subPhrase.add(new Phrase(PDF_FOOTER_MESSAGE, f));
    // phrase.add(subPhrase);
    // phrase.add(new Chunk("www.meddev.co.za", f));
    // phrase.add(new Phrase(PDF_FOOTER_MESSAGE, f));
    // load document footer
    HeaderFooter footer = new HeaderFooter(new Phrase(PDF_FOOTER_MESSAGE, f), false);
    // HeaderFooter footer = new HeaderFooter(paragraph, false);
    // HeaderFooter footer = new HeaderFooter(phrase, false);

    footer.setAlignment(Element.ALIGN_LEFT);
    footer.setBorder(Rectangle.NO_BORDER);
    document.setFooter(footer);

    // document.open();
    //
    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // PdfWriter writer = PdfWriter.getInstance(document, baos);
    //
    // writer.open();
    // PdfContentByte cb = writer.getDirectContent();
    // cb.addImage(image);
    // ColumnText columnText = new ColumnText(cb);
    // load the customer logo
    String logoPath = LogoPathFinder.getPath(servletContext, "meddev-logo2");

    Image image = Image.getInstance(logoPath);
    image.scaleToFit(149, 55);
    image.setIndentationLeft(360f);

    PdfPCell imageCell = new PdfPCell(image, false);
    imageCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    imageCell.setBorder(0);

    PdfPCell headerTextCell = new PdfPCell(new Phrase(header, tahoma));
    headerTextCell.setBorder(0);

    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    table.setWidths(new int[] { 1, 2 });
    table.addCell(headerTextCell);
    table.addCell(imageCell);

    phrase = new Phrase();
    phrase.add(table);

    // load document header
    HeaderFooter header = new HeaderFooter(phrase, false);
    header.setAlignment(Element.ALIGN_CENTER);
    header.setBorder(Rectangle.NO_BORDER);

    document.setHeader(header);

    // create gray line separator
    // Chunk lineSeparator = new Chunk(new LineSeparator(1, 100, Color.GRAY,
    // Element.ALIGN_CENTER, -2));
    // document.add(lineSeparator);
    Font headerFont = new Font(bf_helv, 9);
    // write the header lines for this statement
    phrase = new Phrase(12, "\n", headerFont);
    phrase.add("\n\n");

    // open the pdf document for editing
    document.open();
    document.setPageSize(PageSize.A4);

    // Meddev Information Block
    table = new PdfPTable(4);
    table.setWidthPercentage(100);

    // table.setWidths(new int[] { 25, 25, 30, 20 });
    Phrase phrase1 = new Phrase();
    phrase1.add(new Phrase("MED DEV cc", fontBold));
    phrase1.add(new Phrase("\nUNIT 10, THE CORNER", f));
    phrase1.add(new Phrase("\nc/o Theuns & Hilde Ave", f));
    phrase1.add(new Phrase("\nHennopspark, 0157", f));
    phrase1.add(new Phrase("\nTel: +27 (0) 12 653 3063", f));
    phrase1.add(new Phrase("\nReg No: 2006/166603/23", f));
    phrase1.add(new Phrase("\n", f));

    Phrase phrase2 = new Phrase();
    phrase2.add(new Phrase("VAT No: ", fontBold));
    phrase2.add(new Phrase("4150231498", f));
    phrase2.add(new Phrase("\nImport/Export #: ", fontBold));
    phrase2.add(new Phrase("20544748", f));
    phrase2.add(new Phrase("\n", f));

    Phrase phrase3 = new Phrase();
    phrase3.add(new Phrase("QUOTE NO ", fontBold));
    phrase3.add(new Phrase("\nDATE ", fontBold));
    phrase3.add(new Phrase("\nREFERNCE ", fontBold));
    phrase3.add(new Phrase("\nSUPPLIER CODE ", fontBold));
    phrase3.add(new Phrase("\nEXPIRY DATE ", fontBold));

    Phrase phrase4 = new Phrase();
    //        phrase4.add(new Phrase(quotation.getQuotationNumber(), f));
    //        phrase4.add(new Phrase("\n" + DateFormatter.formatMonthDate(quotation.getQuotationDate()), f));
    //        phrase4.add(new Phrase("\n" + (quotation.getClient().getReference() == null ? "-" : quotation.getClient().getReference()), f));
    //        phrase4.add(new Phrase("\nMEDDEV1", f));
    //        phrase4.add(new Phrase("\n" + DateFormatter.formatMonthDate(quotation.calculateExpiryDate()), f));

    PdfPCell cell1 = new PdfPCell(phrase1);
    cell1.setBorder(0);
    PdfPCell cell2 = new PdfPCell(phrase2);
    cell2.setBorder(0);
    PdfPCell cell3 = new PdfPCell(phrase3);
    cell3.setBorder(0);
    PdfPCell cell4 = new PdfPCell(phrase4);
    cell4.setBorder(0);

    table.addCell(cell1);
    table.addCell(cell2);
    table.addCell(cell3);
    table.addCell(cell4);

    phrase.add(table);

    // create gray line separator
    Chunk lineSeparator = new Chunk(new LineSeparator(1, 100, Color.GRAY, Element.ALIGN_CENTER, -2));
    phrase.add(lineSeparator);

    // Customer Information Block
    table = new PdfPTable(3);
    table.setWidthPercentage(100);
    table.setWidths(new int[] { 5, 2, 3 });

    //        Phrase phrase11 = new Phrase();
    //        phrase11.add(new Phrase(quotation.getClient().getCompany().toUpperCase() + "(CLIENT)", fontBold));
    //        if (quotation.getClient().getVatRegNo() != null && quotation.getClient().getVatRegNo().trim().length() > 0) {
    //            phrase11.add(new Phrase("\nCustomer VAT No: ", fontBold));
    //            phrase11.add(new Phrase(quotation.getClient().getVatRegNo(), f));
    //        }
    //        if (quotation.getClient().getResAddress1() != null && quotation.getClient().getResAddress1().trim().length() > 0) {
    //            phrase11.add(new Phrase("\n" + quotation.getClient().getResAddress1(), f));
    //        }
    //        if (quotation.getClient().getResAddress2() != null && quotation.getClient().getResAddress2().trim().length() > 0) {
    //            phrase11.add(new Phrase("\n" + quotation.getClient().getResAddress2(), f));
    //        }
    //        if (quotation.getClient().getResCity() != null && quotation.getClient().getResCity().trim().length() > 0) {
    //            phrase11.add(new Phrase("\n" + quotation.getClient().getResCity(), f));
    //        }
    //        if (quotation.getClient().getPhone() != null && quotation.getClient().getPhone().trim().length() > 0) {
    //            phrase11.add(new Phrase("\nTel: " + quotation.getClient().getPhone(), f));
    //        }

    //PdfPCell cell11 = new PdfPCell(phrase11);
    //cell11.setBorder(0);

    phrase2 = new Phrase();
    phrase2.add(new Phrase("CURRENCY: ", fontBold));
    PdfPCell cell12 = new PdfPCell(phrase2);
    cell12.setBorder(0);

    phrase3 = new Phrase();
    //phrase3.add(new Phrase(quotation.getClient().getCurrencyCode(), f));
    PdfPCell cell13 = new PdfPCell(phrase3);
    cell13.setBorder(0);

    // table.addCell(cell11);
    table.addCell(cell12);
    table.addCell(cell13);

    phrase.add(table);

    // create gray line separator
    phrase.add("\n");
    document.add(phrase);

    String footerImagePath = LogoPathFinder.getPath(servletContext, "footer-image");

    Image footerImage = Image.getInstance(footerImagePath);
    footerImage.scaleToFit(90, 50);
    footerImage.setAbsolutePosition((PageSize.A4.getWidth() - (footerImage.getScaledWidth() + 40)),
            (PageSize.A4.getHeight() - (PageSize.A4.getHeight() - (footerImage.getScaledHeight() - 10))));

    document.add(footerImage);

}