Adding a cover page to an existing PDF

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

11th October 2015
admin-marketing

Switch code for this example

AddCover1.java
  1. /**
  2.  * Example written by Bruno Lowagie.
  3.  * http://stackoverflow.com/questions/29563942/how-to-add-a-cover-pdf-in-a-existing-itext-document
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfCopy;
  10. import com.itextpdf.text.pdf.PdfReader;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class AddCover1 {
  19.  
  20.     /** The original PDF file. */
  21.     public static final String COVER
  22.         = "resources/pdfs/hero.pdf";
  23.     /** The original PDF file. */
  24.     public static final String SRC
  25.         = "resources/pdfs/pages.pdf";
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String DEST
  29.         = "results/merge/pages_with_cover.pdf";
  30.    
  31.     /**
  32.      * Manipulates a PDF file src with the file dest as result
  33.      * @param src the original PDF
  34.      * @param dest the resulting PDF
  35.      * @throws IOException
  36.      * @throws DocumentException
  37.      */
  38.     public void manipulatePdf(String src, String dest)
  39.         throws IOException, DocumentException {
  40.         PdfReader cover = new PdfReader(COVER);
  41.         PdfReader reader = new PdfReader(src);
  42.         Document document = new Document();
  43.         PdfCopy copy = new PdfCopy(document, new FileOutputStream(dest));
  44.         document.open();
  45.         copy.addDocument(cover);
  46.         copy.addDocument(reader);
  47.         document.close();
  48.         cover.close();
  49.         reader.close();
  50.     }
  51.  
  52.     /**
  53.      * Main method.
  54.      * @param    args    no arguments needed
  55.      * @throws DocumentException
  56.      * @throws IOException
  57.      */
  58.     public static void main(String[] args)
  59.         throws IOException, DocumentException {
  60.         File file = new File(DEST);
  61.         file.getParentFile().mkdirs();
  62.         new AddCover1().manipulatePdf(SRC, DEST);
  63.     }
  64. }
AddCover2.java
  1. /**
  2.  * Example written by Bruno Lowagie.
  3.  * http://stackoverflow.com/questions/29563942/how-to-add-a-cover-pdf-in-a-existing-itext-document
  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.PdfImportedPage;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class AddCover2 {
  20.  
  21.     /** The original PDF file. */
  22.     public static final String COVER
  23.         = "resources/pdfs/hero.pdf";
  24.     /** The original PDF file. */
  25.     public static final String SRC
  26.         = "resources/pdfs/pages.pdf";
  27.  
  28.     /** The resulting PDF file. */
  29.     public static final String DEST
  30.         = "results/merge/cover_with_pages.pdf";
  31.    
  32.     /**
  33.      * Manipulates a PDF file src with the file dest as result
  34.      * @param src the original PDF
  35.      * @param dest the resulting PDF
  36.      * @throws IOException
  37.      * @throws DocumentException
  38.      */
  39.     public void manipulatePdf(String src, String dest)
  40.         throws IOException, DocumentException {
  41.         PdfReader cover = new PdfReader(COVER);
  42.         PdfReader reader = new PdfReader(src);
  43.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  44.         stamper.insertPage(1, cover.getPageSizeWithRotation(1));
  45.         PdfContentByte page1 = stamper.getOverContent(1);
  46.         PdfImportedPage page = stamper.getImportedPage(cover, 1);
  47.         page1.addTemplate(page, 0, 0);
  48.         stamper.close();
  49.         cover.close();
  50.         reader.close();
  51.     }
  52.  
  53.     /**
  54.      * Main method.
  55.      * @param    args    no arguments needed
  56.      * @throws DocumentException
  57.      * @throws IOException
  58.      */
  59.     public static void main(String[] args)
  60.         throws IOException, DocumentException {
  61.         File file = new File(DEST);
  62.         file.getParentFile().mkdirs();
  63.         new AddCover2().manipulatePdf(SRC, DEST);
  64.     }
  65. }
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