Merging documents with bookmarks

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

11th October 2015
admin-marketing

Switch code for this example

MergeWithOutlines.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow
  3.  *
  4.  * When concatenating documents, we add a named destination every time
  5.  * a new document is started. After we've finished merging, we add an extra
  6.  * page with the table of contents and links to the named destinations.
  7.  */
  8. package sandbox.merge;
  9.  
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.pdf.PdfCopy;
  13. import com.itextpdf.text.pdf.PdfReader;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class MergeWithOutlines {
  25.  
  26.     public static final String SRC1 = "resources/pdfs/hello.pdf";
  27.     public static final String SRC2 = "resources/pdfs/links1.pdf";
  28.     public static final String SRC3 = "resources/pdfs/links2.pdf";
  29.     public static final String DEST = "results/merge/merge_with_outlines.pdf";
  30.    
  31.     public Map filesToMerge;
  32.    
  33.     public static void main(String[] args) throws IOException, DocumentException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new MergeWithOutlines().createPdf(DEST);
  37.     }
  38.    
  39.    
  40.     public void createPdf(String filename) throws IOException, DocumentException {
  41.         Document document = new Document();
  42.         PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
  43.         document.open();
  44.         int page = 1;
  45.         ArrayList outlines = new ArrayList();
  46.         // add the first document
  47.         PdfReader reader1 = new PdfReader(SRC1);
  48.         copy.addDocument(reader1);
  49.         // add the first outline element
  50.         HashMap helloworld = new HashMap();
  51.         helloworld.put("Title", "Hello World");
  52.         helloworld.put("Action", "GoTo");
  53.         helloworld.put("Page", String.format("%d Fit", page));
  54.         outlines.add(helloworld);
  55.         // update page count
  56.         page += reader1.getNumberOfPages();
  57.         // add the second document
  58.         PdfReader reader2 = new PdfReader(SRC2);
  59.         copy.addDocument(reader2);
  60.         // add the second outline element as a kid of the first one
  61.         ArrayList kids = new ArrayList();
  62.         HashMap link1 = new HashMap();
  63.         link1.put("Title", "link1");
  64.         link1.put("Action", "GoTo");
  65.         link1.put("Page", String.format("%d Fit", page));
  66.         kids.add(link1);
  67.         helloworld.put("Kids", kids);
  68.         // update page count
  69.         page += reader2.getNumberOfPages();
  70.         // add the third document
  71.         PdfReader reader3 = new PdfReader(SRC3);
  72.         copy.addDocument(reader3);
  73.         // add the third outline element to the root
  74.         HashMap link2 = new HashMap();
  75.         link2.put("Title", "Link 2");
  76.         link2.put("Action", "GoTo");
  77.         link2.put("Page", String.format("%d Fit", page));
  78.         outlines.add(link2);
  79.         // add the outlines
  80.         copy.setOutlines(outlines);
  81.         // close the document
  82.         document.close();
  83.         reader1.close();
  84.         reader2.close();
  85.         reader3.close();
  86.     }
  87. }
InsertAndAdaptOutlines.java
  1. /*
  2.  * Example written in answer to:
  3.  * http://stackoverflow.com/questions/34381849
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfContentByte;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11. import com.itextpdf.text.pdf.SimpleBookmark;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class InsertAndAdaptOutlines {
  21.  
  22.     /** The original PDF file. */
  23.     public static final String INSERT
  24.         = "resources/pdfs/hello.pdf";
  25.     /** The original PDF file. */
  26.     public static final String SRC
  27.         = "resources/pdfs/bookmarks.pdf";
  28.     /** The resulting PDF file. */
  29.     public static final String DEST
  30.         = "results/merge/bookmarks_hello.pdf";
  31.    
  32.     /**
  33.      * Main method.
  34.      * @param    args    no arguments needed
  35.      * @throws DocumentException
  36.      * @throws IOException
  37.      */
  38.     public static void main(String[] args)
  39.         throws IOException, DocumentException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new InsertAndAdaptOutlines().manipulatePdf(SRC, DEST);
  43.     }
  44.    
  45.     /**
  46.      * Manipulates a PDF file src with the file dest as result
  47.      * @param src the original PDF
  48.      * @param dest the resulting PDF
  49.      * @throws IOException
  50.      * @throws DocumentException
  51.      */
  52.     public void manipulatePdf(String src, String dest)
  53.         throws IOException, DocumentException {
  54.         PdfReader insert = new PdfReader(INSERT);
  55.         PdfReader reader = new PdfReader(src);
  56.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST));
  57.         stamper.insertPage(4, insert.getPageSize(1));
  58.         PdfContentByte cb = stamper.getOverContent(4);
  59.         cb.addTemplate(stamper.getImportedPage(insert, 1), 0, 0);
  60.         List outlines = SimpleBookmark.getBookmark(reader);
  61.         HashMap entry = new HashMap();
  62.         entry.put("Title", "Hello");
  63.         entry.put("Action", "GoTo");
  64.         entry.put("Page", "4 Fit");
  65.         updateOutline(outlines, entry, 4);
  66.         stamper.setOutlines(outlines);
  67.         stamper.close();
  68.     }
  69.    
  70.     public boolean updateOutline(List outlines, HashMap entry, int p) {
  71.         int index = 0;
  72.         for (HashMap outline : outlines) {
  73.             Object kids = outline.get("Kids");
  74.             if (kids != null) {
  75.                 updateOutline((List)kids, entry, p);
  76.             }
  77.             else {
  78.                 if (p < getPage(outline)) {
  79.                     outlines.add(index, entry);
  80.                     return true;
  81.                 }
  82.                 index++;
  83.             }
  84.         }
  85.         return false;
  86.     }
  87.    
  88.     public int getPage(HashMap outline) {
  89.         Object page = outline.get("Page");
  90.         if (page == null) return -1;
  91.         String p = page.toString().substring(0, page.toString().indexOf(" "));
  92.         return Integer.parseInt(p);
  93.     }
  94. }
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