How not to merge documents

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

31st May 2016
iText PDF

Switch code for this example

IncorrectExample.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/28332692/itextsharp-rotated-pdf-page-reverts-orientation-when-file-is-rasterized-at-print
  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 IncorrectExample extends GenericTest {
  31.     public static final String DEST
  32.             = "./target/test/resources/sandbox/merge/incorrect_example.pdf";
  33.     public static final String SOURCE
  34.             = "./src/test/resources/pdfs/pages.pdf";
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new IncorrectExample().manipulatePdf(DEST);
  40.     }
  41.  
  42.     public void manipulatePdf(String dest) throws IOException {
  43.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SOURCE));
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.  
  46.         PageSize pageSize;
  47.         PdfCanvas canvas;
  48.         PdfFormXObject page;
  49.         for (int i = 1; i <= srcDoc.getNumberOfPages(); i++) {
  50.             pageSize = getPageSize(srcDoc, i);
  51.             pdfDoc.setDefaultPageSize(pageSize);
  52.             canvas = new PdfCanvas(pdfDoc.addNewPage());
  53.             page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
  54.             if (isPortrait(srcDoc, i)) {
  55.                 canvas.addXObject(page, 0, 0);
  56.             } else {
  57.                 canvas.addXObject(page, 0, 1, -1, 0, pageSize.getWidth(), 0);
  58.             }
  59.  
  60.         }
  61.         pdfDoc.close();
  62.         srcDoc.close();
  63.     }
  64.  
  65.     public PageSize getPageSize(PdfDocument pdfDoc, int pageNumber) {
  66.         Rectangle pageSize = pdfDoc.getPage(pageNumber).getPageSize();
  67.         return new PageSize(
  68.                 Math.min(pageSize.getWidth(), pageSize.getHeight()),
  69.                 Math.max(pageSize.getWidth(), pageSize.getHeight()));
  70.     }
  71.  
  72.     public boolean isPortrait(PdfDocument pdfDoc, int pageNumber) {
  73.         Rectangle pageSize = pdfDoc.getPage(pageNumber).getPageSize();
  74.         return pageSize.getHeight() > pageSize.getWidth();
  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