Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Bates Number in PDF

by snoopy (Deacon)
on Jun 18, 2009 at 01:57 UTC ( #772612=note: print w/replies, xml ) Need Help??

in reply to Bates Number in PDF

One possibility is to use the approach shown in Re: PDF page numbering as a starting point.

This example is over-printing text (in this case page numbers) using PDF::API2.

Note also that you can adjust the page size by changing the scale argument to formimage E.g. to shrink page to 90% of full size:

$gfx->formimage($xo, # page overlay 0, 0, # x y .9); # scale to 90% of full size

Replies are listed 'Best First'.
Re^2: Bates Number in PDF
by emike (Novice) on Jun 18, 2009 at 02:40 UTC
    Thanks to a tip from Friar snoopy, I have a working Bates Number script. I got some interesting results when I ran this on the PDF specs at I'll post that next.
    #!/usr/local/bin/perl $| = 1; # flush output use warnings; use strict; use PDF::API2; use POSIX qw(floor); use Smart::Comments; my $diemsg = setDieMsg(); my $batesStartStr = shift (@ARGV); die $diemsg.'(batesStartStr)' if !d +efined $batesStartStr; my $batesEndStr = shift (@ARGV) ; die $diemsg.'(batesEndStr)' if !defi +ned $batesEndStr; my $batesNum = shift (@ARGV) ; die $diemsg.'(batesNum)' if !defined $b +atesNum; my $scale = shift (@ARGV) ; die $diemsg.'(scale)' if !defined $scale; my $adjust = shift (@ARGV) ; die $diemsg.'(adjust)' if !defined $adjus +t; my $infile = shift (@ARGV) or die $diemsg.'(infile)'; my $outfile = shift (@ARGV) || $infile.'bates.pdf'; my $pdf_in = PDF::API2->open($infile) or die "can't PDF::API2->open($infile): $!"; my $pdf_out = PDF::API2->new(-file => $outfile); ; my $pageCnt = $pdf_in->pages; print "PDF Document $infile $pageCnt pages are now read into memory!\n +" if $pageCnt >100; # some globals my (@mbox, $left_edge, $bottom, $right_edge, $top, $new_left, $new_bot +tom); my ($page_out, $page_in, $gfx, $xo, $txt, $font, $bates); foreach my $pagenum (1 .. $pdf_in->pages) { ### Writing===[%] done # # create new page # $page_out = $pdf_out->page(0); { $page_in = $pdf_in->openpage($pagenum); # # Get the page size # @mbox = $page_in->get_mediabox; # # Inherit mediabox $page_out->mediabox(@mbox); ($left_edge, $bottom, $right_edge, $top) = @mbox; # # copy page as a form. # $gfx = $page_out->gfx; $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum); # move the scaled down image up to make room for Bates number at b +ottom $new_left = 0; $new_bottom = floor ((1 + $top - $bottom) * (1-$scale)); $gfx->formimage($xo, $new_left, $new_bottom, # x y $scale); # scale $txt = $page_out->text; $txt->strokecolor('#000000'); $txt->fillcolor('#000000'); # print Bates string at bottom of new page, just left of center $txt->translate(my $_x = ($right_edge - $left_edge) / 2 - 10, my $_y = $adjust ); $font = $pdf_out->corefont('Courier'); $txt->font($font, 12); $bates = $batesStartStr.$batesNum++.$batesEndStr; $txt->text( $bates.$pagenum ); # flush the page to save memory on big PDFs $pdf_out->finishobjects($page_out, $gfx, $txt); } } # finish up and exit $pdf_out->save; exit; sub setDieMsg { return <<"DIE"; usage $0: batesStartStr batesEndStr batesNum scale adjust infile <outf +ile> ex: $0 'Kramer v. Kramer-' '-NY2009' 42 .95 old.pdf new.pdf would Bates stamp the first page of old.pdf w/ the string "Kramer v. + Kramer-42-NY2009, increment the page number on next page(s), and output all of the pag +es to new.pdf DIE }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://772612]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-06-21 12:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.