Create booklet from A4 document

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

31st May 2016
admin-marketing

Switch code for this example

MakeA3Booklet.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 a question on StackOverflow:
  10.  * http://stackoverflow.com/questions/34019298
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14.  
  15. import com.itextpdf.kernel.geom.PageSize;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class MakeA3Booklet extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/merge/make_a3_booklet.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  33.  
  34.     public static void main(String[] args) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new MakeA3Booklet().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public void manipulatePdf(String dest) throws IOException {
  41.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         // Note that it is not necessary to create new PageSize object,
  44.         // but for testing reasons (connected to parallelization) we call constructor here
  45.         pdfDoc.setDefaultPageSize(new PageSize(PageSize.A3).rotate());
  46.  
  47.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  48.         float a4_width = PageSize.A4.getWidth();
  49.         int n = srcDoc.getNumberOfPages();
  50.         int p = 0;
  51.         PdfFormXObject page;
  52.         while (p++ < n) {
  53.             page = srcDoc.getPage(p).copyAsFormXObject(pdfDoc);
  54.             if (p % 2 == 1) {
  55.                 canvas.addXObject(page, 0, 0);
  56.             } else {
  57.                 canvas.addXObject(page, a4_width, 0);
  58.                 canvas = new PdfCanvas(pdfDoc.addNewPage());
  59.             }
  60.         }
  61.         pdfDoc.close();
  62.         srcDoc.close();
  63.     }
  64. }
MakeBooklet.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 a question on StackOverflow:
  10.  * http://stackoverflow.com/questions/34432502
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14.  
  15. import com.itextpdf.kernel.geom.AffineTransform;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfReader;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  21. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29.  
  30. @Category(SampleTest.class)
  31. public class MakeBooklet extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/merge/make_booklet.pdf";
  33.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new MakeBooklet().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void addPage(PdfCanvas canvas, PdfDocument srcDoc, PdfDocument pdfDoc, int p, float x) throws IOException {
  42.         if (p > srcDoc.getNumberOfPages()) {
  43.             return;
  44.         }
  45.         PdfFormXObject page = srcDoc.getPage(p).copyAsFormXObject(pdfDoc);
  46.         canvas.addXObject(page, x, 0);
  47.     }
  48.  
  49.     public void manipulatePdf(String dest) throws IOException {
  50.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  51.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  52.  
  53.         float a4_width = PageSize.A4.getWidth();
  54.         float a4_height = PageSize.A4.getHeight();
  55.         PageSize pagesize = new PageSize(a4_width * 4, a4_height * 2);
  56.         pdfDoc.setDefaultPageSize(pagesize);
  57.  
  58.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  59.         int n = srcDoc.getNumberOfPages();
  60.         int p = 1;
  61.         while ((p - 1) / 16
MakeBookletA3.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 a question on StackOverflow:
  10.  * http://stackoverflow.com/questions/34432502
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14.  
  15. import com.itextpdf.kernel.geom.PageSize;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class MakeBookletA3 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/merge/make_booklet_a3.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  33.  
  34.     public static void main(String[] args) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new MakeBookletA3().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public void addPage(PdfCanvas canvas, PdfDocument srcDoc, PdfDocument pdfDoc, int p, float x) throws IOException {
  41.         if (p > srcDoc.getNumberOfPages()) {
  42.             return;
  43.         }
  44.         PdfFormXObject page = srcDoc.getPage(p).copyAsFormXObject(pdfDoc);
  45.         canvas.addXObject(page, x, 0);
  46.     }
  47.  
  48.     public void manipulatePdf(String dest) throws IOException {
  49.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  51.         PageSize pageSize = new PageSize(
  52.                 PageSize.A4.getWidth() * 2,
  53.                 PageSize.A4.getHeight());
  54.         pdfDoc.setDefaultPageSize(pageSize);
  55.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  56.         float a4_width = PageSize.A4.getWidth();
  57.         int n = srcDoc.getNumberOfPages();
  58.         int p = 1;
  59.         while ((p - 1) / 4

Resources

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