Adding a cover page to an existing PDF

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

31st May 2016
admin-marketing

Switch code for this example

AddCover1.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.
  10.  * http://stackoverflow.com/questions/29563942/how-to-add-a-cover-pdf-in-a-existing-itext-document
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.utils.PdfMerger;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24. import java.io.IOException;
  25.  
  26. @Category(SampleTest.class)
  27. public class AddCover1 extends GenericTest {
  28.     public static final String COVER
  29.             = "./src/test/resources/pdfs/hero.pdf";
  30.     public static final String DEST
  31.             = "./target/test/resources/sandbox/merge/add_cover.pdf";
  32.     public static final String RESOURCE
  33.             = "./src/test/resources/pdfs/pages.pdf";
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddCover1().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void manipulatePdf(String dest) throws IOException {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         PdfMerger merger = new PdfMerger(pdfDoc);
  44.         PdfDocument cover = new PdfDocument(new PdfReader(COVER));
  45.         PdfDocument resource = new PdfDocument(new PdfReader(RESOURCE));
  46.         merger.merge(cover, 1, 1);
  47.         merger.merge(resource, 1, resource.getNumberOfPages());
  48.         cover.close();
  49.         resource.close();
  50.         pdfDoc.close();
  51.     }
  52. }
AddCover2.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.
  10.  * http://stackoverflow.com/questions/29563942/how-to-add-a-cover-pdf-in-a-existing-itext-document
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14. import com.itextpdf.forms.PdfPageFormCopier;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24. import java.io.IOException;
  25.  
  26. @Category(SampleTest.class)
  27. public class AddCover2 extends GenericTest {
  28.     public static final String DEST
  29.             = "./target/test/resources/sandbox/merge/add_cover2.pdf";
  30.     public static final String COVER
  31.             = "./src/test/resources/pdfs/hero.pdf";
  32.     public static final String RESOURCE
  33.             = "./src/test/resources/pdfs/pages.pdf";
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddCover2().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void manipulatePdf(String dest) throws IOException {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(RESOURCE), new PdfWriter(dest));
  43.         PdfDocument cover = new PdfDocument(new PdfReader(COVER));
  44.         cover.copyPagesTo(1, 1, pdfDoc, 1, new PdfPageFormCopier());
  45.         cover.close();
  46.         pdfDoc.close();
  47.     }
  48. }
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