Page orientation and rotation

Tags: page orientationpage rotationlandscapeiText 5seascapeportraitinverted portrait

Examples written in answer to questions such as:

Files: 
/**
 * This example was written by Bruno Lowagie.
 */
package sandbox.events;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfPage;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class PageOrientations {
    public static final String DEST = "results/events/orientations.pdf";
 
    public class Rotate extends PdfPageEventHelper {
 
        protected PdfNumber orientation = PdfPage.PORTRAIT;
 
        public void setOrientation(PdfNumber orientation) {
            this.orientation = orientation;
        }
 
        @Override
        public void onStartPage(PdfWriter writer, Document document) {
            writer.addPageDictEntry(PdfName.ROTATE, orientation);
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PageOrientations().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        Rotate event = new Rotate();
        writer.setPageEvent(event);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("A simple page in portrait orientation"));
        event.setOrientation(PdfPage.LANDSCAPE);
        document.newPage();
        document.add(new Paragraph("A simple page in landscape orientation"));;
        event.setOrientation(PdfPage.INVERTEDPORTRAIT);
        document.newPage();
        document.add(new Paragraph("A simple page in inverted portrait orientation"));;
        event.setOrientation(PdfPage.SEASCAPE);
        document.newPage();
        document.add(new Paragraph("A simple page in seascape orientation"));
        // step 5
        document.close();
    }
}
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/22051835/itext-pdf-document-rotate-some-but-not-all-pages
 */
package sandbox.events;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfPage;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class PageRotation {
    public static final String DEST = "results/events/rotate_pages.pdf";
 
 
    public class Rotate extends PdfPageEventHelper {
        protected PdfNumber rotation = PdfPage.PORTRAIT;
        public void setRotation(PdfNumber rotation) {
            this.rotation = rotation;
        }
        public void onEndPage(PdfWriter writer, Document document) {
            writer.addPageDictEntry(PdfName.ROTATE, rotation);
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PageRotation().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        Rotate rotation = new Rotate();
        writer.setPageEvent(rotation);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        rotation.setRotation(PdfPage.LANDSCAPE);
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        rotation.setRotation(PdfPage.INVERTEDPORTRAIT);
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        rotation.setRotation(PdfPage.SEASCAPE);
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        rotation.setRotation(PdfPage.PORTRAIT);
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }
}
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29152313/fix-the-orientation-of-a-pdf-in-order-to-scale-it
 */
package sandbox.events;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPage;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class Seascape {
    public static final String DEST = "results/events/seascape.pdf";
 
    public class Rotate extends PdfPageEventHelper {
        @Override
        public void onStartPage(PdfWriter writer, Document document) {
            writer.addPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Seascape().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        Rotate event = new Rotate();
        writer.setPageEvent(event);
        // step 3
        document.open();
        // step 4
        for (int i = 0; i < 50; i++)
            document.add(new Paragraph("Hello World!"));
        document.newPage();
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }
}
File nameRaw URLUpdated
PageOrientations.javaPageOrientations.java2015-11-08 1:37 pm
PageRotation.javaPageRotation.java2015-11-08 1:37 pm
Seascape.javaSeascape.java2015-11-08 1:37 pm
Results: 
File nameRaw URLUpdated
cmp_orientations.pdfcmp_orientations.pdf2015-11-08 1:38 pm
cmp_rotate_pages.pdfcmp_rotate_pages.pdf2015-11-08 1:38 pm
cmp_seascape.pdfcmp_seascape.pdf2015-11-08 1:38 pm