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);
|