Example usage for com.lowagie.text Chunk setAction

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

Introduction

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

Prototype


public Chunk setAction(PdfAction action) 

Source Link

Document

Sets an action for this Chunk.

Usage

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

/**
 *
 *//*ww w .j  a  v  a2  s .  c o m*/
protected void setHyperlinkInfo(Chunk chunk, JRPrintHyperlink link) {
    if (link != null) {
        switch (link.getHyperlinkTypeValue()) {
        case REFERENCE: {
            if (link.getHyperlinkReference() != null) {
                switch (link.getHyperlinkTargetValue()) {
                case BLANK: {
                    chunk.setAction(PdfAction.javaScript("if (app.viewerVersion < 7)" + "{this.getURL(\""
                            + link.getHyperlinkReference() + "\");}" + "else {app.launchURL(\""
                            + link.getHyperlinkReference() + "\", true);};", pdfWriter));
                    break;
                }
                case SELF:
                default: {
                    chunk.setAnchor(link.getHyperlinkReference());
                    break;
                }
                }
            }
            break;
        }
        case LOCAL_ANCHOR: {
            if (link.getHyperlinkAnchor() != null) {
                chunk.setLocalGoto(link.getHyperlinkAnchor());
            }
            break;
        }
        case LOCAL_PAGE: {
            if (link.getHyperlinkPage() != null) {
                chunk.setLocalGoto(
                        JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + link.getHyperlinkPage().toString());
            }
            break;
        }
        case REMOTE_ANCHOR: {
            if (link.getHyperlinkReference() != null && link.getHyperlinkAnchor() != null) {
                chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkAnchor());
            }
            break;
        }
        case REMOTE_PAGE: {
            if (link.getHyperlinkReference() != null && link.getHyperlinkPage() != null) {
                chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkPage().intValue());
            }
            break;
        }
        case CUSTOM: {
            if (hyperlinkProducerFactory != null) {
                String hyperlink = hyperlinkProducerFactory.produceHyperlink(link);
                if (hyperlink != null) {
                    switch (link.getHyperlinkTargetValue()) {
                    case BLANK: {
                        chunk.setAction(
                                PdfAction.javaScript(
                                        "if (app.viewerVersion < 7)" + "{this.getURL(\"" + hyperlink + "\");}"
                                                + "else {app.launchURL(\"" + hyperlink + "\", true);};",
                                        pdfWriter));
                        break;
                    }
                    case SELF:
                    default: {
                        chunk.setAnchor(hyperlink);
                        break;
                    }
                    }
                }
            }
        }
        case NONE:
        default: {
            break;
        }
        }
    }
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

License:LGPL

/**
 *
 *///from ww w  .  ja va2 s  . co  m
protected void setHyperlinkInfo(Chunk chunk, JRPrintHyperlink link) {
    switch (link.getHyperlinkType()) {
    case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {
        if (link.getHyperlinkReference() != null) {
            switch (link.getHyperlinkTarget()) {
            case JRHyperlink.HYPERLINK_TARGET_BLANK: {
                chunk.setAction(PdfAction.javaScript("if (app.viewerVersion < 7)" + "{this.getURL(\""
                        + link.getHyperlinkReference() + "\");}" + "else {app.launchURL(\""
                        + link.getHyperlinkReference() + "\", true);};", pdfWriter));
                break;
            }
            case JRHyperlink.HYPERLINK_TARGET_SELF:
            default: {
                chunk.setAnchor(link.getHyperlinkReference());
                break;
            }
            }
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR: {
        if (link.getHyperlinkAnchor() != null) {
            chunk.setLocalGoto(link.getHyperlinkAnchor());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: {
        if (link.getHyperlinkPage() != null) {
            chunk.setLocalGoto(JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + link.getHyperlinkPage().toString());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR: {
        if (link.getHyperlinkReference() != null && link.getHyperlinkAnchor() != null) {
            chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkAnchor());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: {
        if (link.getHyperlinkReference() != null && link.getHyperlinkPage() != null) {
            chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkPage().intValue());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_CUSTOM: {
        if (hyperlinkProducerFactory != null) {
            String hyperlink = hyperlinkProducerFactory.produceHyperlink(link);
            if (hyperlink != null) {
                switch (link.getHyperlinkTarget()) {
                case JRHyperlink.HYPERLINK_TARGET_BLANK: {
                    chunk.setAction(
                            PdfAction.javaScript(
                                    "if (app.viewerVersion < 7)" + "{this.getURL(\"" + hyperlink + "\");}"
                                            + "else {app.launchURL(\"" + hyperlink + "\", true);};",
                                    pdfWriter));
                    break;
                }
                case JRHyperlink.HYPERLINK_TARGET_SELF:
                default: {
                    chunk.setAnchor(hyperlink);
                    break;
                }
                }
            }
        }
    }
    case JRHyperlink.HYPERLINK_TYPE_NONE:
    default: {
        break;
    }
    }
}

From source file:org.areasy.common.doclet.document.tags.TagA.java

License:Open Source License

public Element toElement(String text) {
    String addr = getAttribute("href");

    if (addr == null || !DefaultConfiguration.isLinksCreationActive())
        addr = "";
    if (!isPre())
        text = DocletUtility.stripLineFeeds(text);

    Element aChunk;//  www.ja  v  a  2s.  c  o  m
    if (addr.startsWith("locallink")) {
        boolean plainText = addr.startsWith("locallinkplain");
        String dest = addr.substring(addr.indexOf(':') + 1).trim();

        setCode(!plainText);

        return new LinkPhrase(dest, text, Math.max(9, (int) getFont().size()), plainText);
    } else if (addr.equalsIgnoreCase("newpage"))
        return super.toElement(text);
    else if (addr.startsWith("http://") || addr.startsWith("https://")) {
        try {
            URL url = new URL(addr);
            return new Chunk(text, getFont()).setAnchor(url);
        } catch (MalformedURLException e) {
            log.error("Malformed URL: " + addr);
        }
    } else {
        String fileName = addr.trim();
        String anchorName = "";

        int hashIndex = addr.indexOf('#');

        if (hashIndex >= 0) {
            fileName = addr.substring(0, hashIndex).trim();
            anchorName = addr.substring(hashIndex + 1).trim();
        }

        boolean isLocalAnchor = (fileName.length() == 0 && anchorName.length() > 0);
        File file = null;

        try {
            if (fileName.length() > 0)
                file = new File(DocletUtility.getFilePath(fileName));
            else
                file = State.getCurrentFile();
        } catch (FileNotFoundException e) {
            log.debug("Could not find linked file " + fileName);
        }

        if (isLocalAnchor || Destinations.isValidDestinationFile(file)) {
            String fullAnchor = Destinations.createAnchorDestination(file, anchorName);

            PdfAction action = new PdfAction("", "");
            action.remove(PdfName.F);
            action.put(PdfName.S, PdfName.GOTO);
            action.put(PdfName.D, new PdfString(fullAnchor));

            aChunk = new Phrase();

            Chunk chunk = createChunk(text);
            ((Phrase) aChunk).add(chunk.setAction(action));

            return aChunk;
        }
    }

    if (getAttribute("name") != null)
        setLink(false); // no underline for anchors

    Font font = getFont();
    font.setColor(0, 0, 0);

    aChunk = new Chunk(text, font);

    setLink(false);

    return aChunk;
}

From source file:org.cgiar.ccafs.ap.summaries.projects.pdf.CaseStudieSummaryPDF.java

License:Open Source License

/**
 * This method is used for add the main information table of project summary
 *///from   w  ww .  j ava 2s . co m
private void addMainInformationTable() {

    String startDate, endDate;
    if (project.getStartDate() != null) {
        startDate = new SimpleDateFormat("dd-MM-yyyy").format(project.getStartDate());
    } else {
        startDate = this.messageReturn(null);
    }

    if (project.getEndDate() != null) {
        endDate = new SimpleDateFormat("dd-MM-yyyy").format(project.getEndDate());
    } else {
        endDate = this.messageReturn(null);
    }

    Paragraph cellContent;

    // Add content
    try {
        PdfPTable table = new PdfPTable(4);

        // Set table widths
        table.setLockedWidth(true);
        table.setTotalWidth(480);
        table.setWidths(new int[] { 3, 5, 3, 5 });

        // First row
        cellContent = new Paragraph(this.getText("summaries.project.startDate") + "\n" + " (dd-MM-yyyy)",
                TABLE_BODY_BOLD_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 0);

        cellContent = new Paragraph(this.messageReturn(startDate), TABLE_BODY_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 0);

        cellContent = new Paragraph(this.getText("summaries.project.endDate") + "\n" + " (dd-MM-yyyy)",
                TABLE_BODY_BOLD_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 0);

        cellContent = new Paragraph(this.messageReturn(endDate), TABLE_BODY_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 0);

        // Second row
        cellContent = new Paragraph(this.getText("summaries.project.managementLiaison"), TABLE_BODY_BOLD_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 1);

        cellContent = new Paragraph(this.messageReturn(project.getLiaisonInstitution().getAcronym() + " - "
                + project.getLiaisonInstitution().getName()), TABLE_BODY_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 1);

        cellContent = new Paragraph(this.getText("summaries.project.contactPerson"), TABLE_BODY_BOLD_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 1);

        cellContent = new Paragraph(this.messageReturn(project.getOwner().getComposedName()), TABLE_BODY_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 1);

        // Third row
        cellContent = new Paragraph(this.getText("summaries.project.leadOrganization"), TABLE_BODY_BOLD_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 0);
        if (project.getLeader() == null || project.getLeader().getInstitution() == null) {
            cellContent = new Paragraph(this.getText("summaries.project.empty"), TABLE_BODY_FONT);
        } else {
            cellContent = new Paragraph(
                    this.messageReturn(
                            this.messageReturn(project.getLeader().getInstitution().getComposedName())),
                    TABLE_BODY_FONT);

        }
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 0);

        cellContent = new Paragraph(this.getText("summaries.project.projectLeader"), TABLE_BODY_BOLD_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 0);
        if (project.getLeaderPerson() == null || project.getLeaderPerson().getUser() == null) {
            cellContent = new Paragraph(this.getText("summaries.project.empty"), TABLE_BODY_FONT);
        } else {
            cellContent = new Paragraph(
                    this.messageReturn(project.getLeaderPerson().getUser().getComposedName()), TABLE_BODY_FONT);
        }

        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 0);

        // Fourth row
        cellContent = (new Paragraph(this.getText("summaries.project.projectType"), TABLE_BODY_BOLD_FONT));
        this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 1);

        cellContent = new Paragraph(this.messageReturn(project.getType().replaceAll("_", " ")),
                TABLE_BODY_FONT);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 1);

        // Fiveth row
        Chunk imdb = null;
        Font hyperLink = new Font(FontFactory.getFont("openSans", 10, Color.BLUE));
        hyperLink.setStyle(Font.UNDERLINE);

        // -- Not Bilateral
        if (!project.isBilateralProject()) {
            cellContent = (new Paragraph(this.getText("summaries.project.detailed"), TABLE_BODY_BOLD_FONT));
            this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 1);

            if (project.getWorkplanName() != null && !project.getWorkplanName().equals("")) {
                imdb = new Chunk(project.getWorkplanName(), hyperLink);
                try {
                    imdb.setAction(new PdfAction(new URL(this.messageReturn(project.getWorkplanURL()))));
                } catch (MalformedURLException exp) {
                    imdb = new Chunk(project.getWorkplanName(), TABLE_BODY_FONT);
                    LOG.error("There is an Malformed exception in " + project.getWorkplanName());
                }
            } else {
                imdb = new Chunk(this.getText("summaries.project.empty"), TABLE_BODY_FONT);
            }
        }

        // -- Bilateral
        else {
            cellContent = (new Paragraph(this.getText("summaries.project.ipContributions.proposal.space"),
                    TABLE_BODY_BOLD_FONT));
            this.addTableBodyCell(table, cellContent, Element.ALIGN_RIGHT, 1);

            if (project.getBilateralContractProposalName() != null
                    && !project.getBilateralContractProposalName().equals("")) {
                imdb = new Chunk(project.getBilateralContractProposalName(), hyperLink);
                try {
                    imdb.setAction(new PdfAction(new URL(this.messageReturn(project.getWorkplanURL()))));
                } catch (MalformedURLException exp) {
                    imdb = new Chunk(project.getBilateralContractProposalName(), TABLE_BODY_FONT);
                    LOG.error("There is an Malformed exception in bilateral contract: "
                            + project.getBilateralContractProposalName());
                }
            } else {
                imdb = new Chunk(this.getText("summaries.project.empty"), TABLE_BODY_FONT);
            }
        }

        cellContent = new Paragraph();
        cellContent.add(imdb);
        this.addTableBodyCell(table, cellContent, Element.ALIGN_LEFT, 1);

        document.add(table);
        document.add(Chunk.NEWLINE);
    } catch (DocumentException e) {
        LOG.error(
                "-- generatePdf() > There was an error adding the table with content for case study summary. ",
                e);
    }
}

From source file:questions.stamppages.BookmarksToTOC1.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  w  ww. j  a va2  s  .c  om*/
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        writer.setPageEvent(new ParagraphBookmarkEvents(false));
        document.open();
        BufferedReader reader = new BufferedReader(new FileReader(RESOURCE));
        String line;
        Paragraph p;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph(line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            document.add(Chunk.NEWLINE);
        }
        reader.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        PdfReader reader = new PdfReader(baos.toByteArray());
        Rectangle rect = reader.getPageSizeWithRotation(1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.insertPage(1, rect);
        ColumnText column = new ColumnText(stamper.getOverContent(1));
        column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36));
        column.addElement(new Paragraph("TABLE OF CONTENTS"));
        List<Map> list = SimpleBookmark.getBookmark(reader);
        Chunk link;
        PdfAction action;
        String info;
        int p = 1;
        float y = 10;
        for (Map<String, String> bookmark : list) {
            link = new Chunk(bookmark.get("Title"));
            info = bookmark.get("Page");
            p = Integer.parseInt(info.substring(0, info.indexOf(' ')));
            y = Float.parseFloat(info.substring(info.lastIndexOf(' ') + 1) + "f");
            action = PdfAction.gotoLocalPage(p, new PdfDestination(PdfDestination.FITH, y),
                    stamper.getWriter());
            link.setAction(action);
            column.addElement(new Paragraph(link));
        }
        column.go();
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}

From source file:questions.stamppages.BookmarksToTOC2.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*  w  ww  .j  av  a  2s .  co m*/
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        writer.setPageEvent(new ParagraphBookmarkEvents(true));
        document.open();
        BufferedReader reader = new BufferedReader(new FileReader(RESOURCE));
        String line;
        Paragraph p;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph(line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            document.add(Chunk.NEWLINE);
        }
        reader.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        PdfReader reader = new PdfReader(baos.toByteArray());
        Rectangle rect = reader.getPageSizeWithRotation(1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.insertPage(1, rect);
        ColumnText column = new ColumnText(stamper.getOverContent(1));
        column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36));
        column.addElement(new Paragraph("TABLE OF CONTENTS"));
        List<Map> list = SimpleBookmark.getBookmark(reader);
        Chunk link;
        String dest;
        for (Map<String, String> bookmark : list) {
            link = new Chunk(bookmark.get("Title"));
            dest = bookmark.get("Named");
            link.setAction(PdfAction.gotoLocalPage(dest, false));
            column.addElement(new Paragraph(link));
        }
        column.go();
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}