polarius has asked for the wisdom of the Perl Monks concerning the following question:

I have a set of a few thousand pdf files and I need to add a text at the top of each file. The pages have a set width but variable height. I can use use PDF::API2 and the textlabel method to add the text, however I need to know the page height of the documents in order to place the text at the right height so that the text is consistently at the same distance from the top of the page. Any ideas of how I can get the value of the page height?
  • Comment on How to read page dimensions of pdf file

Replies are listed 'Best First'.
Re: How to read page dimensions of pdf file
by roboticus (Chancellor) on Jul 27, 2012 at 19:28 UTC

    polarius:

    I've never done that, but I took a quick look at the PDF handling modules I have installed and found a couple candidates:

    So if you have one of those packages installed, give it a go and report back.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: How to read page dimensions of pdf file
by polarius (Initiate) on Jul 27, 2012 at 22:36 UTC

    roboticus:

    Thanks for the help. I used the Page object from PDF::API2.

    Here's my basic code in case someone else needs to use something similar:

    #!/usr/bin/perl use PDF::API2; my $filename="c:/filename.pdf"; my $pdf = PDF::API2->open("$filename"); for my $index (1 .. $pdf->pages) { my $page = $pdf->openpage($index); (my $llx, my $lly, my $urx, my $ury) = $page->get_mediabox; my $txt = $page->text; $txt->textlabel($urx-100, $ury-30, $pdf->corefont('Arial'), 12, 'a +dd text here'); } my $fileout = "c:/fileout.pdf"; $pdf->saveas("$fileout"); $pdf->end;
Re: How to read page dimensions of pdf file
by cavac (Prior) on Jul 27, 2012 at 21:15 UTC

    Here's an example where i load a PDF file, add some data from database and save it as a new PDF: 983016. For this, i use PDF::Report.

    I'm not sure how this works on very complex documents. For the rather simple ones i use for my various printing tasks, it does a nice job though.

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"