Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Bates Number in PDF

by emike (Novice)
on Jun 18, 2009 at 01:02 UTC ( [id://772607]=perlquestion: print w/replies, xml ) Need Help??

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

I'd like to write some Perl code to insert 'Bates numbers' into legal documents that are PDF'd. (If you don't know what Bates numbers are, see the Wikipedia entry. See below for quick summary) I'll probably end up using either PDF::API2 or PDF::Reuse. PDF::Reuse has a feature to insert a stamp or water mark, but that is not quite what I want. What I would like to do is shrink the original page by a user-defined amount (zoom=.95 e.g.) and then insert the user-specified Bates Number. The Bates number is typically composed of some ASCII text concatenated with an incremented sequence number per page. (Just assume an arbitrary string for now). My trouble is, aside from poor documentation of PDF::API2, is I don't see a magnify (or zoom) feature for a page. PDF::Reuse does magnify (or shrink, as well as rotate) the watermark or stamp before appying it to a page. I suppose I will end up reverse engineering that for the magnify of PDF, which seems missing as a standalone function...So what I would do is: 1) shrink the PDF page, 2) insert a string at the bottom of the page that does not overlap the existing PDF. 3) Write the page out.

Replies are listed 'Best First'.
Re: Bates Number in PDF
by snoopy (Curate) on Jun 18, 2009 at 01:57 UTC
    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
      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 adobe.com. 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 }
      mike

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://772607]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found