Example usage for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDOutlineItem getTitle

List of usage examples for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDOutlineItem getTitle

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDOutlineItem getTitle.

Prototype

public String getTitle() 

Source Link

Document

Get the title of this node.

Usage

From source file:com.github.joemcintyre.pdffinish.PDFFinish.java

License:Open Source License

/**
 * Show TOC entries at current hierarchy level, and sub-levels using recursive
 * call.//from  w ww. j  a  v  a 2 s  .  c  o m
 * 
 * @param entry Starting node.
 * @param indent Spaces to precede output text.
 */
private static void showEntry(PDOutlineNode entry, String spaces) {
    PDOutlineItem node = entry.getFirstChild();
    while (node != null) {
        System.out.println(spaces + node.getTitle());
        showEntry(node, spaces + "  ");
        node = node.getNextSibling();
    }
}

From source file:com.quanticate.opensource.pdftkbox.PDFBookmark.java

License:Apache License

/**
 * Creates our Bookmark Wrapper from the outline item.
 * Handling Children (and tracking of levels) is up to
 *  the calling class to manage/*  w  w w .  j a  v  a 2 s . c om*/
 */
public PDFBookmark(PDOutlineItem current, int level) throws IOException {
    this.title = current.getTitle();
    this.outlineItem = current;
    this.level = level;

    // Set defaults
    this.pageNumber = -1;
    this.yOffset = 0;

    // Find where the bookmark points to and record
    PDDestination dest = null;

    // Check for a bookmark via an action
    if (current.getAction() != null) {
        PDAction action = current.getAction();
        if (action instanceof PDActionGoTo) {
            dest = ((PDActionGoTo) action).getDestination();
        }
    }
    if (dest == null) {
        dest = current.getDestination();
    }

    if (dest != null) {
        if (dest instanceof PDPageDestination) {
            PDPageDestination pdest = (PDPageDestination) dest;
            int pageNum = pdest.retrievePageNumber();
            if (pageNum != -1) {
                this.pageNumber = pageNum + 1;
            }
        }

        if (dest instanceof PDPageXYZDestination) {
            PDPageXYZDestination xyz = (PDPageXYZDestination) dest;
            yOffset = xyz.getTop();

            if (xyz.getZoom() > 0) {
                zoomType = ZoomType.ZoomPercent;
                zoom = Integer.toString((int) (xyz.getZoom() * 100)) + "%";
            } else {
                zoomType = ZoomType.Inherit;
            }
        } else if (dest instanceof PDPageFitWidthDestination) {
            PDPageFitWidthDestination width = (PDPageFitWidthDestination) dest;
            yOffset = width.getTop();

            zoomType = ZoomType.FitWidth;
        } else if (dest instanceof PDPageFitDestination) {
            zoomType = ZoomType.FitPage;
        } else if (dest instanceof PDPageFitHeightDestination) {
            zoomType = ZoomType.FitHeight;
        } else {
            System.err.println("TODO: Support destination of type " + dest);
        }

        // Set a zoom description from the type if needed
        if (zoomType != null && zoom == null) {
            zoom = zoomType.name();
        }
    } else {
        System.err.println(
                "Warning - Non-destination bookmark " + current + " with action " + current.getAction());
    }
}

From source file:com.vns.pdf.impl.PdfDocument.java

License:Apache License

public void fillBookmark(PDOutlineNode bookmark, String indentation) throws IOException {
    PDOutlineItem current = bookmark.getFirstChild();
    while (current != null) {
        ActionData actionData = parsePDAction(current.getAction());
        if (actionData == null) {
            actionData = parsePDDestination(current.getDestination());
        }//from   w w w .  j  a v a2  s .  c om
        Annotation annotation;
        if (actionData != null) {
            annotation = new Annotation(-1, -1, -1, -1, actionData.destX, actionData.destY, actionData.destPage,
                    actionData.destZoom, indentation + current.getTitle());
        } else {
            annotation = new Annotation(indentation + current.getTitle());
        }
        this.doc.getBookmarks().add(annotation);
        fillBookmark(current, indentation + "    ");
        current = current.getNextSibling();
    }
}

From source file:de.berber.kindle.annotator.lib.Bookmark.java

License:Apache License

@Override
protected PDAnnotation toPDAnnotation(final @Nonnull PDDocumentOutline documentOutline,
        final @Nonnull PDPage page) {
    LOG.info("Creating bookmark");

    final String OUTLINE_ENTRY_NAME = "Bookmarks";

    // search for an outline entry called Bookmarks
    PDOutlineItem bookmarks = documentOutline.getFirstChild();

    while (bookmarks != null) {
        if (OUTLINE_ENTRY_NAME.equals(bookmarks.getTitle())) {
            break;
        }//from  ww w.j  ava2s .c o  m

        bookmarks = bookmarks.getNextSibling();
    }

    // if we did not found an entry we have to add a new one
    if (bookmarks == null) {
        bookmarks = new PDOutlineItem();
        bookmarks.setTitle(OUTLINE_ENTRY_NAME);
        documentOutline.appendChild(bookmarks);
    }

    // crate the bookmark entry
    final PDOutlineItem bookmark = new PDOutlineItem();
    bookmark.setTitle("Bookmark on page " + getPage());
    bookmark.setDestination(page);
    bookmarks.appendChild(bookmark);

    return null;
}

From source file:de.offis.health.icardea.cied.pdf.extractor.PDFApachePDFBoxExtractor.java

License:Apache License

/**
 * This method will populate the text bookmark list.
 * //from   w w w  .  ja v  a 2  s . c  om
 * @param pdOutlineNode The node element for the bookmark item.
 * @param indentionString The base indention string to be used.
 */
@SuppressWarnings("unchecked")
private void populateBookmarkTextList(PDOutlineNode pdOutlineNode, String indentionString) {
    PDOutlineItem currentOutlineItem = pdOutlineNode.getFirstChild();
    while (currentOutlineItem != null) {
        bookmarkTextList.add(indentionString + currentOutlineItem.getTitle());
        logger.trace(indentionString + currentOutlineItem.getTitle());

        /*
         * Recursive call to fill List
         */
        populateBookmarkTextList(currentOutlineItem, indentionString + bookmarkIndentionString());

        /*
         * Get next outline item
         */
        currentOutlineItem = currentOutlineItem.getNextSibling();
    } // end while
}

From source file:de.oio.jpdfunit.document.pdflibimpl.PdfBoxAnalyser.java

License:Open Source License

private void rekursionBookmarks(PDOutlineItem bla) {
    while (bla != null) {
        bookMarkList.add(bla.getTitle());
        final PDOutlineItem child = bla.getFirstChild();
        rekursionBookmarks(child);//  www . j  a va  2 s.co  m
        bla = bla.getNextSibling();
    }
}

From source file:fr.aviz.hybridvis.utils.PDF.MultiScalePDFViewer.java

License:Open Source License

/**
 * @param PDDocument//  ww  w .j a v a  2 s  .  c  o m
 *            Extracts the PDF's structure (up to 2 levels) if available
 *            (PDF passed as PDDocument). Create an image of the structure
 *            for each of the 1st level titles
 */
protected void pdfGetStructure_PDFbox(PDDocument document) throws IOException {

    PDDocumentOutline root = document.getDocumentCatalog().getDocumentOutline();
    PDOutlineItem item;

    try {
        item = root.getFirstChild();
    } catch (NullPointerException e) {
        System.out.println("No structure for pdf " + PDFname);
        return;
    }

    // fill map with titles per page
    while (item != null) {
        int pageNumber = findPageNumber(document, item.findDestinationPage(document));

        String conc = item.getTitle();
        if (conc.length() > 13)
            conc = conc.subSequence(0, 10) + "...";

        System.out.println("Item:" + conc + " at page " + pageNumber);

        if (pageTitles.containsKey(item.findDestinationPage(document)))

            pageTitles.get(item.findDestinationPage(document)).add(conc);
        else {
            pageTitles.put(item.findDestinationPage(document), new ArrayList<String>());
            pageTitles.get(item.findDestinationPage(document)).add(conc);
        }

        // do nothing with 2nd level children
        PDOutlineItem child = item.getFirstChild();
        while (child != null) {
            System.out.println("    Child:" + child.getTitle());
            child = child.getNextSibling();
        }
        item = item.getNextSibling();
    }

    int pn = 0;
    if (!hybrid) {
        BufferedImage itemImage;
        for (PDPage key : pages) {
            // for (PDPage key : pageTitles.keySet()) {
            ++pn;

            int titlesInPage = 0;
            List<String> titles = null;
            if (pageTitles.containsKey(key)) {
                titles = pageTitles.get(key);
                titlesInPage = titles.size();
            }

            int w = (int) key.getArtBox().getWidth() - 200;
            int h = (int) key.getArtBox().getHeight() - 100;
            ;
            int x = (int) key.getArtBox().getLowerLeftX() + 50;
            int y = (int) key.getArtBox().getLowerLeftY() + 50;

            // calling createGraphics() to get the Graphics2D and setup for
            // drawing titles
            itemImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

            if (titles_on && clouds_on) {
                itemImage = printTitlesAndCloudInImage(key, pn, titles, itemFont);
            } else if (titles_on) {
                if (titles != null)
                    itemImage = printTitlesInImage(key, titles, itemFont);
            } else if (clouds_on) {
                itemImage = printCloudInImage(key, pn, titles, itemFont);
            }
        }

    }

}

From source file:mj.ocraptor.extraction.tika.parser.pdf.PDF2XHTML.java

License:Apache License

void extractBookmarkText(PDOutlineNode bookmark) throws SAXException {
    PDOutlineItem current = bookmark.getFirstChild();
    if (current != null) {
        handler.startElement("ul");
        while (current != null) {
            handler.startElement("li");
            handler.characters(current.getTitle());
            handler.endElement("li");
            // Recurse:
            extractBookmarkText(current);
            current = current.getNextSibling();
        }/* www  . j a  va2  s .c  o m*/
        handler.endElement("ul");
    }
}

From source file:org.apache.tika.parser.pdf.AbstractPDF2XHTML.java

License:Apache License

void extractBookmarkText(PDOutlineNode bookmark) throws SAXException, IOException, TikaException {
    PDOutlineItem current = bookmark.getFirstChild();

    if (current != null) {
        xhtml.startElement("ul");
        while (current != null) {
            xhtml.startElement("li");
            xhtml.characters(current.getTitle());
            xhtml.endElement("li");
            handleDestinationOrAction(current.getAction(), ActionTrigger.BOOKMARK);
            // Recurse:
            extractBookmarkText(current);
            current = current.getNextSibling();
        }/*from  w  w  w.j  a  va  2 s. c  o m*/
        xhtml.endElement("ul");
    }
}

From source file:org.pdfmetamodifier.OutlineHelper.java

License:Apache License

private static String outlineToLine(final PDOutlineItem outlineItem, final PDPageTree pages,
        final PDDestinationNameTreeNode destinations, final int shift) throws IOException {
    // Shift./*  ww  w  .  j  a  v  a 2  s .co m*/
    final StringBuilder bm = new StringBuilder();
    for (int i = 0; i < shift; ++i) {
        bm.append(SHIFT);
    }

    // Title.
    final String title = outlineItem.getTitle();

    // Page number.
    final Integer pageNumber = getOutlinePageNumber(outlineItem, pages, destinations);

    // Convert Outline (bookmark) to line.
    if (pageNumber == null) {
        return String.format(OUTLINE_TEMPLATE_WITHOUT_PAGE_NUMBER, bm.toString(), title);
    } else {
        return String.format(OUTLINE_TEMPLATE_WITH_PAGE_NUMBER, bm.toString(), title, pageNumber);
    }
}