Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in Ghostscript to add watermark to every page in PDF

I convert PDF -> many JPEG and many JPEG -> many PDF using ghostscript. I need to add watermark text on every converted JPEG (PDF) page. Is it possible using only Ghostscript and PostScript?

The only way I found:

gswin32c -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=output.pdf watermark.ps input.pdf

But this will insert watermark.ps watermark on first separate page in output.pdf.

Can I do this on output PDF pages directly?

Can I do this on output JPEG pages directly?

<<
   /BeginPage
   { gsave
       /Helvetica_Bold 120 selectfont
       .85 setgray 130 70 moveto 50 rotate (Sample) show
     grestore
   } bind
>> setpagedevice

If I use /EndPage instead of /BeginPage - it says setpagedevice is not applicable...

How to remake this script for /EndPage?

like image 409
radistao Avatar asked Sep 03 '12 06:09

radistao


2 Answers

I don't know what you mean by 'directly'. Its possible, as you have found, to have a PostScript interpreter do many kinds of things on a per-page basis. PostScript is a programming language after all.

I would suggest that the /BeginPage and/or /EndPage procedures in the page device dictionary would be the place to start. These allow you to execute arbitrary PostScript at the start or end of every page.

If you define a /BeginPage procedure then it will be executed before any marking operations from the input program, if you define a /EndPage then it will be executed after the marking operations from the input program (on a page by page basis(.

This allows you to have your own marks lie 'under' or 'over' the marks from the program.

like image 72
KenS Avatar answered Oct 12 '22 20:10

KenS


The accepted answer was inserting pages for me. The pages were blank aside from the watermark. If you run into this try adding the 2eq bit here

<<
   /EndPage
   {
     2 eq { pop false }
     {
         gsave      
         /Helvetica_Bold 120 selectfont
         .85 setgray 130 70 moveto 50 rotate (Sample) show
         grestore
         true
     } ifelse
   } bind
>> setpagedevice

I found the following site that pointed me in the correct direction

http://habjan.blogspot.com/2013/10/how-to-programmatically-add-watermark.html

Here's the calling syntax where the above file is saved as watermark.ps and gswin32c references the ghostscript exe

gswin32c -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=watermarked.pdf watermark.ps original.pdf
like image 21
Reid Evans Avatar answered Oct 12 '22 19:10

Reid Evans