Create booklet from A4 document

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

19th December 2015
admin-marketing

Switch code for this example

MakeA3Booklet.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow:
  3.  * http://stackoverflow.com/questions/34019298
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfImportedPage;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  * @author Bruno Lowagie (iText Software)
  21.  */
  22. @WrapToTest
  23. public class MakeA3Booklet {
  24.  
  25.     /** The original PDF file. */
  26.     public static final String SRC = "resources/pdfs/primes.pdf";
  27.  
  28.     /** The resulting PDF file. */
  29.     public static final String DEST = "results/merge/a3_booklet.pdf";
  30.    
  31.     /**
  32.      * Main method.
  33.      * @param    args    no arguments needed
  34.      * @throws DocumentException
  35.      * @throws IOException
  36.      */
  37.     public static void main(String[] args)
  38.         throws IOException, DocumentException {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new MakeA3Booklet().manipulatePdf(SRC, DEST);
  42.     }
  43.    
  44.     /**
  45.      * Manipulates a PDF file src with the file dest as result
  46.      * @param src the original PDF
  47.      * @param dest the resulting PDF
  48.      * @throws IOException
  49.      * @throws DocumentException
  50.      */
  51.     public void manipulatePdf(String src, String dest)
  52.         throws IOException, DocumentException {
  53.         // Creating a reader
  54.         PdfReader reader = new PdfReader(src);
  55.         // step 1
  56.         Document document = new Document(PageSize.A3.rotate());
  57.         // step 2
  58.         PdfWriter writer
  59.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  60.         // step 3
  61.         document.open();
  62.         // step 4
  63.         PdfContentByte canvas = writer.getDirectContent();
  64.         float a4_width = PageSize.A4.getWidth();
  65.         int n = reader.getNumberOfPages();
  66.         int p = 0;
  67.         PdfImportedPage page;
  68.         while (p++ < n) {
  69.             page = writer.getImportedPage(reader, p);
  70.             if (p % 2 == 1) {
  71.                 canvas.addTemplate(page, 0, 0);
  72.             }
  73.             else {
  74.                 canvas.addTemplate(page, a4_width, 0);
  75.                 document.newPage();
  76.             }
  77.         }
  78.         // step 5
  79.         document.close();
  80.         reader.close();
  81.     }
  82.    
  83. }
MakeBooklet.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow:
  3.  * http://stackoverflow.com/questions/34432502
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.awt.geom.AffineTransform;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.PageSize;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfImportedPage;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  * @author Bruno Lowagie (iText Software)
  23.  */
  24. @WrapToTest
  25. public class MakeBooklet {
  26.  
  27.     /** The original PDF file. */
  28.     public static final String SRC = "resources/pdfs/primes.pdf";
  29.  
  30.     /** The resulting PDF file. */
  31.     public static final String DEST = "results/merge/book16.pdf";
  32.    
  33.     /**
  34.      * Main method.
  35.      * @param    args    no arguments needed
  36.      * @throws DocumentException
  37.      * @throws IOException
  38.      */
  39.     public static void main(String[] args)
  40.         throws IOException, DocumentException {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new MakeBooklet().manipulatePdf(SRC, DEST);
  44.     }
  45.    
  46.     /**
  47.      * Manipulates a PDF file src with the file dest as result
  48.      * @param src the original PDF
  49.      * @param dest the resulting PDF
  50.      * @throws IOException
  51.      * @throws DocumentException
  52.      */
  53.     public void manipulatePdf(String src, String dest)
  54.         throws IOException, DocumentException {
  55.         // Creating a reader
  56.         PdfReader reader = new PdfReader(src);
  57.         // step 1
  58.         Rectangle pagesize = new Rectangle(
  59.             PageSize.A4.getWidth() * 4,
  60.             PageSize.A4.getHeight() * 2);
  61.         Document document = new Document(pagesize);
  62.         // step 2
  63.         PdfWriter writer
  64.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  65.         // step 3
  66.         document.open();
  67.         // step 4
  68.         PdfContentByte canvas = writer.getDirectContent();
  69.         float a4_width = PageSize.A4.getWidth();
  70.         float a4_height = PageSize.A4.getHeight();
  71.         int n = reader.getNumberOfPages();
  72.         int p = 1;
  73.         PdfImportedPage page;
  74.         while ((p - 1) / 16  reader.getNumberOfPages()) return;
  75.         PdfImportedPage page = canvas.getPdfWriter().getImportedPage(reader, p);
  76.         canvas.addTemplate(page, x, 0);
  77.     }
  78. }
MakeBookletA3.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow:
  3.  * http://stackoverflow.com/questions/34432502
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfImportedPage;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class MakeBookletA3 {
  25.  
  26.     /** The original PDF file. */
  27.     public static final String SRC = "resources/pdfs/primes.pdf";
  28.  
  29.     /** The resulting PDF file. */
  30.     public static final String DEST = "results/merge/book4.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 MakeBookletA3().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.         // Creating a reader
  55.         PdfReader reader = new PdfReader(src);
  56.         // step 1
  57.         Rectangle pagesize = new Rectangle(
  58.             PageSize.A4.getWidth() * 2,
  59.             PageSize.A4.getHeight());
  60.         Document document = new Document(pagesize);
  61.         // step 2
  62.         PdfWriter writer
  63.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  64.         // step 3
  65.         document.open();
  66.         // step 4
  67.         PdfContentByte canvas = writer.getDirectContent();
  68.         float a4_width = PageSize.A4.getWidth();
  69.         int n = reader.getNumberOfPages();
  70.         int p = 1;
  71.         PdfImportedPage page;
  72.         while ((p - 1) / 4  reader.getNumberOfPages()) return;
  73.         PdfImportedPage page = canvas.getPdfWriter().getImportedPage(reader, p);
  74.         canvas.addTemplate(page, x, 0);
  75.     }
  76. }
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