#!/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 !defined $batesStartStr; my $batesEndStr = shift (@ARGV) ; die $diemsg.'(batesEndStr)' if !defined $batesEndStr; my $batesNum = shift (@ARGV) ; die $diemsg.'(batesNum)' if !defined $batesNum; my $scale = shift (@ARGV) ; die $diemsg.'(scale)' if !defined $scale; my $adjust = shift (@ARGV) ; die $diemsg.'(adjust)' if !defined $adjust; 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_bottom); 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 bottom $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 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 pages to new.pdf DIE }