[gs-devel] Page number, watermarking and stamping example file ''

Godfrey Livingstone godber at maxnet.co.nz
Thu May 5 01:32:33 PDT 2005

Hello All

There are often questions on how to add the date or how to add page numbers.

The code below is an example solution, I wonder if it was included in
examples if it would help others.

The code also has examples on how to get the date, how to calculate page
size and how to ensure page numbering is correct.

On a related note an example on how to get one page from a pdf to add as a
watermark or stamp would be useful.  Looking at pdf_main.ps it is possible
but not obvious how to code.

Happy to sign your copyright agreement if necessary.

Note: this is my first attempt at programming in postscript so am sure
there is a more efficient way to do this, but none the less for new people
having all of this in one example file may be useful.



% Copyright (C) 2005 Godfrey Livingstone

% use as follows
% gs <options> -f pagenum.ps -f <postscript or pdf files to add page
number to>

% Set Page Count to 1 do not alter
currentglobal true setglobal globaldict/MyPageCount 1 put setglobal

/_showpage_ /showpage load def

  currentglobal true setglobal
  globaldict begin
  /MyPageCount MyPageCount 1 add def
}bind def

% set a page counter check, ghostscript
% when printing pdf's does not appear to call showpage

currentglobal true setglobal globaldict/MyPageCount_Check MyPageCount put

% Set the current date unless overridden

currentglobal true setglobal globaldict/MyDateString
        (%Calendar%) currentdevparams dup % get dic and duplicate
        /Day get =string cvs % find the day, convert to string
        1 index /Month get 1 sub  % put a copy of dic at top of stack
        {( January )( February )( March )( April )( May )( June )( July )(
August )
        ( September )( October )( November )( December )} exch get
        % get the month as written
             concatstrings % merge day and month
        exch /Year get =string cvs % bring dict to top, get year and
convert to
        concatstrings % merge day and month and year
put setglobal

% load letterhead on demand, only load once
currentglobal true setglobal globaldict/Load_MyLetterHeadD_True         1 put

% No letterhead at all is the default
currentglobal true setglobal globaldict/LetterHead_Number_Set [0] put

        2 eq % if called by endpage subtract 1
        { MyPageCount 1 sub }
        { MyPageCount }
        currentpagedevice /PageSize get aload pop pop
        % put the page width on the stack
        exch % page number and page width
        dup   % stack now contains  page width, page number, page number
        3 1 roll   % stack now contains page number, page width, page number
        =string cvs length 12 mul 20 add sub % stack now contains page
number and
        20 moveto /Helvetica-Bold 12 selectfont 1 0 0 setrgbcolor =string
cvs show
}bind def

        [ /BBox [0 0 100 100] /_objdef {date_logo} /BP pdfmark
                % bottom left corner of a page
                20 20 moveto /Helvetica-Bold 12 selectfont 1 0 0 setrgbcolor
MyDateString show
        [ /EP pdfmark
        currentglobal true setglobal globaldict/Load_MyLetterHeadD_True 0 put
}bind def

        Load_MyLetterHeadD_True 1 eq
        0 0 moveto
        [ {date_logo} /SP pdfmark
}bind def

        % get the varible called
        /my_begin_or_end_page_var exch def

        % This is a hack as ghostscript when printing
        % pdf's does not appear to call showpage

        MyPageCount MyPageCount_Check eq my_begin_or_end_page_var 2 eq and
                currentglobal true setglobal
                globaldict begin
                /MyPageCount MyPageCount 1 add def
                /MyPageCount_Check MyPageCount def
                currentglobal true setglobal globaldict/MyPageCount_Check
put setglobal

        % instructions passed as an array so foreach
        % element of the array do as instructed
        % note LetterHead_Number_Set is an array

        LetterHead_Number_Set  %forall
        dup 10 idiv
        /LetterHead_Number exch def
        10 mod
        /my_water_or_stamp_var exch def

        % if we are stamping only apply if endpage
        % likewise if watermark only apply if beginpage
        % define what is to be stamped as none otherwise

        my_begin_or_end_page_var  my_water_or_stamp_var ne {
/LetterHead_Number 0
def } if

        LetterHead_Number 1 eq % Date
        } if
        LetterHead_Number 2 eq % Number Pages
                my_begin_or_end_page_var MyLetterHeadPageNum
        } if
        } forall
}bind def

% format LLW
%        LL Letterhead to apply
%        W whether watermark (1) or stamp (2)
% Letterhead numbers
% 01 Date
% 02 Page Numbers at top of the page

% the example below will apply a stamp of the
% date and stamp page numbers on each page

currentglobal true setglobal globaldict/LetterHead_Number_Set [012 022]
put setglobal

% Note: Watermarking dooes not deal with
% pages size changes very well but stamping does

% These lines must be at the end of the file

<< /BeginPage { pop 1 MyDoLetterHead } bind >> setpagedevice

% using initmatrix is necessary because of the condition the
% windows ps driver leaves the current matrix in

<< /EndPage { 0 ne {false} { pop gsave initmatrix 2 MyDoLetterHead
grestore true} ifelse } bind >> setpagedevice

<---------------- end cut -------------------->

More information about the gs-devel mailing list