Example usage for com.lowagie.text.pdf PdfAction gotoLocalPage

List of usage examples for com.lowagie.text.pdf PdfAction gotoLocalPage

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfAction gotoLocalPage.

Prototype

public static PdfAction gotoLocalPage(String dest, boolean isName) 

Source Link

Document

Creates a GoTo action to a named destination.

Usage

From source file:com.geek.tutorial.itext.bookmarks.Outline.java

License:Open Source License

public Outline() throws Exception {

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("outline.pdf"));
    document.open();/*from  w  w  w.ja va  2s.  c o m*/

    // Code 1
    document.add(new Chunk("Chapter 1").setLocalDestination("1"));
    document.newPage();

    document.add(new Chunk("Chapter 2").setLocalDestination("2"));
    document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));
    document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));
    document.newPage();

    document.add(new Chunk("Chapter 3").setLocalDestination("3"));

    // Code 2
    PdfContentByte cb = writer.getDirectContent();
    PdfOutline root = cb.getRootOutline();

    // Code 3
    PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1");

    PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");
    oline2.setOpen(false);
    PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "Sub 2.1");
    PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "Sub 2.2");

    PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "Chapter 3");

    document.close();
}

From source file:org.areasy.common.doclet.document.Bookmarks.java

License:Open Source License

/**
 * Creates entries for all the given bookmark entry objects.
 * If any of them has child nodes, the method calls itself
 * recursively to process them as well./*from  ww w .  jav  a  2  s . c  o m*/
 *
 * @param parent  The parent PDF outline object.
 * @param entries The bookmark entries for which to add outline objects.
 */
private static void createBookmarks(PdfOutline parent, BookmarkEntry[] entries) {
    if (entries == null)
        return;

    for (int i = 0; i < entries.length; i++) {
        String name = entries[i].getDestinationName();

        PdfAction action = null;

        if (name == null)
            action = new PdfAction();
        else
            action = PdfAction.gotoLocalPage(name, false);

        PdfOutline outline = new PdfOutline(parent, action, entries[i].getLabel());
        outline.setOpen(false);

        createBookmarks(outline, entries[i].getChildren());
    }
}

From source file:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java

License:Open Source License

/**
 * Creates a PdfAction.//w w  w  . j  av a 2 s .  co  m
 *
 * @param hyperlink
 *            the hyperlink.
 * @param bookmark
 *            the bookmark.
 * @param target
 *            if target equals "_blank", the target will be opened in a new
 *            window, else the target will be opened in the current window.
 * @return the created PdfAction.
 */
private PdfAction createPdfAction(String hyperlink, String bookmark, String target, int type) {
    // patch from Ales Novy
    if ("_top".equalsIgnoreCase(target) || "_parent".equalsIgnoreCase(target)
            || "_blank".equalsIgnoreCase(target) || "_self".equalsIgnoreCase(target))
    // Opens the target in a new window.
    {
        if (hyperlink == null)
            hyperlink = "";
        boolean isUrl = hyperlink.startsWith("http");
        if (!isUrl) {
            Matcher matcher = PAGE_LINK_PATTERN.matcher(hyperlink);
            if (matcher.find()) {
                String fileName = matcher.group(1);
                String pageNumber = matcher.group(matcher.groupCount());
                return new PdfAction(fileName, Integer.valueOf(pageNumber));
            }
        }
        return new PdfAction(hyperlink);
    } else

    // Opens the target in the current window.
    {
        if (type == IHyperlinkAction.ACTION_BOOKMARK) {
            return PdfAction.gotoLocalPage(bookmark, false);
        } else {
            return PdfAction.gotoRemotePage(hyperlink, bookmark, false, false);
        }
    }
}

From source file:org.eclipse.birt.report.engine.emitter.pdf.TOCHandler.java

License:Open Source License

/**
 * create a PDF outline for tocNode, using the pol as the parent PDF
 * outline./*www .java 2s .  com*/
 *
 * @param tocNode
 *            The tocNode whose kids need to build a PDF outline tree
 * @param pol
 *            The parent PDF outline for these kids
 * @param bookmarks
 *            All bookMarks created during rendering
 */
protected void createTOC(TOCNode tocNode, PdfOutline pol, Set<String> bookmarks) {
    if (isOutlineSizeOverflow())
        return;
    if (null == tocNode || null == tocNode.getChildren())
        return;
    for (Iterator i = tocNode.getChildren().iterator(); i.hasNext();) {
        TOCNode node = (TOCNode) i.next();
        if (!bookmarks.contains(node.getBookmark())) {
            createTOC(node, outline, bookmarks);
            continue;
        }
        PdfOutline outline = new PdfOutline(pol, PdfAction.gotoLocalPage(node.getBookmark(), false),
                node.getDisplayString());
        countOutlineSize(node.getBookmark().length());
        IScriptStyle style = node.getTOCStyle();
        String color = style.getColor();
        if (color != null) {
            color = color.toLowerCase();
        }
        Color awtColor = PropertyUtil.getColor(color);
        if (awtColor != null) {
            outline.setColor(awtColor);
        }
        String fontStyle = style.getFontStyle();
        String fontWeight = style.getFontWeight();
        int styleValue = PropertyUtil.getFontStyle(fontStyle, fontWeight);
        outline.setStyle(styleValue);
        createTOC(node, outline, bookmarks);
    }
}

From source file:questions.stamppages.BookmarksToTOC2.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from  w  w w  . j  a  v a 2 s  .  c o 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();
    }

}

From source file:questions.stamppages.ParagraphBookmarkEvents.java

/**
 * Adds an outline for every new Paragraph
 * //w w w .j  a v  a 2s. c  o m
 * @param writer
 * @param document
 * @param position
 */
public void onParagraph(PdfWriter writer, Document document, float position) {
    n++;
    PdfContentByte cb = writer.getDirectContent();
    PdfOutline root = cb.getRootOutline();
    PdfDestination destination = new PdfDestination(PdfDestination.FITH, position);
    if (named) {
        cb.localDestination("p" + n, destination);
        new PdfOutline(root, PdfAction.gotoLocalPage("p" + n, false), "paragraph " + n);
    } else {
        new PdfOutline(root, destination, "paragraph " + n);
    }
}