Example usage for com.itextpdf.text.pdf ColumnText ColumnText

List of usage examples for com.itextpdf.text.pdf ColumnText ColumnText

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf ColumnText ColumnText.

Prototype

public ColumnText(final PdfContentByte canvas) 

Source Link

Document

Creates a ColumnText.

Usage

From source file:adams.flow.transformer.pdfproclet.AbstractPdfProclet.java

License:Open Source License

/**
 * Adds the column text to the document at the specified location.
 * Dimension: x to page width, 0 to y./*from  w  ww  .  j ava 2 s  . co  m*/
 *
 * @param generator   the context
 * @return      the column text
 * @throws Exception   if adding fails
 */
protected ColumnText addColumnTextAt(PDFGenerator generator, float x, float y) throws Exception {
    ColumnText result;

    result = new ColumnText(generator.getWriter().getDirectContent());
    result.setSimpleColumn(x, y, generator.getDocument().getPageSize().getWidth(), 0);

    return result;
}

From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java

License:Open Source License

private byte[] personalize(byte[] pdf, Contact contact, Toc tableOfContents)
        throws IOException, DocumentException {

    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {

        final PdfReader reader = new PdfReader(pdf);
        final PdfStamper stamper = new PdfStamper(reader, bos);

        // stamp some text on first page
        PdfContentByte text = stamper.getOverContent(1);

        text.beginText();//from  www  .j  a v  a 2  s  .  c o m

        text.setColorFill(iTextUtil.getFontCoverText().getColor());
        text.setFontAndSize(iTextUtil.getFontCoverText().getBaseFont(), iTextUtil.getFontCoverText().getSize());
        text.showTextAligned(Element.ALIGN_RIGHT, coverTitle1,
                text.getPdfDocument().getPageSize().getWidth() - 15, 195, 0);
        text.showTextAligned(Element.ALIGN_RIGHT, coverTitle2,
                text.getPdfDocument().getPageSize().getWidth() - 15, 175, 0);
        text.showTextAligned(Element.ALIGN_RIGHT, contact.getCurrency().getTitlePageDescription(),
                text.getPdfDocument().getPageSize().getWidth() - 15, 80, 0);

        text.setColorFill(iTextUtil.getFontCoverPricingguide().getColor());
        text.setFontAndSize(iTextUtil.getFontCoverPricingguide().getBaseFont(),
                iTextUtil.getFontCoverPricingguide().getSize());
        text.showTextAligned(Element.ALIGN_RIGHT, coverTitle3,
                text.getPdfDocument().getPageSize().getWidth() - 15, 145, 0);

        text.setColorFill(iTextUtil.getFontCoverYear().getColor());
        text.setFontAndSize(iTextUtil.getFontCoverYear().getBaseFont(), iTextUtil.getFontCoverYear().getSize());
        text.showTextAligned(Element.ALIGN_RIGHT, coverTitle4,
                text.getPdfDocument().getPageSize().getWidth() - 15, 105, 0);

        text.endText();

        // stamp some text on first page of the table of contents page
        final Image logoImage = iTextUtil.getImageFromByteArray(HttpUtil.readByteArray(
                pdfTemplate.getLogo().getUrl(), defaultService.getUserName(), defaultService.getPassword()));
        final PdfContentByte tocContent = stamper.getOverContent(tableOfContents.getFirstPageOfToc());
        final float resizeRatio = logoImage.getHeight() / 85; // define the desired height of the log
        tocContent.addImage(logoImage, logoImage.getWidth() / resizeRatio, 0, 0,
                logoImage.getHeight() / resizeRatio, 59, 615);

        text = stamper.getOverContent(tableOfContents.getFirstPageOfToc());

        text.beginText();

        text.setColorFill(iTextUtil.getFontPersonalization().getColor());
        text.setFontAndSize(iTextUtil.getFontPersonalization().getBaseFont(),
                iTextUtil.getFontPersonalization().getSize());
        text.showTextAligned(Element.ALIGN_LEFT, "Prepared for:", 355, 681, 0);
        text.showTextAligned(Element.ALIGN_LEFT, contact.getFullName(), 355, 662, 0);

        // set company name
        if (!StringUtils.isEmpty(contact.getCompany())) {
            text.showTextAligned(Element.ALIGN_LEFT, contact.getCompany(), 355, 643, 0);
            text.showTextAligned(Element.ALIGN_LEFT, new SimpleDateFormat("MM-dd-yyyy").format(new Date()), 355,
                    624, 0);
        } else {
            text.showTextAligned(Element.ALIGN_LEFT, new SimpleDateFormat("MM-dd-yyyy").format(new Date()), 355,
                    643, 0);
        }

        text.endText();

        final ColumnText ct = new ColumnText(tocContent);
        ct.setSimpleColumn(new Rectangle(55, 517, iTextUtil.PAGE_SIZE.getWidth() - 45, 575));
        final List<Element> elements = HTMLWorker.parseToList(new StringReader(disclaimer), null);
        final Paragraph p = new Paragraph();
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        for (Element element : elements) {
            for (Chunk chunk : element.getChunks()) {
                chunk.setFont(iTextUtil.getFontDisclaimer());
            }
            p.add(element);
        }
        ct.addElement(p);
        ct.go();

        stamper.close();
        reader.close();
        return bos.toByteArray();

    }

}

From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java

License:Open Source License

private byte[] stampTableOfContents(byte[] pdf, Toc tableOfContents) throws IOException, DocumentException {

    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {

        final PdfReader reader = new PdfReader(pdf);
        final PdfStamper stamper = new PdfStamper(reader, bos);

        // stamp the named destinations
        for (int pageNumber = 1; pageNumber <= reader.getNumberOfPages(); pageNumber++) {
            stamper.addNamedDestination("page" + pageNumber, pageNumber,
                    new PdfDestination(PdfDestination.XYZ, 80f, 800f, 0));
        }/* ww w .j a va2 s.  co  m*/

        // create the table of contents
        final Chunk tocTitle = new Chunk("TABLE OF CONTENTS", iTextUtil.getFontTocTitle());

        int currentTocPage = tableOfContents.getFirstPageOfToc();
        int firstTocPage = currentTocPage;
        PdfContentByte canvas = stamper.getOverContent(currentTocPage);

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(tocTitle), 55, 470, 0);

        final List<TocEntry> entriesSorted = tableOfContents.getEntriesSorted();
        int tocEntryNumber = 0;
        for (TocEntry tocEntry : entriesSorted) {

            if (tocEntry.isIncludedInToc()) {
                tocEntryNumber++;

                // take the right TOC page to stamp the TOC entry on (needed for TOC's with multiple pages)
                if (tocEntryNumber == getNumberOfItemsPerTocPage(0)
                        || (tocEntryNumber > getNumberOfItemsPerTocPage(0)
                                && (tocEntryNumber - getNumberOfItemsPerTocPage(0))
                                        % getNumberOfItemsPerTocPage(currentTocPage - firstTocPage) == 0)) {
                    currentTocPage++;
                    canvas = stamper.getOverContent(currentTocPage);
                }

                Font font = iTextUtil.getFontToc();
                if (tocEntry.getLevel() == 1) {
                    font = iTextUtil.getFontTocBold();
                }

                final Phrase p = processHtmlCodes(tocEntry.getLevelString() + tocEntry.getName(), font,
                        iTextUtil.getFontTocSymbol());
                p.add(new Chunk("", iTextUtil.getFontToc()));
                if (tocEntry.isShowingPageNumber()) {
                    p.add(new Chunk(new DottedLineSeparator()));
                    p.add(new Chunk("  " + String.valueOf(tocEntry.getFinalPageNumber()),
                            iTextUtil.getFontToc()));
                }

                for (Chunk chunk : p.getChunks()) {
                    chunk.setAction(PdfAction.gotoLocalPage("page" + tocEntry.getFinalPageNumber(), false));
                }

                int y;
                if (tocEntryNumber < getNumberOfItemsPerTocPage(0)) {
                    y = 460 - (16 * (tocEntryNumber % getNumberOfItemsPerTocPage(0)));
                } else {
                    y = 680 - (16 * ((tocEntryNumber - getNumberOfItemsPerTocPage(0))
                            % getNumberOfItemsPerTocPage(currentTocPage - firstTocPage)));
                }

                final ColumnText ct = new ColumnText(canvas);
                ct.setSimpleColumn(p, 52, y, 555, 70, 0, Element.ALIGN_JUSTIFIED);
                ct.go();

            }
        }

        stamper.close();
        reader.close();

        return bos.toByteArray();

    }

}

From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java

License:Open Source License

private byte[] enableLinkToWebsite(byte[] pdf, Toc tableOfContents) throws IOException, DocumentException {

    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {

        final PdfReader reader = new PdfReader(pdf);

        final PdfStamper stamper = new PdfStamper(reader, bos);

        for (int i = tableOfContents.getFirstPageOfToc(); i <= tableOfContents
                .getLastPageNumberOfModelPages(); i++) {

            final Chunk websiteChunk = new Chunk("..................");
            websiteChunk.setAction(new PdfAction(websiteLink));

            ColumnText ct = new ColumnText(stamper.getUnderContent(i));
            ct.setSimpleColumn(335, 10, 400, 35);
            ct.addText(new Phrase(websiteChunk));
            ct.go();//from www.  j  a  v  a  2 s.  c  om

            final Chunk emailChunk = new Chunk(".........................................");
            emailChunk.setAction(new PdfAction(emailLink));

            ct = new ColumnText(stamper.getUnderContent(i));
            ct.setSimpleColumn(240, 10, 330, 35);
            ct.addText(new Phrase(emailChunk));
            ct.go();

        }

        stamper.close();
        reader.close();
        return bos.toByteArray();

    }

}

From source file:be.roots.taconic.pricingguide.util.iTextUtil.java

License:Open Source License

public static byte[] setPageNumbers(byte[] pdfDocument) throws IOException, DocumentException {

    final int numberOfPages = numberOfPages(pdfDocument);

    if (numberOfPages > 1) {

        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

            final PdfReader reader = new PdfReader(pdfDocument);
            final PdfStamper stamper = new PdfStamper(reader, baos);

            for (int pageNumber = 2; pageNumber <= numberOfPages; pageNumber++) {
                // get the first page
                final PdfContentByte canvas = stamper.getOverContent(pageNumber);

                // stamp the footer on the page
                final ColumnText ct = new ColumnText(canvas);

                ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                        new Phrase(pageNumber + "", getFontPageNumber()), 550, 22, 0);
                ct.go();//from   w  w w . j a  va 2s  .  c  om

            }

            // close out
            stamper.close();
            reader.close();

            return baos.toByteArray();

        }

    }

    return pdfDocument;

}

From source file:book.pdftemplates.FillTemplateHelper.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte canvas = writer.getDirectContentUnder();
    // background
    canvas.addTemplate(background, 0, 0);
    try {/*from   w ww . j  a v a 2  s.  com*/
        // date
        ColumnText ct = new ColumnText(canvas);
        ct.setSimpleColumn(date);
        ct.addText(new Chunk(today, font));
        ct.go();
        // footer (page X of Y)
        ct.setSimpleColumn(footer);
        ct.addText(new Chunk("page " + writer.getPageNumber(), font));
        ct.addText(new Chunk(Image.getInstance(total), 0, 0));
        ct.go();
        // from address
        ct.setSimpleColumn(from);
        ct.addElement(new Paragraph(sender, font));
        ct.go();
        // to address
        ct.setSimpleColumn(to);
        ct.addElement(new Paragraph(receiver, font));
        ct.go();
    } catch (DocumentException e) {
        // can never happen, but if it does, we want to know!
        throw new ExceptionConverter(e);
    }
}

From source file:co.com.realtech.mariner.util.files.PDFUtils.java

public static File agregarTexto(byte[] bytes, String text) {
    File temp = null;//from   www.  j  av  a  2s .  co m
    try {
        temp = File.createTempFile("archivo", ".pdf");
        OutputStream oos = new FileOutputStream(temp);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, oos);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        //InputStream targetStream = new FileInputStream(initialFile);
        // Load existing PDF
        PdfReader reader = new PdfReader(bis);
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        // Copy first page of existing PDF into output PDF
        document.setPageSize(reader.getPageSize(1));
        document.newPage();
        cb.addTemplate(page, 0, 0);

        ColumnText ct = new ColumnText(cb);
        Phrase myText = new Phrase(text);
        Font fuente = new Font();
        fuente.setSize(6);
        myText.setFont(fuente);
        ct.setSimpleColumn(myText, 0, -1, document.right(), document.top(), -10, Element.ALIGN_RIGHT);
        ct.go();

        ColumnText ct2 = new ColumnText(cb);
        Phrase myText2 = new Phrase(text);
        Font fuente2 = new Font();
        fuente2.setSize(6);
        myText2.setFont(fuente);
        ct2.setSimpleColumn(myText, 0, -1, document.right(), document.top(), 248, Element.ALIGN_RIGHT);
        ct2.go();

        ColumnText ct3 = new ColumnText(cb);
        Phrase myText3 = new Phrase(text);
        Font fuente3 = new Font();
        fuente3.setSize(6);
        myText3.setFont(fuente);
        ct3.setSimpleColumn(myText, 0, -1, document.right(), document.top(), 505, Element.ALIGN_RIGHT);
        ct3.go();

        document.close();
    } catch (Exception e) {
        System.out.println("e = " + e);
    }
    return temp;
}

From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java

License:Apache License

/**
 * /*from  w ww  .jav a2s. c o  m*/
 *  box 
 *
 * @param section box  section ?
 * @param e box   element
 * @throws Exception
 */
public static void setBox(Section section, Element e) throws Exception {

    PdfPTable t = new PdfPTable(1);
    t.setSpacingBefore(1);
    t.setSpacingAfter(12);

    if (e.getAttributeValue("width") != null)
        t.setTotalWidth(Float.parseFloat(e.getAttributeValue("width")));

    PdfPCell cell = new PdfPCell();
    if (e.getAttributeValue("option") != null) {

        ColumnText col = new ColumnText(null);
        for (Element e1 : e.getChildren()) {
            if (e1.getAttributeValue("type").equals("red"))
                col.addText(new Phrase(e1.getText(), fnBoxRed));
            else
                col.addText(new Phrase(e1.getText(), fnBox));
        }
        cell.setColumn(col);

    } else {
        cell.setPhrase(new Phrase(e.getText(), fnBox));
    }

    cell.setPaddingLeft(10);
    cell.setLeading(0.0F, 1.8F);
    cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(new BaseColor(238, 236, 225));

    t.addCell(cell);

    section.add(t);

}

From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java

License:Apache License

/**
 * //from   ww  w .ja v a2  s . c  o m
 * box (Backgroud : Black)
 *
 * @param section box  section ?
 * @param e box   element
 * @throws Exception
 */
public static void setBoxB(Section section, Element e) throws Exception {

    PdfPTable t = new PdfPTable(1);
    t.setSpacingBefore(1);
    t.setSpacingAfter(12);

    if (e.getAttributeValue("width") != null)
        t.setTotalWidth(Float.parseFloat(e.getAttributeValue("width")));

    PdfPCell cell = new PdfPCell();
    if (e.getAttributeValue("option") != null) {

        ColumnText col = new ColumnText(null);
        for (Element e1 : e.getChildren()) {
            if (e1.getAttributeValue("type").equals("red"))
                col.addText(new Phrase(e1.getText(), fnBoxRed));
            else
                col.addText(new Phrase(e1.getText(), fnBoxWhite));
        }
        cell.setColumn(col);

    } else {
        cell.setPhrase(new Phrase(e.getText(), fnBoxWhite));
    }

    cell.setPaddingLeft(10);
    cell.setLeading(0.0F, 1.8F);
    cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(new BaseColor(0, 0, 0));

    t.addCell(cell);

    section.add(t);

}

From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java

License:Apache License

/**
 * //from ww  w  .  j  a  va  2  s. c  o m
 * box (Backgroud : White)
 *
 * @param section box  section ?
 * @param e box   element
 * @throws Exception
 */
public static void setBoxW(Section section, Element e) throws Exception {

    PdfPTable t = new PdfPTable(1);
    t.setSpacingBefore(1);
    t.setSpacingAfter(12);

    if (e.getAttributeValue("width") != null)
        t.setTotalWidth(Float.parseFloat(e.getAttributeValue("width")));

    PdfPCell cell = new PdfPCell();
    if (e.getAttributeValue("option") != null) {

        ColumnText col = new ColumnText(null);
        for (Element e1 : e.getChildren()) {
            if (e1.getAttributeValue("type").equals("red"))
                col.addText(new Phrase(e1.getText(), fnBoxRed));
            else
                col.addText(new Phrase(e1.getText(), fnBoxBlack));
        }
        cell.setColumn(col);

    } else {
        cell.setPhrase(new Phrase(e.getText(), fnBoxBlack));
    }

    cell.setPaddingLeft(10);
    cell.setLeading(0.0F, 1.8F);
    cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(new BaseColor(255, 255, 255));

    t.addCell(cell);

    section.add(t);

}