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

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

Introduction

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

Prototype

public void setTitle(String title) 

Source Link

Document

Set the title for this node.

Usage

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

License:Open Source License

/**
 * Update table of contents in destination document.
 * /*from   w  ww  .  j  av  a 2s . c  om*/
 * @param document PDF document to update.
 */
private void updateTOC(PDDocument document) {
    PDDocumentOutline outline = new PDDocumentOutline();
    document.getDocumentCatalog().setDocumentOutline(outline);
    PDOutlineItem topItem = new PDOutlineItem();
    topItem.setTitle(title);
    outline.appendChild(topItem);

    try {
        PDFTextFinder finder = new PDFTextFinder(fontList);
        List<PDFTextFinder.PDFText> headings = finder.getTextList(document);
        PDOutlineItem level[] = { topItem, null, null, null };
        for (PDFTextFinder.PDFText heading : headings) {
            PDPageXYZDestination dest = new PDPageXYZDestination();
            dest.setPage(heading.page);

            PDOutlineItem bookmark = new PDOutlineItem();
            bookmark.setDestination(dest);
            bookmark.setTitle(heading.text);
            level[heading.tag - 1].appendChild(bookmark);
            level[heading.tag] = bookmark;
        }
    } catch (IOException e) {
        System.out.println("Error :" + e);
    }

    topItem.openNode();
    outline.openNode();
}

From source file:com.jt.tool.pdf.CreateBookmarks.java

License:Apache License

public static void createBookmark(String srcFile, String targetFile, String reg) throws Exception {
    PDDocument document = null;/*from   www. java2  s.c  o m*/
    try {
        document = PDDocument.load(new File(srcFile));
        if (document.isEncrypted()) {
            System.err.println("Error: Cannot add bookmarks to encrypted document.");
            System.exit(1);
        }
        PDDocumentOutline outline = new PDDocumentOutline();
        document.getDocumentCatalog().setDocumentOutline(outline);
        PDOutlineItem pagesOutline = new PDOutlineItem();
        pagesOutline.setTitle("All Pages");
        //            outline.appendChild(pagesOutline);
        List pages = new ArrayList();
        //                    document.getDocumentCatalog().getAllPages();
        for (int i = 12; i < pages.size(); i++) {
            String pageText = getPageText(document, i + 1, 0);
            String[] strings = matchTitle(pageText, reg);
            if (makeBookmark(strings)) {
                PDPage page = (PDPage) pages.get(i);
                PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
                dest.setPage(page);
                PDOutlineItem bookmark = new PDOutlineItem();
                bookmark.setDestination(dest);
                bookmark.setTitle(strings[0]);
                //                    pagesOutline.appendChild(bookmark);
                System.out.println("add " + strings[0]);
            }
        }
        pagesOutline.openNode();
        outline.openNode();
        document.save(targetFile);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

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

License:Apache License

public static PDOutlineItem createOutline(String title, int pageNumber, int yOffset, String zoom,
        ZoomType zoomType) {/*from  ww  w .j a  v  a 2 s. com*/
    PDOutlineItem bookmark = new PDOutlineItem();
    bookmark.setTitle(title);

    PDPageDestination dest = null;
    if (zoom != null) {
        if (zoomType == ZoomType.Inherit || zoomType == ZoomType.ZoomPercent) {
            PDPageXYZDestination xyz = new PDPageXYZDestination();
            xyz.setTop(yOffset);
            dest = xyz;

            if (zoomType == ZoomType.Inherit) {
                xyz.setZoom(-1);
            } else {
                String zoomNoPcnt = zoom.substring(0, zoom.length() - 1).trim();
                float zoomf = Integer.parseInt(zoomNoPcnt) / 100.0f;
                xyz.setZoom(zoomf);
            }
        } else if (zoomType == ZoomType.FitPage) {
            dest = new PDPageFitDestination();
        } else if (zoomType == ZoomType.FitHeight) {
            dest = new PDPageFitHeightDestination();
        }
        // Otherwise fall through to the default, FitWidth
    }
    if (dest == null) {
        PDPageFitWidthDestination wdest = new PDPageFitWidthDestination();
        wdest.setTop(yOffset);
        dest = wdest;
    }

    dest.setPageNumber(pageNumber - 1);
    bookmark.setDestination(dest);
    return bookmark;
}

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   w w w  .j  ava2  s  .c  om*/

        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:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java

License:Open Source License

@Override
public void addBookmarks(final String inputUri, final String outputUri, final String title,
        final List<PDFGalBookmark> pdfGalBookmarksList) throws IOException, COSVisitorException {

    if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotEmpty(title)
            && CollectionUtils.isNotEmpty(pdfGalBookmarksList)) {

        final PDDocument doc = PDDocument.load(inputUri);

        final PDDocumentOutline outline = new PDDocumentOutline();
        doc.getDocumentCatalog().setDocumentOutline(outline);
        final PDOutlineItem pagesOutline = new PDOutlineItem();
        pagesOutline.setTitle(title);
        @SuppressWarnings("unchecked")
        final List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
        outline.appendChild(pagesOutline);

        for (final PDFGalBookmark pdfGalBookmark : pdfGalBookmarksList) {
            if (pdfGalBookmark != null && pdfGalBookmark.isInitializated()) {
                final PDPage page = pages.get(pdfGalBookmark.getPage() - 1);
                final PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
                dest.setPage(page);//from w ww .jav  a2 s .  c  om
                final PDOutlineItem bookmark = new PDOutlineItem();
                bookmark.setDestination(dest);
                bookmark.setTitle(pdfGalBookmark.getText());
                pagesOutline.appendChild(bookmark);
            }
        }
        pagesOutline.openNode();
        outline.openNode();

        doc.save(outputUri);
        doc.close();

    } else {
        throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE);
    }
}

From source file:org.pdfmetamodifier.OutlineHelper.java

License:Apache License

private static PDOutlineItem createOutlineItem(final String title) {
    final PDOutlineItem outlineItem = new PDOutlineItem();
    outlineItem.setTitle(title);
    outlineItem.closeNode();//from ww  w .j  ava2 s .c  o m
    return outlineItem;
}

From source file:org.pdfsam.pdfbox.component.PDFBoxOutlineUtils.java

License:Open Source License

/**
 * Copies the dictionary from the given {@link PDOutlineItem} to the destination one
 * /*from  w  w  w  . j a  v  a 2s . c  o  m*/
 * @param from
 * @param to
 */
public static void copyOutlineDictionary(PDOutlineItem from, PDOutlineItem to) {
    to.setTitle(from.getTitle());
    to.setTextColor(from.getTextColor());
    to.setBold(from.isBold());
    to.setItalic(from.isItalic());
    if (from.isNodeOpen()) {
        to.openNode();
    } else {
        to.closeNode();
    }
}

From source file:org.pdfsam.pdfbox.component.PDFBoxOutlineUtilsTest.java

License:Open Source License

@Test
public void copyDictionary() {
    PDOutlineItem from = new PDOutlineItem();
    from.setBold(true);/*from  ww w .j a  v  a2s.  c  o m*/
    from.setItalic(true);
    from.setTitle("Chuck");
    PDOutlineItem to = new PDOutlineItem();
    to.setBold(false);
    to.setItalic(false);
    PDFBoxOutlineUtils.copyOutlineDictionary(from, to);
    assertTrue(to.isBold());
    assertTrue(to.isItalic());
    assertEquals("Chuck", to.getTitle());
}

From source file:uia.pdf.PDFMaker.java

License:Apache License

/**
 * Constructor.//  w w w .  j  av a  2s .  c  o  m
 * @param font Font.
 * @throws IOException
 */
public PDFMaker(PDFont font) throws IOException {
    this.temp = new ArrayList<PDOutlineItem>();
    this.doc = new PDDocument();
    this.font = font;
    this.factory = new ValueParserFactory();

    this.docOutline = new PDDocumentOutline();
    this.doc.getDocumentCatalog().setDocumentOutline(this.docOutline);

    PDOutlineItem rootOI = new PDOutlineItem();
    rootOI.setTitle("All");
    this.docOutline.addLast(rootOI);

    this.hierarchyOI = new ArrayDeque<PDOutlineItem>();
    this.hierarchyOI.push(rootOI);

    this.bookmarkPages = new ArrayList<BookmarkPage>();
}

From source file:uia.pdf.PDFMaker.java

License:Apache License

/**
 * Constructor./*ww w . ja v a 2 s . c  o  m*/
 * @param fontFile TTF font file.
 * @throws IOException
 */
public PDFMaker(File fontFile) throws IOException {
    this.temp = new ArrayList<PDOutlineItem>();
    this.doc = new PDDocument();
    this.font = PDType0Font.load(this.doc, fontFile);
    this.factory = new ValueParserFactory();

    this.docOutline = new PDDocumentOutline();
    this.doc.getDocumentCatalog().setDocumentOutline(this.docOutline);

    PDOutlineItem rootOI = new PDOutlineItem();
    rootOI.setTitle("All");
    this.docOutline.addLast(rootOI);

    this.hierarchyOI = new ArrayDeque<PDOutlineItem>();
    this.hierarchyOI.push(rootOI);

    this.bookmarkPages = new ArrayList<BookmarkPage>();
}