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!


In reply to Re: Editing/Replacing Text in a PDF by almut
in thread Editing/Replacing Text in a PDF by ikkon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.