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

Dear Monks,

I am using Image::Magick to annotate some PDFs.

use strict; use warnings; use Image::Magick; my $pdf = Image::Magick->new; $pdf->Read("foo.pdf"); $pdf->Annotate( font => 'Times', pointsize => 12, fill => 'red', text => "Wahoo!", undercolor => 'white', x => 40, y => 20 ); $pdf->Write("bar.pdf");

This works fine, and places a nice red "Wahoo!" in the upper left corner of my PDFs. The problem is that ImageMagick seems rasterize the entire PDF in order to do this, and the resulting PDF is of very poor resolution. I can fix this by calling $pdf->Resample, but the main problem is that the rasterized PDF loses all of its text data, making it impossible to search in or edit. So I need a way to add text to PDFs quickly, but preserve the text data (and preferably not murder the resolution.)

Thanks!

friedo

Replies are listed 'Best First'.
Re: Better way to add text to PDFs?
by holli (Abbot) on Jul 14, 2005 at 16:38 UTC
    PDF::API2 has a HUGE object model that is surely capable of adding text. However you might be better off with PDF::Reuse wich has a clear API and can do that too (look for the prFont and prAdd methods).

    I'd provide you some code, but i gotta leave office now.


    holli, /regexed monk/
Re: Better way to add text to PDFs?
by shiza (Hermit) on Jul 14, 2005 at 16:33 UTC
    Have you thought about using a module built specifically for PDF manipulation like PDF::API2?

    There are a lot of other modules on CPAN as well: PDF
Re: Better way to add text to PDFs?
by merlyn (Sage) on Jul 14, 2005 at 16:36 UTC
      Thanks for the suggestion, merlyn. Unfortunately the docs for PDF::API2 are a bit sparse. Here is what I'm trying:

      my $pdf = PDF::API2->open($file); my $f1 = $pdf->corefont('Times', -encode => 'latin1' ); my $ppage = $pdf->openpage(1); my $gfx = $ppage->gfx; $gfx->textlabel(15, 15, $f1, 12, "Wahoo!", -color => 'red'); $pdf->saveas("/tmp/foo.pdf");

      Unfortunately the new PDF looks exactly the same (the annotation does not appear.) I looked in the examples directory of the PDF::API2 distribution but, like the docs, they seem to assume that the user has an intricate knowledge of how PDFs work, which I don't. Thanks for any additional pointers.

Re: Better way to add text to PDFs?
by traveler (Parson) on Jul 14, 2005 at 19:31 UTC
    BigLug wrote a tutorial for PDF::API2 at this site. I used those ideas to do just what you want.

    HTH

      I found that tutorial after some googling also. I tried picking apart the pieces and came up with the following:

      my $pdf = PDF::API2->open($file); my $pdpage = $pdf->openpage(1); my $text = $pdpage->text; $text->font( $pdf->corefont('Times', -encoding => 'latin1'), 12 ); $text->fillcolor('red'); $text->text("Wahoo!"); $pdf->saveas("/tmp/foo123.pdf"); $pdf->end;

      Again, the result is dissapointing. The output PDF appears totally unchanged from the original. :(

        Your code works for me .. remember from my tutorial that the origin is the lower left, so look there for your text.

        If you want it anywhere else, then $text->translate($x,$y) before you set your text (in your code, after the fillcolor.

        If that doesn't work for some strange reason, then let me know what version of PDF::API2 you're using, and any other relevant information.

        Cheers!
        Rick Measham

Re: Better way to add text to PDFs?
by TheStudent (Scribe) on Jul 14, 2005 at 19:13 UTC

    While not a perl solution, you may want to take a look at pdftk

    From the webpage:

    If PDF is electronic paper, then pdftk is an electronic staple-remover, hole-punch, binder, secret-decoder-ring, and X-Ray-glasses. Pdftk is a command-line tool for doing everyday things with PDF documents. Keep one in the top drawer of your desktop and use it to:

    • Merge PDF Documents
    • Split PDF Pages into a New Document
    • Decrypt Input as Necessary (Password Required)
    • Encrypt Output as Desired
    • Fill PDF Forms with FDF Data and/or Flatten Forms
    • Apply a Background Watermark
    • Report on PDF Metrics such as Metadata, Bookmarks, and Page Labels
    • Update PDF Metadata
    • Attach Files to PDF Pages or the PDF Document
    • Unpack PDF Attachments
    • Burst a PDF Document into Single Pages
    • Uncompress and Re-Compress Page Streams
    • Repair Corrupted PDF (Where Possible)

    Pdftk allows you to manipulate PDF easily and freely. It does not require Acrobat, and it runs on Windows, Linux, Mac OS X, FreeBSD and Solaris.

    Pdftk is free software (GPL).

    TheStudent