Tiling and N-upping

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

31st May 2016
iText PDF

Switch code for this example

TileInTwo.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/27011829/divide-one-page-pdf-file-in-two-pages-pdf-file
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.geom.Rectangle;
  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 TileInTwo extends GenericTest {
  31.     public static final String DEST
  32.             = "./target/test/resources/sandbox/merge/tile_in_two.pdf";
  33.     public static final String SRC
  34.             = "./src/test/resources/pdfs/united_states.pdf";
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new TileInTwo().manipulatePdf(DEST);
  40.     }
  41.  
  42.     public static PageSize getHalfPageSize(Rectangle pagesize) {
  43.         float width = pagesize.getWidth();
  44.         float height = pagesize.getHeight();
  45.         return new PageSize(width, height / 2);
  46.     }
  47.  
  48.     public void manipulatePdf(String dest) throws IOException {
  49.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  50.         PageSize mediaBox = getHalfPageSize(srcDoc.getFirstPage().getPageSizeWithRotation());
  51.  
  52.         PdfDocument resultDoc = new PdfDocument(new PdfWriter(DEST));
  53.         resultDoc.setDefaultPageSize(mediaBox);
  54.  
  55.         PdfCanvas canvas;
  56.         int n = srcDoc.getNumberOfPages();
  57.         int i = 1;
  58.         while (true) {
  59.             PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(resultDoc);
  60.             canvas = new PdfCanvas(resultDoc.addNewPage());
  61.             canvas.addXObject(page, 0, -mediaBox.getHeight());
  62.             canvas = new PdfCanvas(resultDoc.addNewPage());
  63.             canvas.addXObject(page, 0, 0);
  64.             if (++i > n) {
  65.                 break;
  66.             }
  67.             mediaBox = getHalfPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
  68.             resultDoc.setDefaultPageSize(mediaBox);
  69.         }
  70.  
  71.         resultDoc.close();
  72.         srcDoc.close();
  73.     }
  74. }
TileInTwo2.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/27011829/divide-one-page-pdf-file-in-two-pages-pdf-file
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.geom.Rectangle;
  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 TileInTwo2 extends GenericTest {
  31.     public static final String DEST
  32.             = "./target/test/resources/sandbox/merge/tile_in_two2.pdf";
  33.     public static final String SRC
  34.             = "./src/test/resources/pdfs/united_states.pdf";
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new TileInTwo2().manipulatePdf(DEST);
  40.     }
  41.  
  42.     public static PageSize getHalfPageSize(Rectangle pageSize) {
  43.         float width = pageSize.getWidth();
  44.         float height = pageSize.getHeight();
  45.         return new PageSize(width / 2, height);
  46.     }
  47.  
  48.     public void manipulatePdf(String dest) throws IOException {
  49.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  50.         PageSize mediaBox = getHalfPageSize(srcDoc.getFirstPage().getPageSizeWithRotation());
  51.  
  52.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  53.         pdfDoc.setDefaultPageSize(mediaBox);
  54.  
  55.         PdfCanvas canvas;
  56.         int n = srcDoc.getNumberOfPages();
  57.         int i = 1;
  58.         while (true) {
  59.             PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
  60.             canvas = new PdfCanvas(pdfDoc.addNewPage());
  61.             canvas.addXObject(page, 0, 0);
  62.             canvas = new PdfCanvas(pdfDoc.addNewPage());
  63.             canvas.addXObject(page, -mediaBox.getWidth(), 0);
  64.             if (++i > n) {
  65.                 break;
  66.             }
  67.             mediaBox = getHalfPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
  68.             pdfDoc.setDefaultPageSize(mediaBox);
  69.         }
  70.         pdfDoc.close();
  71.         srcDoc.close();
  72.     }
  73. }
TilingHero.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.  */
  11. package com.itextpdf.samples.sandbox.merge;
  12.  
  13. import com.itextpdf.kernel.geom.PageSize;
  14. import com.itextpdf.kernel.geom.Rectangle;
  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.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26. import java.io.IOException;
  27.  
  28. @Category(SampleTest.class)
  29. public class TilingHero extends GenericTest {
  30.     public static final String DEST
  31.             = "./target/test/resources/sandbox/merge/tiling_hero.pdf";
  32.     public static final String RESOURCE
  33.             = "./src/test/resources/pdfs/hero.pdf";
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new TilingHero().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void manipulatePdf(String dest) throws IOException {
  42.         PdfDocument srcDoc = new PdfDocument(new PdfReader(RESOURCE));
  43.  
  44.         Rectangle pageSize = srcDoc.getFirstPage().getPageSizeWithRotation();
  45.         float width = pageSize.getWidth();
  46.         float height = pageSize.getHeight();
  47.         Rectangle mediaBox = new Rectangle(0, 3 * height, width, height);
  48.  
  49.         PdfDocument resultDoc = new PdfDocument(new PdfWriter(DEST));
  50.         resultDoc.setDefaultPageSize(new PageSize(mediaBox));
  51.         PdfFormXObject page = srcDoc.getFirstPage().copyAsFormXObject(resultDoc);
  52.         PdfCanvas canvas;
  53.         for (int i = 0; i < 16; ) {
  54.             canvas = new PdfCanvas(resultDoc.addNewPage());
  55.             canvas.addXObject(page, 4, 0, 0, 4, 0, 0);
  56.             i++;
  57.             mediaBox = new Rectangle(
  58.                     (i % 4) * width, (4 - (i / 4)) * height,
  59.                     width, -height);
  60.             resultDoc.setDefaultPageSize(new PageSize(mediaBox));
  61.         }
  62.  
  63.         srcDoc.close();
  64.         resultDoc.close();
  65.     }
  66. }
TileClipped.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/32769493/tiling-with-itext-and-adding-margins
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.geom.Rectangle;
  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.  
  28. @Category(SampleTest.class)
  29. public class TileClipped extends GenericTest {
  30.     public static final String DEST =
  31.             "./target/test/resources/sandbox/stamper/tile_clipped.pdf";
  32.     public static final String SRC =
  33.             "./src/test/resources/pdfs/hero.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new TileClipped().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         float margin = 30;
  44.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  45.         Rectangle rect = srcDoc.getFirstPage().getPageSizeWithRotation();
  46.         Rectangle pageSize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
  47.  
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  49.         pdfDoc.setDefaultPageSize(new PageSize(pageSize));
  50.  
  51.         PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
  52.         PdfFormXObject page = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);
  53.         // adding the same page 16 times with a different offset
  54.         float x, y;
  55.         for (int i = 0; i < 16; i++) {
  56.             x = -rect.getWidth() * (i % 4) + margin;
  57.             y = rect.getHeight() * (i / 4 - 3) + margin;
  58.             content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
  59.             content.clip();
  60.             content.newPath();
  61.             content.addXObject(page, 4, 0, 0, 4, x, y);
  62.             if (15 != i) {
  63.                 content = new PdfCanvas(pdfDoc.addNewPage());
  64.             }
  65.         }
  66.         srcDoc.close();
  67.         pdfDoc.close();
  68.     }
  69. }
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