13

有谁知道将 PDF 文件标记为 PDF 文件以及用于定位 PDF 文件标记的控件?

我有一个文件orginal.pdflogo.pdf. 我想在文件的左上角标记logo.pdf文件到文件。如何使用Ghostscriptpdftk完成?orginal.pdforiginal.pdf

4

1 回答 1

32

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

于 2011-12-19T19:48:00.783 回答