Example usage for com.itextpdf.text.pdf PdfCopy setOutlines

List of usage examples for com.itextpdf.text.pdf PdfCopy setOutlines

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfCopy setOutlines.

Prototype

public void setOutlines(final List<HashMap<String, Object>> outlines) 

Source Link

Document

Sets the bookmarks.

Usage

From source file:be.roots.taconic.pricingguide.util.iTextUtil.java

License:Open Source License

public static byte[] merge(byte[]... pdfAsBytes) throws DocumentException, IOException {

    try (final ByteArrayOutputStream copyBaos = new ByteArrayOutputStream()) {
        final Document doc = new Document();
        final PdfCopy copy = new PdfSmartCopy(doc, copyBaos);

        doc.open();/*from  w ww. j ava 2 s  . c  o  m*/

        int numberOfPages = 0;
        final java.util.List<HashMap<String, Object>> bookmarks = new ArrayList<>();
        PdfReader pdf = null;
        for (byte[] pdfAsByte : pdfAsBytes) {

            if (pdfAsByte != null && pdfAsByte.length > 0) {
                pdf = new PdfReader(pdfAsByte);
                pdf.consolidateNamedDestinations();
                final List<HashMap<String, Object>> pdfBookmarks = SimpleBookmark.getBookmark(pdf);
                if (!CollectionUtils.isEmpty(pdfBookmarks)) {
                    SimpleBookmark.shiftPageNumbers(pdfBookmarks, numberOfPages, null);
                    bookmarks.addAll(pdfBookmarks);
                }

                for (int i = 1; i <= pdf.getNumberOfPages(); i++) {
                    copy.addPage(copy.getImportedPage(pdf, i));
                }
                numberOfPages += pdf.getNumberOfPages();
            }

        }
        if (pdf != null) {
            SimpleNamedDestination.getNamedDestination(pdf, false);
        }

        if (!CollectionUtils.isEmpty(bookmarks)) {
            copy.setOutlines(bookmarks);
        }

        copy.close();
        return copyBaos.toByteArray();
    }
}

From source file:com.github.sgelb.sldownloader.model.Pdf.java

License:Open Source License

public void mergePdfs() throws DocumentException, IOException {
    String title = book.getPdfTitle() + ".pdf";
    File saveFile = new File(saveFolder, title);

    int count = 1;
    while (saveFile.exists()) {
        title = book.getPdfTitle() + "_" + count++ + ".pdf";
        saveFile = new File(saveFolder, title);
    }//from ww w  .j  ava  2  s.c  o m
    book.setInfo("saveFile", saveFile.toString());

    Document document = new Document();
    PdfCopy destPdf = new PdfCopy(document, new FileOutputStream(saveFile));
    document.open();
    PdfReader reader;
    int page_offset = 0;
    int n;
    ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
    List<HashMap<String, Object>> tmp;

    count = 1;
    System.out.println("Start mergin\u2026");
    for (File srcPdf : src) {

        if (Thread.interrupted()) {
            return;
        }

        System.out.print(":: " + count++ + "/" + src.size());
        reader = new PdfReader(srcPdf.toString());

        tmp = SimpleBookmark.getBookmark(reader);
        if (tmp != null) {
            SimpleBookmark.shiftPageNumbers(tmp, page_offset, null);
            bookmarks.addAll(tmp);
        }

        n = reader.getNumberOfPages();
        page_offset += n;
        for (int page = 0; page < n;) {
            destPdf.addPage(destPdf.getImportedPage(reader, ++page));
        }
        destPdf.freeReader(reader);
        reader.close();
        System.out.println(" succeed.");
    }
    if (!bookmarks.isEmpty()) {
        destPdf.setOutlines(bookmarks);
    }

    if (book.getInfo("author") != null)
        document.addAuthor(book.getInfo("author"));
    if (book.getInfo("title") != null)
        document.addTitle(book.getInfo("title"));
    if (book.getInfo("subtitle") != null)
        document.addSubject(book.getInfo("subtitle"));
    document.close();

    System.out.println("Merge complete. Saved to " + saveFile);
}