Tiling and N-upping

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

7th November 2015
admin-marketing

Switch code for this example

TilingHero.java
  1. /**
  2.  * Example written by Bruno Lowagie.
  3.  */
  4. package sandbox.merge;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfImportedPage;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import java.io.File;
  14.  
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class TilingHero {
  21.  
  22.     /** The original PDF file. */
  23.     public static final String SRC
  24.         = "resources/pdfs/hero.pdf";
  25.  
  26.     /** The resulting PDF file. */
  27.     public static final String DEST
  28.         = "results/merge/superman.pdf";
  29.    
  30.     /**
  31.      * Manipulates a PDF file src with the file dest as result
  32.      * @param src the original PDF
  33.      * @param dest the resulting PDF
  34.      * @throws IOException
  35.      * @throws DocumentException
  36.      */
  37.     public void manipulatePdf(String src, String dest)
  38.         throws IOException, DocumentException {
  39.         // Creating a reader
  40.         PdfReader reader = new PdfReader(src);
  41.         Rectangle pagesize = reader.getPageSizeWithRotation(1);
  42.         float width = pagesize.getWidth();
  43.         float height = pagesize.getHeight();
  44.         // step 1
  45.         Rectangle mediabox = new Rectangle(0, 3 * height, width, 4 * height);
  46.         Document document = new Document(mediabox);
  47.         // step 2
  48.         PdfWriter writer
  49.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  50.         // step 3
  51.         document.open();
  52.         // step 4
  53.         PdfContentByte content = writer.getDirectContent();
  54.         PdfImportedPage page = writer.getImportedPage(reader, 1);
  55.         // adding the same page 16 times with a different offset
  56.         for (int i = 0; i < 16; ) {
  57.             content.addTemplate(page, 4, 0, 0, 4, 0, 0);
  58.             i++;
  59.             mediabox = new Rectangle(
  60.                     (i % 4) * width, (4 - (i / 4)) * height,
  61.                     ((i % 4) + 1) * width, (3 - (i / 4)) * height);
  62.             document.setPageSize(mediabox);
  63.             document.newPage();
  64.         }
  65.         // step 4
  66.         document.close();
  67.         reader.close();
  68.     }
  69.  
  70.     /**
  71.      * Main method.
  72.      * @param    args    no arguments needed
  73.      * @throws DocumentException
  74.      * @throws IOException
  75.      */
  76.     public static void main(String[] args)
  77.         throws IOException, DocumentException {
  78.         File file = new File(DEST);
  79.         file.getParentFile().mkdirs();
  80.         new TilingHero().manipulatePdf(SRC, DEST);
  81.     }
  82. }
TileClipped.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/32769493/tiling-with-itext-and-adding-margins
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  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.  
  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 TileClipped {
  25.  
  26.     public static final String SRC = "resources/pdfs/hero.pdf";
  27.     public static final String DEST = "results/stamper/hero_tiled_clipped.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new TileClipped().manipulatePdf(SRC, DEST);
  33.     }
  34.    
  35.     public void manipulatePdf(String src, String dest)
  36.         throws IOException, DocumentException {
  37.         float margin = 30;
  38.         // Creating a reader
  39.         PdfReader reader = new PdfReader(src);
  40.         Rectangle rect = reader.getPageSizeWithRotation(1);
  41.         Rectangle pagesize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
  42.         // step 1
  43.         Document document = new Document(pagesize);
  44.         // step 2
  45.         PdfWriter writer
  46.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  47.         // step 3
  48.         document.open();
  49.         // step 4
  50.         PdfContentByte content = writer.getDirectContent();
  51.         PdfImportedPage page = writer.getImportedPage(reader, 1);
  52.         // adding the same page 16 times with a different offset
  53.         float x, y;
  54.         for (int i = 0; i < 16; i++) {
  55.             x = -rect.getWidth() * (i % 4) + margin;
  56.             y = rect.getHeight() * (i / 4 - 3) + margin;
  57.             content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
  58.             content.clip();
  59.             content.newPath();
  60.             content.addTemplate(page, 4, 0, 0, 4, x, y);
  61.             document.newPage();
  62.         }
  63.         // step 4
  64.         document.close();
  65.         reader.close();
  66.     }
  67. }
TileInTwo.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow:
  3.  * http://stackoverflow.com/questions/27011829/divide-one-page-pdf-file-in-two-pages-pdf-file
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  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.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class TileInTwo {
  22.    
  23.     /** The original PDF file. */
  24.     public static final String SRC
  25.         = "resources/pdfs/united_states.pdf";
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String DEST
  29.         = "results/merge/unitedstates_tiled.pdf";
  30.    
  31.     public static void main(String[] args) throws DocumentException, IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new TileInTwo().manipulatePdf(SRC, DEST);
  35.     }
  36.    
  37.     public void manipulatePdf(String src, String dest)
  38.         throws IOException, DocumentException {
  39.         // Creating a reader
  40.         PdfReader reader = new PdfReader(src);
  41.         int n = reader.getNumberOfPages();
  42.         // step 1
  43.         Rectangle mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(1)));
  44.         Document document = new Document(mediabox);
  45.         // step 2
  46.         PdfWriter writer
  47.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  48.         // step 3
  49.         document.open();
  50.         // step 4
  51.         PdfContentByte content = writer.getDirectContent();
  52.         PdfImportedPage page;
  53.         int i = 1;
  54.         while (true) {
  55.             page = writer.getImportedPage(reader, i);
  56.             content.addTemplate(page, 0, -mediabox.getHeight());
  57.             document.newPage();
  58.             content.addTemplate(page, 0, 0);
  59.             if (++i > n)
  60.                 break;
  61.             mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(i)));
  62.             document.setPageSize(mediabox);
  63.             document.newPage();
  64.         }
  65.         // step 5
  66.         document.close();
  67.         reader.close();
  68.     }
  69.    
  70.     public Rectangle getHalfPageSize(Rectangle pagesize) {
  71.         float width = pagesize.getWidth();
  72.         float height = pagesize.getHeight();
  73.         return new Rectangle(width, height / 2);
  74.     }
  75. }
TileInTwo2.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow:
  3.  * http://stackoverflow.com/questions/27600809/divide-pdf-exact-equal-half-using-itextsharp
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  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.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class TileInTwo2 {
  22.    
  23.     /** The original PDF file. */
  24.     public static final String SRC
  25.         = "resources/pdfs/united_states.pdf";
  26.  
  27.     /** The resulting PDF file. */
  28.     public static final String DEST
  29.         = "results/merge/unitedstates_tiled2.pdf";
  30.    
  31.     public static void main(String[] args) throws DocumentException, IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new TileInTwo2().manipulatePdf(SRC, DEST);
  35.     }
  36.    
  37.     public void manipulatePdf(String src, String dest)
  38.         throws IOException, DocumentException {
  39.         // Creating a reader
  40.         PdfReader reader = new PdfReader(src);
  41.         int n = reader.getNumberOfPages();
  42.         // step 1
  43.         Rectangle mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(1)));
  44.         Document document = new Document(mediabox);
  45.         // step 2
  46.         PdfWriter writer
  47.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  48.         // step 3
  49.         document.open();
  50.         // step 4
  51.         PdfContentByte content = writer.getDirectContent();
  52.         PdfImportedPage page;
  53.         int i = 1;
  54.         while (true) {
  55.             page = writer.getImportedPage(reader, i);
  56.             content.addTemplate(page, 0, 0);
  57.             document.newPage();
  58.             content.addTemplate(page, -mediabox.getWidth(), 0);
  59.             if (++i > n)
  60.                 break;
  61.             mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(i)));
  62.             document.setPageSize(mediabox);
  63.             document.newPage();
  64.         }
  65.         // step 5
  66.         document.close();
  67.         reader.close();
  68.     }
  69.    
  70.     public Rectangle getHalfPageSize(Rectangle pagesize) {
  71.         float width = pagesize.getWidth();
  72.         float height = pagesize.getHeight();
  73.         return new Rectangle(width / 2, height);
  74.     }
  75. }
NUpWithLink.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to a question on StackOverflow
  3.  *
  4.  * When concatenating documents, we add a named destination every time
  5.  * a new document is started. After we've finished merging, we add an extra
  6.  * page with the table of contents and links to the named destinations.
  7.  */
  8. package sandbox.merge;
  9.  
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.pdf.PdfAnnotation;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfImportedPage;
  16. import com.itextpdf.text.pdf.PdfReader;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class NUpWithLink {
  27.     public static final String SRC1 = "resources/pdfs/links1.pdf";
  28.     public static final String SRC2 = "resources/pdfs/links2.pdf";
  29.     public static final String DEST = "results/merge/nup_links.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new NUpWithLink().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String filename) throws IOException, DocumentException {
  38.          Document document = new Document();
  39.          PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  40.          float W = PageSize.A4.getWidth() / 2;
  41.          float H = PageSize.A4.getHeight() / 2;
  42.          document.open();
  43.          int firstPage = 1;
  44.          String[] files = new String[]{SRC1, SRC2};
  45.          PdfContentByte cb = writer.getDirectContent();
  46.          for (int i = 0; i < files.length; i++) {
  47.             PdfReader currentReader = new PdfReader(files[i]);
  48.             currentReader.consolidateNamedDestinations();
  49.             for (int page = 1; page
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