Merging documents with bookmarks

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

InsertAndAdaptOutlines.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written in answer to:
  10.  * http://stackoverflow.com/questions/34381849
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfOutline;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. @Category(SampleTest.class)
  28. public class InsertAndAdaptOutlines extends GenericTest {
  29.     public static final String DEST =
  30.             "./target/test/resources/sandbox/merge/insert_and_adapt_outlines.pdf";
  31.     public static final String INSERT =
  32.             "./src/test/resources/pdfs/hello.pdf";
  33.     public static final String SRC =
  34.             "./src/test/resources/pdfs/bookmarks.pdf";
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new InsertAndAdaptOutlines().manipulatePdf(DEST);
  40.     }
  41.  
  42.     public void manipulatePdf(String dest) throws IOException {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
  44.         PdfDocument insertDoc = new PdfDocument(new PdfReader(INSERT));
  45.         insertDoc.copyPagesTo(1, 1, pdfDoc, 4);
  46.         insertDoc.close();
  47.  
  48.         PdfOutline outlines = pdfDoc.getOutlines(false);
  49.         PdfOutline outline = outlines.getAllChildren().get(0).addOutline("Hello", 3);
  50.         outline.addDestination(PdfExplicitDestination.createFit(4));
  51.         pdfDoc.close();
  52.     }
  53. }
MergeWithOutlines.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to a question on StackOverflow
  10.  *
  11.  * When concatenating documents, we add a named destination every time
  12.  * a new document is started. After we've finished merging, we add an extra
  13.  * page with the table of contents and links to the named destinations.
  14.  */
  15. package com.itextpdf.samples.sandbox.merge;
  16.  
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfOutline;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  22. import com.itextpdf.kernel.utils.PdfMerger;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29. import java.io.IOException;
  30.  
  31. @Category(SampleTest.class)
  32. public class MergeWithOutlines extends GenericTest {
  33.     public static final String DEST
  34.             = "./target/test/resources/sandbox/merge/merge_with_outlines.pdf";
  35.     public static final String SRC1
  36.             = "./src/test/resources/pdfs/hello.pdf";
  37.     public static final String SRC2
  38.             = "./src/test/resources/pdfs/links1.pdf";
  39.     public static final String SRC3
  40.             = "./src/test/resources/pdfs/links2.pdf";
  41.  
  42.     public static void main(String[] args) throws IOException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new MergeWithOutlines().manipulatePdf(DEST);
  46.     }
  47.  
  48.     public void manipulatePdf(String destFolder) throws IOException {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  50.         PdfMerger merger = new PdfMerger(pdfDoc);
  51.         PdfOutline rootOutline = pdfDoc.getOutlines(false);
  52.  
  53.         int page = 1;
  54.  
  55.         PdfDocument srcDoc1 = new PdfDocument(new PdfReader(SRC1));
  56.         merger.merge(srcDoc1, 1, srcDoc1.getNumberOfPages());
  57.  
  58.         PdfDocument srcDoc2 = new PdfDocument(new PdfReader(SRC2));
  59.         PdfDocument srcDoc3 = new PdfDocument(new PdfReader(SRC3));
  60.         merger.setCloseSourceDocuments(true)
  61.                 .merge(srcDoc2, 1, srcDoc2.getNumberOfPages())
  62.                 .merge(srcDoc3, 1, srcDoc3.getNumberOfPages());
  63.  
  64.         PdfOutline helloWorld = rootOutline.addOutline("Hello World");
  65.         helloWorld.addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(page)));
  66.         page += srcDoc1.getNumberOfPages();
  67.  
  68.         PdfOutline link1 = helloWorld.addOutline("link1");
  69.         link1.addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(page)));
  70.         page += srcDoc1.getNumberOfPages();
  71.  
  72.         PdfOutline link2 = rootOutline.addOutline("Link 2");
  73.         link2.addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(page)));
  74.  
  75.         srcDoc1.close();
  76.         pdfDoc.close();
  77.     }
  78. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now