R – How to get the page orientation of a PDF page

pdfperl

The function getPageDimensions (of CAM::PDF) returns same values for both portrait and landscape pages.
How can I identify the orientation of a PDF page?
I am using CAM::PDF Perl library and would like to know how to do this using this library.
But any other means to identify this is also welcome (preferably using a Perl lib).

Thanks.

Best Answer

I'm the author of CAM::PDF.

Well, there's two parts to this. One is the page's dimensions, as you noted. That works as expected: I used Apple's Preview.app to rotate a PDF file and ran these two command lines:

perl -MCAM::PDF -le'print "@{[CAM::PDF->new(shift)->getPageDimensions(1)]}"' orig.pdf 
0 0 612 792
perl -MCAM::PDF -le'print "@{[CAM::PDF->new(shift)->getPageDimensions(1)]}"' rotated.pdf 
0 0 792 612

But there's also the `/Rotate' page attribute. The argument is a number of degrees (default 0, but 90 or 270 are not uncommon). Like page dimensions, it's an inheritable property so you have to navigate to parent pages. Here's a quick-and-dirty command line tool to output the rotation value:

use CAM::PDF;
my $filename = shift || die;
my $pagenum = shift || die;
my $pdf = CAM::PDF->new($filename) || die;
my $pagedict = $pdf->getPage($pagenum);
my $rotate = 0;
while ($pagedict) {
   $rotate = $pdf->getValue($pagedict->{Rotate});
   if (defined $rotate) {
      last;
   }
   my $parent = $pagedict->{Parent};
   $pagedict = $parent && $pdf->getValue($parent);
}
print "/Rotate $rotate\n";
Related Topic