Stamp PDF file with control for position of stamp

2019-02-02 00:39发布

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?

1条回答
欢心
2楼-- · 2019-02-02 01:09

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.)

查看更多
登录 后发表回答