Adding to Existing PDF Files
Each PDF::API2 object represents a top-level PDF document. The
document can be created from scratch by adding pages and drawing on
them by adding Gfx or Text objects, or it can be read from an existing
PDF file. To manipulate an existing file, use the open( )
method when creating the top-level
PDF object.
Example 12-4 opens an
existing PDF file (with the name provided when the script is invoked)
and adds a rotated “Confidential” stamp to each page. Once the
document is successfully opened, a new font resource is added first,
in case the existing document does not use the Helvetica font. Then,
each page is accessed in turn by calling the openpage( )
method, which returns a
reference to the Page object for a particular existing page. Next, the
stamp is added to the page with a Text object. The text is moved into
place and rotated using the transform(
)
method, as more than one transformation is being performed
on the same object. The result is written back to the original
document with the update( )
method.
#!/usr/bin/perl -w # # Example 12-4. Add a stamp to an existing document. use strict; use PDF::API2; # Used to generate the PDF output unless (defined($ARGV[0])) { die "Please provide a filename.\n"; } # Open the PDF file whose name was provided on the command line my $pdf = PDF::API2->open($ARGV[0]) or die "Couldn't open $ARGV[0]!\n"; # Initialize the font used to create the stamp my $font = $pdf->corefont('Helvetica'); ...
Get Perl Graphics Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.