How not to merge documents

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

7th November 2015
admin-marketing

Switch code for this example

This is an example that is used by many people, but it is completely wrong: one should not use PdfWriter to merge documents. Use PdfCopy or PdfSmartCopy instead.



IncorrectExample.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/28332692/itextsharp-rotated-pdf-page-reverts-orientation-when-file-is-rasterized-at-print
  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. import java.io.File;
  15.  
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * This example is named IncorrectExample because this is not how you would
  22.  * typically solve the problem of rotating pages, nor of merging documents.
  23.  * The correct way is NOT to use Document / PdfWriter, but to use PdfStamper,
  24.  * PdfCopy or PdfSmartCopy. However: in the question mentioned above, the
  25.  * circumstances are very particular and using this example in those
  26.  * circumstances is justified.
  27.  */
  28. @WrapToTest
  29. public class IncorrectExample {
  30.  
  31.     /** The original PDF file. */
  32.     public static final String SRC
  33.         = "resources/pdfs/pages.pdf";
  34.  
  35.     /** The resulting PDF file. */
  36.     public static final String DEST
  37.         = "results/merge/pages_changed.pdf";
  38.    
  39.     /**
  40.      * Manipulates a PDF file src with the file dest as result
  41.      * @param src the original PDF
  42.      * @param dest the resulting PDF
  43.      * @throws IOException
  44.      * @throws DocumentException
  45.      */
  46.     public void manipulatePdf(String src, String dest)
  47.         throws IOException, DocumentException {
  48.         // Creating a reader
  49.         PdfReader reader = new PdfReader(src);
  50.         // step 1
  51.         Rectangle pagesize = getPageSize(reader, 1);
  52.         Document document = new Document(pagesize);
  53.         // step 2
  54.         PdfWriter writer
  55.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  56.         // step 3
  57.         document.open();
  58.         // step 4
  59.         PdfContentByte cb = writer.getDirectContent();
  60.         for (int i = 1; i  pagesize.getWidth();
  61.     }
  62.    
  63.     /**
  64.      * Main method.
  65.      * @param    args    no arguments needed
  66.      * @throws DocumentException
  67.      * @throws IOException
  68.      */
  69.     public static void main(String[] args)
  70.         throws IOException, DocumentException {
  71.         File file = new File(DEST);
  72.         file.getParentFile().mkdirs();
  73.         new IncorrectExample().manipulatePdf(SRC, DEST);
  74.     }
  75. }
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