Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Add watermark to a pdf

by holli (Abbot)
on Dec 28, 2004 at 08:14 UTC ( [id://417718]=perlquestion: print w/replies, xml ) Need Help??

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

greetings monks.

my current objective is to add a watermark image (with the text "COPY") into the background of existing pdf-files.

i managed to add an image to existing files using pdf::reuse but it ended up beeing on an new page.

iŽd be glad if anyone could jog me into the right direction.

tanks thanks in advance

Replies are listed 'Best First'.
Re: Add watermark to a pdf
by steves (Curate) on Dec 28, 2004 at 13:33 UTC

    It looks to me like you have to add a form to each page of the existing document; i.e., I believe background images in PDF are really what they call forms. I'm not exactly sure how to do that from the examples though. It might be worth posing this question to the author of the module. That seems like a good example to have in the module tutorials/examples if it can be done.

    I would probably start by trying to get to each page in the existing document individually, versus adding something to the entire document stream which seems to be what your example is doing.

      thank you for that "form"-hint. that was what i needed. i finally assembled a script which does perfectly what i wanted to achieve:

      i know that the string concatenation ($string) is a bit clumsy, but i wanted to add the comments. you cannot have comments in quotes, can you?

      use strict; use PDF::API2; use PDF::Reuse; # specify files my $ifile = "c:/kopie/anlage-h-beg.pdf"; my $ofile = "c:/kopie/new.pdf"; #find out number of pages my $ipdf = PDF::API2->open($ifile); my $nop = $ipdf->pages; $ipdf->end(); #set outfile for PDF::Reuse prFile($ofile); #get font my $intName = prFont("Helvetica"); # assemble watermark text my $string = # save graphic state "q\n" . # Begin Text "BT\n" . # set font and "size" "/$intName 1 Tf\n" . # set a text matrix # 1. width of text # 2. rotation angle (counterclockwise) # 3. inclination (clockwise) # 4. height of text # 5. position x (0=left) # 6. position y (0=bottom) "180 60 -60 180 100 185 Tm\n" . #set rgb-color (values between 0 and 1) #"1 0 0 rg" . #set grayscale (white=1 black=0) "0.9 g\n" . # show text "(Kopie) Tj\n" . # End Text "ET\n" . "Q\n"; #loop over pages for (my $i=1;$i<=$nop;$i++) { #add watermark prAdd ($string); #add original content my $internalName = prForm ( { file => $ifile, page => $i, effect => 'print', tolerant => 'no' } ); #add pagebreak prPage; } #save and close prEnd();
        To be a true watermark I would like the text to be on top but have an opacity value that would allow you to see the original document underneath. Does anyone know how to do this? Plus I would like to set the prMbox value based on the original pdf page

        This method of creating a watermark works well except that there are certain things that seem to screw up...

        Images currently inside document will be opaque and overtop of your watermark. Sometimes, the first page of a .pdf is totally blank, aside from the newly added watermark.

        Would anyone have a suggestion as to why this is?

        but this only creates empty pages with my watermark....
Re: Add watermark to a pdf
by steves (Curate) on Dec 28, 2004 at 12:35 UTC

    Can you show us the code you wrote using PDF::Reuse? It looks like the prAdd method will allow images to be added to existing pages.

    You may also need to use something like ghostscript for that level of PDF manipulation. There is a GSAPI module at CPAN that provides Perl access to the ghostscript API, but I've never used it personally.

    There's a PDF FAQ here. Your question of how to add watermarks is covered there but they suggest using this tool, which you have to buy.

      i used (basically taken from the docs)

      use PDF::Reuse; use Image::Info qw(image_info dim); use strict; my $file = 'c:/kopie.jpg'; my $info = image_info($file); my ($width, $height) = dim($info); # Get the dimensions prFile('c:/myFile.pdf'); my $intName = prJpeg("$file", $width, $height); my $str = "q\n"; $str .= "$width 500 500 $height 100 100 cm\n"; $str .= "/$intName Do\n"; $str .= "Q\n"; prDoc('c:/anlage-h-rbskt.pdf'); prAdd($str); prEnd();


      but as i said, this will add the image to a new page. am i missing something?

      it will also be ok if i could just add text to the background.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://417718]
Approved by pelagic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found