Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: PDF page numbering

by snoopy (Deacon)
on Mar 26, 2009 at 06:48 UTC ( #753355=note: print w/ replies, xml ) Need Help??


in reply to PDF page numbering

Here's a possible solution using PDF::API2. This adds a page number centered along the top of the page.

When overprinting on an existing PDF, I tend to convert the input pages to a PDF form using the importPageIntoForm and formimage methods.

#!/usr/bin/perl use warnings; use strict; use PDF::API2; my $infile = shift (@ARGV) or die "usage $0: infile outfile"; my $outfile = shift (@ARGV) || $infile.'paginated'; my $pdf_in = PDF::API2->open($infile); my $pdf_out = PDF::API2->new; foreach my $pagenum (1 .. $pdf_in->pages) { my $page_in = $pdf_in->openpage($pagenum); # # create new page # my $page_out = $pdf_out->page(0); # # Get the page size # my @mbox = $page_in->get_mediabox; # # Inherit mediabox $page_out->mediabox(@mbox); # # copy page as a form. # my $gfx = $page_out->gfx; my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum); $gfx->formimage($xo, 0, 0, # x y 1); # scale my $left_edge = $mbox[0]; my $right_edge = $mbox[2]; my $top = $mbox[3]; my $txt = $page_out->text; $txt->strokecolor('#000000'); $txt->fillcolor('#000000'); $txt->translate(my $_x = ($right_edge - $left_edge) / 2 - 10, my $_y = $top - 15 ); my $font = $pdf_out->corefont('Courier'); $txt->font($font, 12); $txt->text( 'Page: '.$pagenum ); } $pdf_out->saveas($outfile);


Comment on Re: PDF page numbering
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://753355]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-11-23 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls