Example usage for com.itextpdf.text Chunk setAction

List of usage examples for com.itextpdf.text Chunk setAction

Introduction

In this page you can find the example usage for com.itextpdf.text Chunk setAction.

Prototype


public Chunk setAction(final PdfAction action) 

Source Link

Document

Sets an action for this Chunk.

Usage

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

License:Open Source License

private PdfPTable buildModelPricingTables(Contact contact, Model model) {

    final PdfPTable table = new PdfPTable(1);

    final List<Pricing> validPricings = model.validPricings(contact);

    if (CollectionUtils.isEmpty(validPricings)) {
        final Chunk chunk = new Chunk("Contact us for pricing on this model", iTextUtil.getFontContactUs());
        chunk.setAction(new PdfAction(contactUsUrl));
        table.addCell(cell(new Phrase(chunk)));
    } else {/*from  ww w . jav  a 2s  . c  om*/
        for (Pricing pricing : validPricings) {
            table.addCell(cell(buildModelPricingTable(pricing)));
        }
    }

    return table;

}

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

License:Open Source License

private PdfPTable buildModelDetailSection(Model model, PdfWriter pdfWriter)
        throws IOException, BadElementException {

    final Phrase p = processHtmlCodes(model.getProductNameProcessed(), iTextUtil.getFontModelTitle(),
            iTextUtil.getFontModelSymbol());
    for (Chunk c : p.getChunks()) {
        c.setAction(new PdfAction(model.getUrl()));
    }//from w w  w  .  j a v  a2 s  . c  om

    final StringBuilder strAppList = new StringBuilder();
    for (String application : model.getApplicationsSorted()) {
        if (strAppList.length() != 0) {
            strAppList.append(", ");
        }
        strAppList.append(application);
    }

    final PdfPTable table = new PdfPTable(1);

    table.addCell(cell(p));

    table.addCell(createRow("Model Number", model.getModelNumber(), null));
    table.addCell(createRow("Animal Type", model.getAnimalType(), null));
    table.addCell(createRow("Nomenclature", model.getNomenclatureParsed(), null));
    table.addCell(createRow("Application(s)", strAppList.toString(), null));
    table.addCell(createRow("Health Report", model.getHealthReport(), model.getHealthReport()));
    table.addCell(createRow("Species", model.getSpecies(), null));

    table.addCell(cell(createOrderButton(model)));

    return table;

}

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

License:Open Source License

private PdfPTable createOrderButton(Model model) throws IOException, BadElementException {

    final Chunk chunk = new Chunk("Order on taconic.com", iTextUtil.getFontButton());
    chunk.setAction(
            new PdfAction("http://www.taconic.com/start-an-order?modelNumber=" + model.getModelNumber()));

    final PdfPCell cell = cell(new Phrase(chunk));
    cell.setBackgroundColor(iTextUtil.getTaconicRed());
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setPaddingTop(5);/*w  w w .j  a  v a2s.c om*/
    cell.setPaddingBottom(8);

    final PdfPTable button = new PdfPTable(new float[] { 80f, 20f });
    button.addCell(cell(new Phrase(" "), 2));
    button.addCell(cell);
    button.addCell(cell(new Phrase(" ")));
    button.addCell(cell(new Phrase(" "), 2));

    return button;
}

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

License:Open Source License

private PdfPCell createRow(String key, String value, String url) {
    final Paragraph paragraph = new Paragraph();
    paragraph.add(new Chunk(key, iTextUtil.getFontModelKey()));
    paragraph.add(new Chunk(": ", iTextUtil.getFontModelKey()));

    if (value == null) {
        value = "";
    }/* ww  w. j  a  va2s.  com*/

    final Phrase valuePhrase = processHtmlCodes(value.trim(), iTextUtil.getFontModelValue(),
            iTextUtil.getFontModelSymbol());
    if (!StringUtils.isEmpty(url)) {
        for (Chunk chunk : valuePhrase.getChunks()) {
            chunk.setAction(new PdfAction(url));
        }
    }
    for (Chunk chunk : valuePhrase.getChunks()) {
        chunk.setLineHeight(13f);
    }
    paragraph.add(valuePhrase);

    final PdfPCell cell = cell(paragraph);
    cell.setPaddingBottom(5f);
    return cell;
}

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));
        }// www . ja  va  2s . 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();//  w ww . ja  va 2  s .c o  m

            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:com.athena.chameleon.engine.core.PDFCommonEventHelper.java

License:Apache License

/**
 * /*www.j  a  v  a2  s  . c  o m*/
 *  Paragraph ?.(Navigation  ?  title ) 
 *
 * @param title 
 * @param pageNumber 
 * @param depth 
 * @param x1 document left 
 * @param x2 document right 
 * @return Paragraph
 */
public Paragraph getTocParagraph(String title, int pageNumber, int depth, float x1, float x2) {

    Font tocFont = new Font(bfKorean, 10);
    if (depth == 0)
        tocFont.setStyle(Font.BOLD);

    Paragraph p = new Paragraph();
    p.setSpacingAfter(5);

    Chunk tit = new Chunk(title + " ", tocFont);
    tit.setAction(PdfAction.gotoLocalPage(title, false));

    Chunk point = new Chunk(".", tocFont);

    Chunk number = new Chunk(" " + pageNumber, tocFont);
    number.setAction(PdfAction.gotoLocalPage(title, false));

    p.add(tit);

    float width = x2 - x1 - tit.getWidthPoint() - number.getWidthPoint() - (depth * 12);

    if ((x2 - x1) < tit.getWidthPoint())
        width = x2 - x1 - (tit.getWidthPoint() - (x2 - x1)) - number.getWidthPoint() - (depth * 12) - 65;

    float i = point.getWidthPoint();
    while (i < width) {
        p.add(point);
        i += point.getWidthPoint();
    }

    p.add(number);

    return p;
}

From source file:com.athena.chameleon.engine.core.PDFDocGenerator.java

License:Apache License

public static void setLastPageInfo(Document doc, PdfWriter writer, int cNum) throws Exception {
    Chapter chapter = PDFWriterUtil.getChapter(MessageUtil.getMessage("pdf.message.chapter.confirm.title"),
            cNum);// www  . j  a  v  a2 s  . com

    Paragraph preP = new Paragraph();
    preP.add(new Phrase(MessageUtil.getMessage("pdf.message.chapter.confirm.prepared1"),
            PDFWriterUtil.fnNormalBold));
    preP.add(new Phrase("                                                           ",
            new Font(bfKorean, 10, Font.UNDERLINE)));
    preP.setSpacingBefore(15);
    preP.setSpacingAfter(2);
    chapter.add(preP);

    preP = new Paragraph();
    preP.add(new Phrase(MessageUtil.getMessage("pdf.message.chapter.confirm.prepared2"),
            PDFWriterUtil.fnNormal));
    preP.setIndentationLeft(65);
    preP.setSpacingAfter(14);
    chapter.add(preP);

    preP = new Paragraph();
    preP.add(new Phrase(MessageUtil.getMessage("pdf.message.chapter.confirm.approved1"),
            PDFWriterUtil.fnNormalBold));
    preP.add(new Phrase("                                                           ",
            new Font(bfKorean, 10, Font.UNDERLINE)));
    preP.setSpacingBefore(15);
    preP.setSpacingAfter(2);
    chapter.add(preP);

    preP = new Paragraph();
    preP.add(new Phrase(MessageUtil.getMessage("pdf.message.chapter.confirm.approved2"),
            PDFWriterUtil.fnNormal));
    preP.setIndentationLeft(65);
    preP.setSpacingAfter(14);
    chapter.add(preP);

    cNum++;
    doc.add(chapter);

    chapter = PDFWriterUtil.getChapter(MessageUtil.getMessage("pdf.message.chapter.appendices.title"), cNum);
    Section section = PDFWriterUtil.getSection(chapter,
            MessageUtil.getMessage("pdf.message.chapter.appendices.label1"));

    Chunk url = new Chunk(MessageUtil.getMessage("pdf.message.chapter.appendices.text1"), PDFWriterUtil.fnURL);
    url.setAction(new PdfAction(new URL(MessageUtil.getMessage("pdf.message.chapter.appendices.text1"))));

    preP = new Paragraph(url);
    preP.setIndentationLeft(23);
    preP.setSpacingAfter(14);
    section.add(preP);

    section = PDFWriterUtil.getSection(chapter,
            MessageUtil.getMessage("pdf.message.chapter.appendices.label2"));

    doc.add(chapter);
}

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

License:Apache License

/**
 * /*from   ww w .  ja  v  a2  s .co  m*/
 * url Mapping
 *
 * @param e url   element
 * @return Chunk
 * @throws Exception
 */
public static Chunk getUrl(Element e) throws Exception {
    Chunk url = new Chunk(e.getText(), fnURL);
    url.setAction(new PdfAction(new URL(e.getText())));

    return url;
}

From source file:nz.ac.waikato.cms.doc.HyperLinkGrades.java

License:Open Source License

/**
 * Adds the index with locations to the existing PDF.
 *
 * @param locations   the locations to index
 * @param input   the input PDF//from w w  w  .  ja  v a2s.co m
 * @param output   the output PDF
 * @return      true if successfully generated
 */
public static boolean addIndex(List<Location> locations, File input, File output) {
    try {
        // copy pages, add target
        PdfReader reader = new PdfReader(input.getAbsolutePath());
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(output.getAbsolutePath()));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        PdfImportedPage page;
        float height = 0;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            document.newPage();
            page = writer.getImportedPage(reader, i);
            canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
            Chunk loc = new Chunk(" ");
            loc.setLocalDestination("loc" + i);
            height = page.getHeight();
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(loc), 50, height - 50, 0);
        }
        // add index
        for (int i = 0; i < locations.size(); i++) {
            Location loc = locations.get(i);
            if (i % MAX_ITEMS_PER_PAGE == 0)
                document.newPage();
            String text = loc.getID() + " " + (loc.getName() == null ? "???" : loc.getName());
            Chunk chunk = new Chunk("Page " + (loc.getPage() + 1) + ": " + text);
            chunk.setAction(PdfAction.gotoLocalPage("loc" + (loc.getPage() + 1), false));
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(chunk), 50,
                    height - 100 - (i % MAX_ITEMS_PER_PAGE) * 20, 0);
        }
        document.close();

        return true;
    } catch (Exception e) {
        System.err.println("Failed to overlay locations!");
        e.printStackTrace();
        return false;
    }
}