Merging documents and create a Table of Contents

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

31st May 2016
iText PDF

Switch code for this example

MergeWithToc.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:
  10.  * http://stackoverflow.com/questions/21548552/create-index-filetoc-for-merged-pdf-using-itext-library-in-java
  11.  *
  12.  * When concatenating documents, we add a named destination every time
  13.  * a new document is started. After we've finished merging, we add an extra
  14.  * page with the table of contents and links to the named destinations.
  15.  */
  16. package com.itextpdf.samples.sandbox.merge;
  17.  
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.action.PdfAction;
  22. import com.itextpdf.kernel.pdf.canvas.draw.DashedLine;
  23. import com.itextpdf.layout.Document;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.element.Tab;
  26. import com.itextpdf.layout.element.TabStop;
  27. import com.itextpdf.layout.element.Text;
  28. import com.itextpdf.layout.property.TabAlignment;
  29. import com.itextpdf.samples.GenericTest;
  30. import com.itextpdf.test.annotations.type.SampleTest;
  31.  
  32. import org.junit.experimental.categories.Category;
  33.  
  34. import java.io.File;
  35. import java.io.IOException;
  36. import java.util.Map;
  37. import java.util.TreeMap;
  38.  
  39. @Category(SampleTest.class)
  40. public class MergeWithToc extends GenericTest {
  41.     public static final String DEST = "./target/test/resources/sandbox/merge/merge_with_toc.pdf";
  42.     public static final String SRC1 = "./src/test/resources/pdfs/united_states.pdf";
  43.     public static final String SRC2 = "./src/test/resources/pdfs/hello.pdf";
  44.     public static final String SRC3 = "./src/test/resources/pdfs/toc.pdf";
  45.  
  46.     public Map filesToMerge;
  47.  
  48.     public static void main(String[] args) throws IOException {
  49.         File file = new File(DEST);
  50.         file.getParentFile().mkdirs();
  51.         new MergeWithToc().manipulatePdf(DEST);
  52.     }
  53.  
  54.     public MergeWithToc() throws IOException {
  55.         filesToMerge = new TreeMap();
  56.         filesToMerge.put("01 Hello World", new PdfDocument(new PdfReader(SRC1)));
  57.         filesToMerge.put("02 Movies / Countries", new PdfDocument(new PdfReader(SRC2)));
  58.     }
  59.  
  60.     @Override
  61.     protected void manipulatePdf(String dest) throws IOException {
  62.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  63.         pdfDoc.initializeOutlines();
  64.         Document doc = new Document(pdfDoc);
  65.  
  66.         Map toc = new TreeMap();
  67.         int n;
  68.         int pageNo = 0;
  69.         for (Map.Entry entry : filesToMerge.entrySet()) {
  70.             n = entry.getValue().getNumberOfPages();
  71.             toc.put(pageNo + 1, entry.getKey());
  72.             for (int i = 1; i
MergeWithToc2.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:
  10.  * http://stackoverflow.com/questions/21548552/create-index-filetoc-for-merged-pdf-using-itext-library-in-java
  11.  *
  12.  * When concatenating documents, we add a named destination every time
  13.  * a new document is started. After we've finished merging, we add an extra
  14.  * page with the table of contents and links to the named destinations.
  15.  */
  16.  
  17. package com.itextpdf.samples.sandbox.merge;
  18.  
  19. import com.itextpdf.io.source.RandomAccessSourceFactory;
  20. import com.itextpdf.kernel.pdf.*;
  21. import com.itextpdf.kernel.pdf.action.PdfAction;
  22. import com.itextpdf.kernel.pdf.canvas.draw.DashedLine;
  23. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  24. import com.itextpdf.layout.Document;
  25. import com.itextpdf.layout.element.Paragraph;
  26. import com.itextpdf.layout.element.Tab;
  27. import com.itextpdf.layout.element.TabStop;
  28. import com.itextpdf.layout.element.Text;
  29. import com.itextpdf.layout.property.TabAlignment;
  30. import com.itextpdf.samples.GenericTest;
  31. import com.itextpdf.test.annotations.type.SampleTest;
  32.  
  33. import org.junit.experimental.categories.Category;
  34.  
  35. import java.io.ByteArrayOutputStream;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import java.util.Map;
  39. import java.util.TreeMap;
  40.  
  41. @Category(SampleTest.class)
  42. public class MergeWithToc2 extends GenericTest {
  43.     public static final String DEST = "./target/test/resources/sandbox/merge/merge_with_toc2.pdf";
  44.     public static final String SRC1 = "./src/test/resources/pdfs/united_states.pdf";
  45.     public static final String SRC2 = "./src/test/resources/pdfs/hello.pdf";
  46.     public static final String SRC3 = "./src/test/resources/pdfs/toc.pdf";
  47.  
  48.     public Map filesToMerge;
  49.  
  50.     public static void main(String[] args) throws IOException {
  51.         File file = new File(DEST);
  52.         file.getParentFile().mkdirs();
  53.         new MergeWithToc().manipulatePdf(DEST);
  54.     }
  55.  
  56.     public MergeWithToc2() throws IOException {
  57.         filesToMerge = new TreeMap();
  58.         filesToMerge.put("01 Hello World", new PdfDocument(new PdfReader(SRC1)));
  59.         filesToMerge.put("02 Movies / Countries", new PdfDocument(new PdfReader(SRC2)));
  60.     }
  61.  
  62.     @Override
  63.     protected void manipulatePdf(String dest) throws IOException {
  64.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  65.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
  66.         Document doc = new Document(pdfDoc);
  67.         PdfOutline rootOutLine = pdfDoc.getOutlines(false);
  68.  
  69.         Map toc = new TreeMap();
  70.         int n;
  71.         int pageNo = 0;
  72.         for (Map.Entry entry : filesToMerge.entrySet()) {
  73.             n = entry.getValue().getNumberOfPages();
  74.             toc.put(pageNo + 1, entry.getKey());
  75.             for (int i = 1; i
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