Home > Net >  How to add page numbers with their own overlay background to PDF using Ghostscript?
How to add page numbers with their own overlay background to PDF using Ghostscript?

Time:04-26

I want to find a way how to add overlay page numbers on PDF pages with motley background. The problem is that if I use a usual approach like this:

(pagecount.ps)

globaldict
/MyPageCount 1 put

<<
   /EndPage
   {
     exch pop 0 eq dup
     {
       /Helvetica 12 selectfont
       MyPageCount =string
       cvs dup stringwidth pop
       currentpagedevice

       /PageSize get 0 get exch
       sub 460 sub 810
       moveto show
       globaldict

       /MyPageCount MyPageCount 1 add put
     } if
   } bind
>> setpagedevice

Then gs -dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -o output.pdf -sDEVICE=pdfwrite -f pagecount.ps input.pdf

It works but page numbers previously seen on blank pages are hardly visible now due to motley background stuff on pages.

So I want some little white substrate to be drawn around numbers to obscure the area they occupy on a page but with the numbers themselves being visible.

One idea was to use annotations with \Rect:

(pagecount.ps, originally taken from enter image description here

globaldict
/MyPageCount 16 put

<<
   /EndPage
   {
     exch pop 0 eq dup
     {

/Helvetica 12 selectfont

MyPageCount =string
       cvs
  dup stringwidth pop
       currentpagedevice


       /PageSize get 0 get exch
       sub 460 sub 710


       moveto                              % move to text drawing position

       % lines new to pagecount.ps

       dup                                 % duplicate string on the stack


       true charpath flattenpath pathbbox  % consume the string and put coordinates of bounding box to stack


       newpath                             % start drawing bounding box
       3 index 3 index moveto              % copy llx and lly to the top of stack and move to them
       3 index 1 index lineto              % copy llx and ury to the top of stack and draw line to them
       1 index 1 index lineto              % copy urx and ury to the top of stack and draw line to them
       1 index 3 index lineto              % copy urx and lly to the top of stack and draw line to them
       3 index 3 index lineto              % copy llx and lly to the top of stack and draw line to them
       closepath

                            % remove coordinates of bounding box from stack

       gsave

       1 0 0 setrgbcolor


       fill grestore 1 0 0 setrgbcolor
 5 setlinewidth stroke
pop pop pop pop

       0 0 0 setrgbcolor


dup stringwidth pop
       currentpagedevice

       /PageSize get 0 get exch
       sub 460 sub 710
       moveto



       % end of new lines


 show

       globaldict

       /MyPageCount MyPageCount 1 add put
     } if
   } bind
>> setpagedevice

The second one is a numbering scheme where numbers have auxilliary text and it all has obscuring background.

See: enter image description here

userdict begin
%%EndProlog

%           
  • Related