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

In reply to Re: PDF page numbering by snoopy
in thread PDF page numbering by srikrishnan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.