I have a PDFReader which contains some page in landscape mode and other in portrait.
I need to distinct them to do some treatment... However, if I call the getOrientation or getPageSize, the value is always the same (595 for pagesize and 0 for orientation).
Why isn't the value different for a page in landscape ?
I've tryied 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
}
Thanks !
There are two convenience methods, named
getPageSize()
andgetPageSizeWithRotation()
.Let's take a look at an example:
In this example, the
show()
method looks like this:This is the output:
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 whygetPageSize()
returns the same result.The page is in landscape, because the
\Rotate
entry in the page dictionary is set to90
. Possible values for this entry are0
(which is the default value if the entry is missing),90
,180
and270
.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 is0
.That explains why the output of the
getPageSizeWithRotation()
method is identical to the output of thegetPageSize()
method.When I read your question, I see that you are looking for the rotation. This can be done with the
getRotation()
method.Remark: This text is copied from my book "The ABC of PDF" (the book is under construction; you can download the first chapters for free). The code sample can be found here.
Fix :
use
instead of