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, named getPageSize() and getPageSizeWithRotation().

Let's take a look at an example:

PdfReader reader =
    new PdfReader("src/main/resources/pages.pdf");

In this example, the show() method looks like this:

public static void show(Rectangle rect) {
    System.out.print("llx: ");
    System.out.print(", lly: ");
    System.out.print(", urx: ");
    System.out.print(", lly: ");
    System.out.print(", rotation: ");

This is the output:

llx: 0.0, lly: 0.0, urx: 595.0, lly: 842.0, rotation: 0
llx: 0.0, lly: 0.0, urx: 595.0, lly: 842.0, rotation: 0
llx: 0.0, lly: 0.0, urx: 842.0, lly: 595.0, rotation: 90
llx: 0.0, lly: 0.0, urx: 842.0, lly: 595.0, rotation: 0
llx: 0.0, lly: 0.0, urx: 842.0, lly: 595.0, rotation: 0

Page 3 (see line 4 in code sample 3.8) is an A4 page just like page 1, but it's oriented in landscape. The /MediaBox entry is identical to the one used for the first page [0 0 595 842], and that's why getPageSize() returns the same result.

The page is in landscape, because the \Rotate entry in the page dictionary is set to 90. Possible values for this entry are 0 (which is the default value if the entry is missing), 90, 180 and 270.

The getPageSizeWithRotation() method takes this value into account. It swaps the width and the height so that you're aware of the difference. It also gives you the value of the /Rotate entry.

Page 4 also has a landscape orientation, but in this case, the rotation is mimicked by adapting the /MediaBox entry. In this case the value of the /MediaBox is [0 0 842 595] and if there's a /Rotate entry, its value is 0.

That explains why the output of the getPageSizeWithRotation() method is identical to the output of the getPageSize() method.

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

The major mistake you are making can be found in this line:

document = PdfStamper.getOverContent(i).getPdfDocument();

This line doesn't make sense. You should not use the internal PdfDocument instance. If you want to know the size and orientation of a page, you should ask PdfReader for those values.

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