in reply to Editing/Replacing Text in a PDF

As suggested by others, CAM::PDF is probably the better option for what you want to do (you should be able to do it with PDF::API2, too, but it's likely somewhat more involved...). CAM::PDF lets you easily edit content streams and stuff, and, most importantly, takes care of everything you don't want to do manually, like adjusting the object crossreference table after having modified an object's size (as explained by Russ), (un)compressing the streams, etc.

Let's say you have the text "some placeholder text" (among other things) written on page 1 at some position, and you want to replace that text. In that case you could try:

use CAM::PDF; my $pdf = CAM::PDF->new('Test1.pdf'); # existing document my $page = $pdf->getPageContent(1); # $page now holds the uncompressed page content as a string # replace the text part $page =~ s/some placeholder text/my new text/; $pdf->setPageContent(1, $page); $pdf->cleanoutput('Test2.pdf');

The page content you get is in raw PDF syntax, i.e. various PDF operators like BT, ET, Tf, Tm, TJ, etc. together with their parameters. Consult Adobe's PDF Reference Document for what they do, their syntax, and so on.

Literal text strings are written in parentheses (like in PostScript), so that's what you have to look for. Things may be complicated somewhat by the fact that some PDF generators are splitting up text in order to position individual substrings, to achieve custom word/letter spacing (i.e. differing from the font's defaults), kerning, etc... In the worst case, you'll find characters individually wrapped in parentheses.
Well, just give it a try... it might not be as bad after all.

Good luck!

Replies are listed 'Best First'.
Re^2: Editing/Replacing Text in a PDF
by ikkon (Monk) on Dec 21, 2006 at 11:42 UTC
    I don't think this is the first time you have helped me out alot, I appreciate your patience with me, this deffently put me on track where i wanted to be, but i think i will eventually learn more about what Russ was talking about again thanks i do appreciate it.