kusanagi
kusanagi

Reputation: 14614

How add watermark to ghostscript with command line?

Is any way to add watermark on each pdf with command line? because I need set up params from code

'gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="7ed753c56994067cb0c8dc18fbf14921.pdf" "b79d2282c15b7e824cb8ee400401161d.pdf" "f21958c0b3a4a01fe22c9a60b6e15121.pdf" "d46615b5dd7b6e6565ef1ce8b117b860.pdf" "f46ea9512f5763693c84d8061eeff742.pdf"'

I just need setup path to watermark, opacity, position x,y and width\height

Upvotes: 4

Views: 6702

Answers (2)

Nestor Urquiza
Nestor Urquiza

Reputation: 3027

If you can afford using just text instead of an image for watermarking, then you can simulate watermark with 'setlinewidth stroke'.

Just create a mark.ps file like below:

<<
   /EndPage
   {
     2 eq { pop false }
     {
         gsave      
         /Helvetica findfont 48 scalefont setfont
         newpath
         .90 setgray 130 70 moveto 50 rotate
         (${watermark}) false  charpath
         1 setlinewidth stroke
         grestore
         true
     } ifelse
   } bind
>> setpagedevice

And then run:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=watermarked.pdf mark.ps original.pdf

I have made this and other features like password protection, search, split and merge available from a plain old bash (POB) open source CLI project in github called pdf-bash-tools.

Upvotes: 7

KenS
KenS

Reputation: 31159

The only way to add content to each page is going to be to use PostScript to do it for you. Adding a custom EndPage procedure will allow you to place marks on the rendered page bitmap (or in the case of pdfwrite, append to the page content stream) immediately before the page is finalised.

PostScript does not support transparency, so you cannot immediately use PostScript to achieve 'opacity'. However, Ghostscript does support the pdfmark operator, and for PDF output only it is possible to use this.

So what you need to do is write an EndPage procedure which uses a pdfmark to draw an annotation on the page. The appearance stream for the annotation will contain the drawing operations for the 'watermark'. If you want the appearance to involve transparency you may well need to add a transparency group but that can be done with a pdfmark as well.

Upvotes: 0

Related Questions