Why is the page size of a PDF always the same, no matter if it's landscape or portrait?

I have a PdfReader instance that contains some pages in landscape mode and other page in portrait. I need to distinguish them to perform some operations... However, if I call the getPageSize(), the value is always the same. Why isn't the value different for a page in landscape ? I've tried to find other methods to retrieve page width / orientation but nothing worked.

Here is my code :


for(int i = 0; i  pdfreader.getNumberOfPages(); i++) {
    document = PdfStamper.getOverContent(i).getPdfDocument();
    document.getPageSize().getWidth; //this will always be the same
Posted on StackOverflow on Apr 28, 2014 by Jean-François Savard

There are two convenience methods in iText 7, named getPageSize() and getPageSizeWithRotation().

Let's take a look at an example:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
System.out.println("Feel the difference:");

We've created a PdfDocument instance from the src document with two pages: both with standard A4 size, but the second one has a landscape rotation. The show() method looks like this:

public static void show(Rectangle rect) {
    System.out.print("x: ");
    System.out.print(", y: ");
    System.out.print(", width: ");
    System.out.print(", height: ");

This is the output:

x: 0.0, y: 0.0, width: 595.0, height: 842.0
x: 0.0, y: 0.0, width: 595.0, height: 842.0
x: 0.0, y: 0.0, width: 595.0, height: 842.0
Feel the difference:
x: 0.0, y: 0.0, width: 842.0, height: 595.0

The /MediaBox entry is identical to both of the pages: [0 0 595 842], and that's why getPageSize() returns the same result.

The getPageSizeWithRotation() method takes the rotation value into account. It swaps the width and the height so that you're aware of the difference.

When I read your question, I see that you are looking for the rotation. This can be done with the getRotation() method:

int rotation = pdfDoc.getPage(2).getRotation()

It returns 90 for the second page.

So if you want to know the size and orientation of a page, you should ask PdfDocument for those values.

Click this link if you want to see how to answer this question in iText 5.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

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