A couple of related questions: How do I get the page orientation of a PDF page? and How to get character offset information from a pdf document?
Starting with the solution for the last question, I came up with this recipe:
use CAM::PDF; my $pdf = CAM::PDF->new('my.pdf') or die $CAM::PDF::errstr; for my $pagenum (1 .. $pdf->numPages) { my $pagetree = $pdf->getPageContentTree($pagenum) or next; my @text = $pagetree->traverse('MyRenderer')->getTextBlocks; for my $textblock (@text) { print "text '$textblock->{str}' at ", "($textblock->{left},$textblock->{bottom}), angle $textblock->{angle}\n"; } } package MyRenderer; use base 'CAM::PDF::GS'; sub new { my ($pkg, @args) = @_; my $self = $pkg->SUPER::new(@args); $self->{refs}->{text} = []; return $self; } sub getTextBlocks { my ($self) = @_; return @{$self->{refs}->{text}}; } sub renderText { my ($self, $string, $width) = @_; my ($x, $y) = $self->textToDevice(0,0); my ($x1, $y1) = $self->textToDevice(1,0); push @{$self->{refs}->{text}}, { str => $string, left => $x, bottom => $y, angle => atan2($y1-$y, $x1-$x), }; return; }
which gave this result for page 565 of PDFReference15_v5.pdf:
text 'ab' at (371.324,583.7249), angle -1.5707963267949 text 'c' at (371.324,576.63365), angle -1.5707963267949
Please note that the angle is in radians. Divide by Pi and multiply by 180 to convert this to degrees. Thus, -1.5707963267949 is 270 degrees, which corresponds to p. 565.
Please note that the angle at which the angle is plotted is the angle relative to the contents of the page. If the page itself is rotated (in accordance with the question of page orientation above), you may want to combine rotation calculations.