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 How to add page numbers to Postscript/PDF)
globaldict
/MyPageCount 1 put
<<
/EndPage
{
newpath [
/Rect
[ 20 dup moveto (Link on page1) false charpath pathbbox
2 add 4 1 roll 2 add 4 1 roll 2 sub 4 1 roll 2 sub 4 1 roll
newpath ]
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
But it produces only a white page.
How to make page numbers with their own little background be drawn on PDF pages using Ghostscript?
UPD:
I updated pagecount.ps
according to advices and now I have right little background in right place but page numbers stopped to get drawn (Error: /nocurrentpoint in /--.endpage--
).
New 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 % 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
pop pop pop pop % remove coordinates of bounding box from stack
gsave
1 0 0 setrgbcolor
fill
grestore
stroke
% end of new lines
show
globaldict
/MyPageCount MyPageCount 1 add put
} if
} bind
>> setpagedevice
UPD 2:
I updated pagecount.ps
to fix (Error: /nocurrentpoint in /--.endpage--
). I also removed stroke
command afted filling the rectangle.
Now I have Error: /typecheck in /--.endpage-- Operand stack: 0 true 595 (1)
New 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 % 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
pop pop pop pop % remove coordinates of bounding box from stack
gsave
1 0 0 setrgbcolor
fill
grestore
% end of new lines
currentpagedevice
/PageSize get 0 get exch
sub 460 sub 810
moveto show
globaldict
/MyPageCount MyPageCount 1 add put
} if
} bind
>> setpagedevice