Roland's homepage

My random knot in the Web

Adding an overlay to each page of a pdf file

We can use PostScript’s Endpage procedure to e.g. add a signature to each page of a PDF file, using only ghostscript.

In contrast to the earlier article using TeX, this method is suitable for a small annotation that is identical for every page.

This definition of EndPage will be put in a separate file that precedes our input files.

Note that both BeginPage and Endpage should be defined according to Adobe’s conventions. And these routines should consume their parameters from the stack. Furthermore, EndPage should return true if the page is to be output and false otherwise.

Whatever is drawn to the page during EndPage is by definition the last thing added to the page, so it appears on “top” of everything else. You could use BeginPage to make things show up “under” the rest of the content.

<<
/BeginPage {
    /count exch def % of previous showpage calls for this device
} bind
/EndPage {
    % Get the parameters from the stack.
    /code exch def % 0=showpage, 1=copypage, 2=device deactivation
    /count exch def % of previous showpage calls for this device
    % Make the signature/overlay
    /Courier findfont
    12 scalefont
    setfont
    0 setgray
    45 110 moveto
    (Hello there!) show
    % return (output=) true only for showpage.
    code 0 eq
} bind
>> setpagedevice

Let’s call this file overlay.ps.

Let’s try this with the following file foo.pdf:

original ‘foo.pdf’

We run ghostscript like this:

gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=result.pdf \
overlay.ps foo.pdf

Note that overlay.ps appears before foo.pdf in the invocation of ghostscript! The result.pdf file looks like this:

original ‘result.pdf’

For comments, please send me an e-mail.


Related articles


←  Transplanting noscript settings and bookmarks to another machine Video manipulation with ffmpeg  →