Example usage for org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination PDPageFitWidthDestination setPage

List of usage examples for org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination PDPageFitWidthDestination setPage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination PDPageFitWidthDestination setPage.

Prototype

public void setPage(PDPage page) 

Source Link

Document

Set the page for a local destination.

Usage

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;/*ww  w.j ava 2s  . com*/
    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: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);/*  w w  w .  jav  a 2  s .c o m*/
        @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);
                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);
    }
}