Stamp PDF file with control for position of stamp file
Asked Answered
S

1

14

Does anyone know about stamping PDF file to PDF file and also controls for positioning PDF file stamp?

I have a file orginal.pdf and logo.pdf. I want to stamp logo.pdf file to file orginal.pdf at the top left of file original.pdf. How can it be done with Ghostscript or pdftk?

Southpaw answered 12/12, 2011 at 6:49 Comment(0)
M
35

It can be done with Ghostscript plus pdftk, but it requires at least 2 different steps.

AFAIK, you cannot directly control pdftk's stamp placement. By default it puts the stamp on the center of the page, and at the same time does a 'scale-to-fit' operation.

So, you have to fix your stamp first so it is placed on an empty page first, right at the position you want it. This you can achieve with the help of Ghostscript in a first step. Then, in a second step, use pdftk to merge the two files.

Let's use an example.

First: Create a 'stamp-small.pdf'. (You have yours already; I need one to demonstrate the principle.)

gs \
  -o stamp-small.pdf \
  -sDEVICE=pdfwrite \
  -g3200x500 \
  -c "/Helvetica-Bold findfont 36 scalefont setfont" \
  -c "0 .8 0 0 setcmykcolor" \
  -c "12 12 moveto" \
  -c "(This is my stamp) show" \
  -c "showpage"

This example was for Linux or Mac OS X. On Windows you would modify it like this:

gswin32c.exe ^
  -o stamp-small.pdf ^
  -sDEVICE=pdfwrite ^
  -g3200x500 ^
  -c "/Helvetica-Bold findfont 36 scalefont setfont" ^
  -c "0 .8 0 0 setcmykcolor" ^
  -c "12 12 moveto" ^
  -c "(This is my stamp) show" ^
  -c "showpage"

(You could also put it all in one line, but then skip the line-continuation marks for the respective OS.) This first command hands a series of simple PostScript statements to Ghostscript's commandline and tells it to create a small PDF page with a dimension of 320x50 pts. This should simulate your 'small' stamp which you seek placement for.

Second: Create a full-page (in my case, an A4-sized) PDF file which can be applied in the third step as the real stamp:

gs \
  -o A4-stamp.pdf \
  -sDEVICE=pdfwrite \
  -g5950x8420 \
  -c "<</PageOffset [280 790]>> setpagedevice" \
  -f stamp-small.pdf 

On Windows:

gswin32c.exe ^
  -o A4-stamp.pdf ^
  -sDEVICE=pdfwrite ^
  -g5950x8420 ^
  -c "<</PageOffset [280 790]>> setpagedevice" ^
  -f stamp-small.pdf 

This command achieved several things:

  1. It took the initially created 'stamp-small.pdf' as input.
  2. It used a canvas of 595x842 pts (that is ISO A4 page size).
  3. It applies a small PostScript command to shift the input content by 280 pts to the right and 790 pts to the top (PostScript and PDF coordinates start counting from the lower left corner).
  4. It creates 'A4-stamp.pdf' as output.

Effectively, my original small-sized stamp string is now in the top right corner of an A4 page.

Third: Now you can apply this new 'stamp' to your original PDF file using pdftk:

pdftk original.pdf stamp A4-stamp.pdf output stamped.pdf

or, to stamp all pages of a multi-page original PDF:

pdftk original.pdf multistamp A4-stamp.pdf output stamped.pdf

This example should give you enough of an idea about how to develop a similar procedure for your own logo.pdf as I did for my stamp-small.pdf. (I did it for the top right corner, you want yours for the top left corner.)

Melanoma answered 19/12, 2011 at 19:48 Comment(5)
Thank's For u'r Answer before. This Problem done with script PHP use FPDI.Southpaw
Don't know why this answer did not get tagged -- it totally solved my problem of how to place stamps reliably on a A4 page. Great explanations!Dewar
Yep, great answered, no clue why the OP didn't have the decency to accept.Aileenailene
This is an awesome answer.Mongolism
When you already have your stamp as a PDF, but not positioned right, using pdfjam to create the overlay PDF can be slightly easier to grasp. It has options such as --scale and --offset. I placed a PNG on my PDF using convert, pdfjam and pdftk stamp.Selfconsistent

© 2022 - 2024 — McMap. All rights reserved.