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

Tags: page sizepage rotationinspect PDFiText 7

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.