in reply to Determining a PDF's orientation
Here is a script which will tell you what you want. The script opens a Pdf and then extracts each page objects MediaBox information in the order it is stored in the pdf. The actual page order is determined by the the order of the Kids entries for the Pages and is left as an exercise for you.
The R:0 is the page view rotation ( a multiple of 90 ). The pages dimensions are defined in points so a portrait definition would have the following entry /MediaBox 0 0 612 792 I hope this is not overkill for you. I know a bit too much about this because I have written a Pdf module which I have not yet released to CPAN...
Hope it helps,
JamesNC
use strict; my $fh; open $fh, "<out.pdf" or die "$!"; binmode( $fh ); my %page; my @kids; my @pages; my $root; my ( $line, $buf ); while( read($fh, $buf, 256) ){ $line .= $buf; my (@lines) = split /endobj/, $line; $line = $lines[-1]; foreach ( @lines ){ my $obj; my $pf = 0; my $cf = 0; my $parent = ""; my $rotation = 0; s/\r|\n/ /g; if ( /(\d+\s+\d+\s+obj)/ ){ $obj = $1; } if ( /Rotate\s+(\d+)/ ){ $rotation = $1; } if ( /Pages\s+/){ $pf = 1; } if ( /Parent\s+(\d+\s+\d+\s+R)/ ){ $parent = $1; } if ( /Catalog/ ){ $cf = 1; } if ( /Pages\s+(\d+\s+\d+\s+R)/ && $cf == 1 ){ push @kids, "1st Pages obj: $1"; } if ( /Kids\s+\[([^\]]+)\]/ && $pf ==1 ){ push @kids, "$obj", $1; } if ( /MediaBox\s+\[([^\]]+)\]/ ){ $page{$obj} = "$1 R:$rotation"; push @pages, $obj; } } } print "$_\n" for @kids; print "$_ $page{$_}\n" for @pages; __END__ Output: 1st Pages obj: 4 0 R <- root pages object 4 0 obj 5 0 R 8 0 R <- Kids of root pages (page 1 is obj 5 0 R ) 5 0 obj 0 0 612 792 R:0 <- ( 0 0 612 792 ) MediaBox 8 0 obj 0 0 612 792 R:0
The R:0 is the page view rotation ( a multiple of 90 ). The pages dimensions are defined in points so a portrait definition would have the following entry /MediaBox 0 0 612 792 I hope this is not overkill for you. I know a bit too much about this because I have written a Pdf module which I have not yet released to CPAN...
Hope it helps,
JamesNC
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Determining a PDF's orientation
by friedo (Prior) on Aug 22, 2005 at 07:13 UTC | |
by JamesNC (Chaplain) on Aug 22, 2005 at 21:29 UTC |
In Section
Seekers of Perl Wisdom