How to add page numbers to Postscript/PDF

Viewed 33592

If you've got a large document (500 pages+) in Postscript and want to add page numbers, does anyone know how to do this?

15 Answers

I was looking for a postscript-only solution, using ghostscript. I needed this to merge multiple PDFs and put a counter on every page. Only solution I found was an old gs-devel posting, which I heavily simplified:

%!PS
% add page numbers document bottom right (20 units spacing , harcoded below)
% Note: Page dimensions are expressed in units of the default user space (72nds of an inch).
% inspired by https://www.ghostscript.com/pipermail/gs-devel/2005-May/006956.html

globaldict /MyPageCount 1 put % initialize page counter

% executed at the end of each page. Before calling the procedure, the interpreter
% pushes two integers on the operand stack:
% 1. a count of previous showpage executions for this device
% 2. a reason code indicating the circumstances under which this call is being made:
%    0: During showpage or (LanguageLevel 3) copypage
%    1: During copypage (LanguageLevel 2 only)
%    2: At device deactivation
% The procedure must return a boolean value specifying whether to transmit the page image to the
% physical output device.
<< /EndPage {
  exch pop % remove showpage counter (unused)
  0 eq dup { % only run and return true for showpage
    /Helvetica 12 selectfont % select font and size for following operations
    MyPageCount =string cvs % get page counter as string
    dup % need it twice (width determination and actual show)
    stringwidth pop % get width of page counter string ...
    currentpagedevice /PageSize get 0 get % get width from PageSize on stack
    exch sub 20 sub % pagewidth - stringwidth - some extra space
    20 moveto % move to calculated x and y=20 (0/0 is the bottom left corner)
    show % finally show the page counter
    globaldict /MyPageCount MyPageCount 1 add put % increment page counter
  } if
} bind >> setpagedevice

If you save this to a file called pagecount.ps you can use it on command line like this:

gs \
  -dBATCH -dNOPAUSE \
  -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
  -sOutputFile=/path/to/merged.pdf \
  -f pagecount.ps -f input1.pdf -f input2.pdf

Note that pagecount.ps must be given first (technically, right before the the input file which the page counting should start with).

If you don't want to use an extra .ps file, you can also use a minimized form like this:

gs \
  -dBATCH -dNOPAUSE \
  -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
  -sOutputFile=/path/to/merged.pdf \
  -c '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 20 sub 20 moveto show globaldict /MyPageCount MyPageCount 1 add put } if } bind >> setpagedevice' \
  -f input1.pdf -f input2.pdf

Depending on your input, you may have to use gsave/grestore at the beginning/end of the if block.

You can use the free and open source pdftools to add page numbers to a PDF file with a single command line.

The command line you could use is (on GNU/Linux you have to escape the $ sign in the shell, on Windows it is not necessary):

pdftools.py --input-file ./input/wikipedia_algorithm.pdf --output ./output/addtext.pdf --text "\$page/\$pages" br 1 1 --overwrite

Regarding the --text option:

  • The first parameter is the text to add. Some placeholders are available. $page stands for the current page number, while $pages stands for the total number of pages in the PDF file. Thus the option so formulated would add something like "1/10" for the first page of a 10-page PDF document, and so on for the following pages
  • The second parameter is the anchor point of the text box. "br" will position the bottom right corner of the text box
  • The third parameter is the horizontal position of the anchor point of the text box as a percentage of the page width. Must be a number between 0 and 1, with the dot . separating decimals
  • The fourth parameter option is the vertical position of the anchor point on the text box as a percentage of the page height. Must be a number between 0 and 1, with the dot . separating decimals

Disclaimer: I'm the author of pdftools

I have used LibreOffice Calc for this. Adding a page number field is easy using Insert->Field->Page Number. And then you can copy-and-paste this field to other pages; fortunately the position is not changed and the copy-and-paste can be done quickly with down arrow key and Ctrl+V. Worked for me for a 30 page article. Maybe prone to errors for a 500+ one!

Related