Skip to main content
Skip table of contents

Page orientation and rotation

Examples written in answer to questions such as:


pageorientations

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.events;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class PageOrientations {
    public static final String DEST = "./target/sandbox/events/page_orientations.pdf";

    public static final PdfNumber PORTRAIT = new PdfNumber(0);
    public static final PdfNumber LANDSCAPE = new PdfNumber(90);
    public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
    public static final PdfNumber SEASCAPE = new PdfNumber(270);

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new PageOrientations().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        // The default page orientation is set to portrait in the custom event handler.
        PageOrientationsEventHandler eventHandler = new PageOrientationsEventHandler();
        pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
        Document doc = new Document(pdfDoc);

        doc.add(new Paragraph("A simple page in portrait orientation"));

        eventHandler.setOrientation(LANDSCAPE);
        doc.add(new AreaBreak());
        doc.add(new Paragraph("A simple page in landscape orientation"));

        eventHandler.setOrientation(INVERTEDPORTRAIT);
        doc.add(new AreaBreak());
        doc.add(new Paragraph("A simple page in inverted portrait orientation"));

        eventHandler.setOrientation(SEASCAPE);
        doc.add(new AreaBreak());
        doc.add(new Paragraph("A simple page in seascape orientation"));

        doc.close();
    }


    private static class PageOrientationsEventHandler implements IEventHandler {
        private PdfNumber orientation = PORTRAIT;

        public void setOrientation(PdfNumber orientation) {
            this.orientation = orientation;
        }

        @Override
        public void handleEvent(Event currentEvent) {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
            docEvent.getPage().put(PdfName.Rotate, orientation);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Events;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Events
{
    public class PageOrientations
    {
        public static readonly String DEST = "results/sandbox/events/page_orientations.pdf";

        public static readonly PdfNumber PORTRAIT = new PdfNumber(0);
        public static readonly PdfNumber LANDSCAPE = new PdfNumber(90);
        public static readonly PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
        public static readonly PdfNumber SEASCAPE = new PdfNumber(270);

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new PageOrientations().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            // The default page orientation is set to portrait in the custom event handler.
            PageOrientationsEventHandler eventHandler = new PageOrientationsEventHandler();
            pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
            Document doc = new Document(pdfDoc);

            doc.Add(new Paragraph("A simple page in portrait orientation"));

            eventHandler.SetOrientation(LANDSCAPE);
            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("A simple page in landscape orientation"));

            eventHandler.SetOrientation(INVERTEDPORTRAIT);
            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("A simple page in inverted portrait orientation"));

            eventHandler.SetOrientation(SEASCAPE);
            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("A simple page in seascape orientation"));

            doc.Close();
        }

        private class PageOrientationsEventHandler : IEventHandler
        {
            private PdfNumber orientation = PORTRAIT;

            public void SetOrientation(PdfNumber orientation)
            {
                this.orientation = orientation;
            }

            public void HandleEvent(Event currentEvent)
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
                docEvent.GetPage().Put(PdfName.Rotate, orientation);
            }
        }
    }
}


pagerotation

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.events;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class PageRotation {
    public static final String DEST = "./target/sandbox/events/page_rotation.pdf";

    public static final PdfNumber PORTRAIT = new PdfNumber(0);
    public static final PdfNumber LANDSCAPE = new PdfNumber(90);
    public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
    public static final PdfNumber SEASCAPE = new PdfNumber(270);

    private static final Paragraph HELLO_WORLD = new Paragraph("Hello World!");

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new PageRotation().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        // The default page rotation is set to portrait in the custom event handler.
        PageRotationEventHandler eventHandler = new PageRotationEventHandler();
        pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
        Document doc = new Document(pdfDoc);

        doc.add(HELLO_WORLD);

        eventHandler.setRotation(LANDSCAPE);
        doc.add(new AreaBreak());
        doc.add(HELLO_WORLD);

        eventHandler.setRotation(INVERTEDPORTRAIT);
        doc.add(new AreaBreak());
        doc.add(HELLO_WORLD);

        eventHandler.setRotation(SEASCAPE);
        doc.add(new AreaBreak());
        doc.add(HELLO_WORLD);

        eventHandler.setRotation(PORTRAIT);
        doc.add(new AreaBreak());
        doc.add(HELLO_WORLD);

        doc.close();
    }


    private static class PageRotationEventHandler implements IEventHandler {
        private PdfNumber rotation = PORTRAIT;

        public void setRotation(PdfNumber orientation) {
            this.rotation = orientation;
        }

        @Override
        public void handleEvent(Event currentEvent) {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
            docEvent.getPage().put(PdfName.Rotate, rotation);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Events;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Events
{
    public class PageRotation
    {
        public static readonly String DEST = "results/sandbox/events/page_rotation.pdf";

        public static readonly PdfNumber PORTRAIT = new PdfNumber(0);
        public static readonly PdfNumber LANDSCAPE = new PdfNumber(90);
        public static readonly PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
        public static readonly PdfNumber SEASCAPE = new PdfNumber(270);

        private static readonly Paragraph HELLO_WORLD = new Paragraph("Hello World!");

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new PageRotation().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            // The default page rotation is set to portrait in the custom event handler.
            PageRotationEventHandler eventHandler = new PageRotationEventHandler();
            pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
            Document doc = new Document(pdfDoc);

            doc.Add(HELLO_WORLD);

            eventHandler.SetRotation(LANDSCAPE);
            doc.Add(new AreaBreak());
            doc.Add(HELLO_WORLD);

            eventHandler.SetRotation(INVERTEDPORTRAIT);
            doc.Add(new AreaBreak());
            doc.Add(HELLO_WORLD);

            eventHandler.SetRotation(SEASCAPE);
            doc.Add(new AreaBreak());
            doc.Add(HELLO_WORLD);

            eventHandler.SetRotation(PORTRAIT);
            doc.Add(new AreaBreak());
            doc.Add(HELLO_WORLD);

            doc.Close();
        }

        private class PageRotationEventHandler : IEventHandler
        {
            private PdfNumber rotation = PORTRAIT;

            public void SetRotation(PdfNumber orientation)
            {
                this.rotation = orientation;
            }

            public void HandleEvent(Event currentEvent)
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
                docEvent.GetPage().Put(PdfName.Rotate, rotation);
            }
        }
    }
}


seascape

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.events;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class Seascape {
    public static final String DEST = "./target/sandbox/events/seascape.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new Seascape().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new SeascapeEventHandler());

        for (int i = 0; i < 50; i++) {
            doc.add(new Paragraph("Hello World!"));
        }

        doc.add(new AreaBreak());
        doc.add(new Paragraph("Hello World!"));
        doc.add(new AreaBreak());
        doc.add(new Paragraph("Hello World!"));

        doc.close();
    }


    private static class SeascapeEventHandler implements IEventHandler {
        @Override
        public void handleEvent(Event currentEvent) {
            PdfDocumentEvent documentEvent = (PdfDocumentEvent) currentEvent;
            documentEvent.getPage().setRotation(270);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Events;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Events
{
    public class Seascape
    {
        public static readonly String DEST = "results/sandbox/events/seascape.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new Seascape().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, new SeascapeEventHandler());

            for (int i = 0; i < 50; i++)
            {
                doc.Add(new Paragraph("Hello World!"));
            }

            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("Hello World!"));
            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("Hello World!"));

            doc.Close();
        }

        private class SeascapeEventHandler : IEventHandler
        {
            public void HandleEvent(Event currentEvent)
            {
                PdfDocumentEvent documentEvent = (PdfDocumentEvent) currentEvent;
                documentEvent.GetPage().SetRotation(270);
            }
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.