Page orientation and rotation

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

8th November 2015
admin-marketing

Switch code for this example

PageOrientations.java
  1. /**
  2.  * This example was written by Bruno Lowagie.
  3.  */
  4. package sandbox.events;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Paragraph;
  9. import com.itextpdf.text.pdf.PdfName;
  10. import com.itextpdf.text.pdf.PdfNumber;
  11. import com.itextpdf.text.pdf.PdfPage;
  12. import com.itextpdf.text.pdf.PdfPageEventHelper;
  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 PageOrientations {
  22.     public static final String DEST = "results/events/orientations.pdf";
  23.    
  24.     public class Rotate extends PdfPageEventHelper {
  25.        
  26.         protected PdfNumber orientation = PdfPage.PORTRAIT;
  27.        
  28.         public void setOrientation(PdfNumber orientation) {
  29.             this.orientation = orientation;
  30.         }
  31.        
  32.         @Override
  33.         public void onStartPage(PdfWriter writer, Document document) {
  34.             writer.addPageDictEntry(PdfName.ROTATE, orientation);
  35.         }
  36.     }
  37.    
  38.     public static void main(String[] args) throws IOException, DocumentException {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new PageOrientations().createPdf(DEST);
  42.     }
  43.    
  44.     public void createPdf(String filename) throws IOException, DocumentException {
  45.         // step 1
  46.         Document document = new Document();
  47.         // step 2
  48.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  49.         Rotate event = new Rotate();
  50.         writer.setPageEvent(event);
  51.         // step 3
  52.         document.open();
  53.         // step 4
  54.         document.add(new Paragraph("A simple page in portrait orientation"));
  55.         event.setOrientation(PdfPage.LANDSCAPE);
  56.         document.newPage();
  57.         document.add(new Paragraph("A simple page in landscape orientation"));;
  58.         event.setOrientation(PdfPage.INVERTEDPORTRAIT);
  59.         document.newPage();
  60.         document.add(new Paragraph("A simple page in inverted portrait orientation"));;
  61.         event.setOrientation(PdfPage.SEASCAPE);
  62.         document.newPage();
  63.         document.add(new Paragraph("A simple page in seascape orientation"));
  64.         // step 5
  65.         document.close();
  66.     }
  67. }
PageRotation.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22051835/itext-pdf-document-rotate-some-but-not-all-pages
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfNumber;
  12. import com.itextpdf.text.pdf.PdfPage;
  13. import com.itextpdf.text.pdf.PdfPageEventHelper;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class PageRotation {
  24.     public static final String DEST = "results/events/rotate_pages.pdf";
  25.    
  26.    
  27.     public class Rotate extends PdfPageEventHelper {
  28.         protected PdfNumber rotation = PdfPage.PORTRAIT;
  29.         public void setRotation(PdfNumber rotation) {
  30.             this.rotation = rotation;
  31.         }
  32.         public void onEndPage(PdfWriter writer, Document document) {
  33.             writer.addPageDictEntry(PdfName.ROTATE, rotation);
  34.         }
  35.     }
  36.    
  37.     public static void main(String[] args) throws IOException, DocumentException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new PageRotation().createPdf(DEST);
  41.     }
  42.    
  43.     public void createPdf(String filename) throws IOException, DocumentException {
  44.         // step 1
  45.         Document document = new Document();
  46.         // step 2
  47.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  48.         Rotate rotation = new Rotate();
  49.         writer.setPageEvent(rotation);
  50.         // step 3
  51.         document.open();
  52.         // step 4
  53.         document.add(new Paragraph("Hello World!"));
  54.         document.newPage();
  55.         rotation.setRotation(PdfPage.LANDSCAPE);
  56.         document.add(new Paragraph("Hello World!"));
  57.         document.newPage();
  58.         rotation.setRotation(PdfPage.INVERTEDPORTRAIT);
  59.         document.add(new Paragraph("Hello World!"));
  60.         document.newPage();
  61.         rotation.setRotation(PdfPage.SEASCAPE);
  62.         document.add(new Paragraph("Hello World!"));
  63.         document.newPage();
  64.         rotation.setRotation(PdfPage.PORTRAIT);
  65.         document.add(new Paragraph("Hello World!"));
  66.         // step 5
  67.         document.close();
  68.     }
  69. }
Seascape.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/29152313/fix-the-orientation-of-a-pdf-in-order-to-scale-it
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfPage;
  12. import com.itextpdf.text.pdf.PdfPageEventHelper;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class Seascape {
  23.     public static final String DEST = "results/events/seascape.pdf";
  24.    
  25.     public class Rotate extends PdfPageEventHelper {
  26.         @Override
  27.         public void onStartPage(PdfWriter writer, Document document) {
  28.             writer.addPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
  29.         }
  30.     }
  31.    
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new Seascape().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String filename) throws IOException, DocumentException {
  39.         // step 1
  40.         Document document = new Document();
  41.         // step 2
  42.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  43.         Rotate event = new Rotate();
  44.         writer.setPageEvent(event);
  45.         // step 3
  46.         document.open();
  47.         // step 4
  48.         for (int i = 0; i < 50; i++)
  49.             document.add(new Paragraph("Hello World!"));
  50.         document.newPage();
  51.         document.add(new Paragraph("Hello World!"));
  52.         document.newPage();
  53.         document.add(new Paragraph("Hello World!"));
  54.         // step 5
  55.         document.close();
  56.     }
  57. }
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