Page orientation and rotation

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

31st May 2016
iText PDF

Switch code for this example

PageOrientations.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.  * This example was written by Bruno Lowagie.
  10.  */
  11. package com.itextpdf.samples.sandbox.events;
  12.  
  13. import com.itextpdf.kernel.events.Event;
  14. import com.itextpdf.kernel.events.IEventHandler;
  15. import com.itextpdf.kernel.events.PdfDocumentEvent;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfName;
  18. import com.itextpdf.kernel.pdf.PdfNumber;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.AreaBreak;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30. @Category(SampleTest.class)
  31. public class PageOrientations extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/events/page_orientations.pdf";
  33.  
  34.     /* Constants form itext5 */
  35.     public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
  36.     public static final PdfNumber LANDSCAPE = new PdfNumber(90);
  37.     public static final PdfNumber PORTRAIT = new PdfNumber(0);
  38.     public static final PdfNumber SEASCAPE = new PdfNumber(270);
  39.  
  40.     public static void main(String[] args) throws Exception {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new PageOrientations().manipulatePdf(DEST);
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  49.         PageOrientationsEventHandler eventHandler = new PageOrientationsEventHandler();
  50.         pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
  51.         Document doc = new Document(pdfDoc);
  52.         doc.add(new Paragraph("A simple page in portrait orientation"));
  53.         eventHandler.setOrientation(LANDSCAPE);
  54.         doc.add(new AreaBreak());
  55.         doc.add(new Paragraph("A simple page in landscape orientation"));
  56.         eventHandler.setOrientation(INVERTEDPORTRAIT);
  57.         doc.add(new AreaBreak());
  58.         doc.add(new Paragraph("A simple page in inverted portrait orientation"));
  59.         eventHandler.setOrientation(SEASCAPE);
  60.         doc.add(new AreaBreak());
  61.         doc.add(new Paragraph("A simple page in seascape orientation"));
  62.         doc.close();
  63.     }
  64.  
  65.  
  66.     public static class PageOrientationsEventHandler implements IEventHandler {
  67.         protected PdfNumber orientation = PORTRAIT;
  68.  
  69.         public void setOrientation(PdfNumber orientation) {
  70.             this.orientation = orientation;
  71.         }
  72.  
  73.         @Override
  74.         public void handleEvent(Event event) {
  75.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  76.             docEvent.getPage().put(PdfName.Rotate, orientation);
  77.         }
  78.     }
  79. }
PageRotation.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.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/22051835/itext-pdf-document-rotate-some-but-not-all-pages
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.events.Event;
  15. import com.itextpdf.kernel.events.IEventHandler;
  16. import com.itextpdf.kernel.events.PdfDocumentEvent;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfName;
  19. import com.itextpdf.kernel.pdf.PdfNumber;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.AreaBreak;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class PageRotation extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/events/page_rotation.pdf";
  34.     /* Constants form itext5 */
  35.     public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
  36.     public static final PdfNumber LANDSCAPE = new PdfNumber(90);
  37.     public static final PdfNumber PORTRAIT = new PdfNumber(0);
  38.     public static final PdfNumber SEASCAPE = new PdfNumber(270);
  39.  
  40.     private static final Paragraph HELLO_WORLD = new Paragraph("Hello World!");
  41.  
  42.     public static void main(String[] args) throws Exception {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new PageRotation().manipulatePdf(DEST);
  46.     }
  47.  
  48.     @Override
  49.     protected void manipulatePdf(String dest) throws Exception {
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  51.         PageRotationEventHandler eventHandler = new PageRotationEventHandler();
  52.         pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
  53.         Document doc = new Document(pdfDoc);
  54.         doc.add(HELLO_WORLD);
  55.         eventHandler.setRotation(LANDSCAPE);
  56.         doc.add(new AreaBreak());
  57.         doc.add(HELLO_WORLD);
  58.         eventHandler.setRotation(INVERTEDPORTRAIT);
  59.         doc.add(new AreaBreak());
  60.         doc.add(HELLO_WORLD);
  61.         eventHandler.setRotation(SEASCAPE);
  62.         doc.add(new AreaBreak());
  63.         doc.add(HELLO_WORLD);
  64.         eventHandler.setRotation(PORTRAIT);
  65.         doc.add(new AreaBreak());
  66.         doc.add(HELLO_WORLD);
  67.         doc.close();
  68.     }
  69.  
  70.  
  71.     protected class PageRotationEventHandler implements IEventHandler {
  72.         protected PdfNumber rotation = PORTRAIT;
  73.  
  74.         public void setRotation(PdfNumber orientation) {
  75.             this.rotation = orientation;
  76.         }
  77.  
  78.         @Override
  79.         public void handleEvent(Event event) {
  80.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  81.             docEvent.getPage().put(PdfName.Rotate, rotation);
  82.         }
  83.     }
  84. }
Seascape.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.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/29152313/fix-the-orientation-of-a-pdf-in-order-to-scale-it
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.events.Event;
  15. import com.itextpdf.kernel.events.IEventHandler;
  16. import com.itextpdf.kernel.events.PdfDocumentEvent;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfName;
  19. import com.itextpdf.kernel.pdf.PdfNumber;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.AreaBreak;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class Seascape extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/events/seascape.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new Seascape().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  44.         Document doc = new Document(pdfDoc);
  45.         pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new SeascapeEventHandler());
  46.         for (int i = 0; i < 50; i++)
  47.             doc.add(new Paragraph("Hello World!"));
  48.         doc.add(new AreaBreak());
  49.         doc.add(new Paragraph("Hello World!"));
  50.         doc.add(new AreaBreak());
  51.         doc.add(new Paragraph("Hello World!"));
  52.         doc.close();
  53.     }
  54.  
  55.  
  56.     protected class SeascapeEventHandler implements IEventHandler {
  57.         @Override
  58.         public void handleEvent(Event event) {
  59.             PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
  60.             documentEvent.getPage().getPdfObject().put(PdfName.Rotate, new PdfNumber(270));
  61.         }
  62.     }
  63. }
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