2015-04-30
I've recently been putting together applications (to ask for funding for PhD students) that required me to create single PDF files containing various documents (some I had produced myself, some I had scanned). I knew how to do this using gs or pdftk, but then I found myself with large documents (around 30 pages) and realized page numbers would be really helpful. "I'm sure there's a way to do this with pdftk," I thought to myself. And sure enough, there was.
So here are the magic commands.
To concatenate all your original PDF documents in a single PDF file:
gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=single_file.pdf file_1.pdf file_2.pdf ... file_last.pdf
or
pdftk file_1.pdf file_2.pdf ... file_last.pdf output single_file.pdf
Now you need to create a PDF that contains only page numbers. In numbers.tex:
\documentclass[12pt,a4paper]{article} \usepackage{multido} \usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry} \begin{document} \multido{}{31}{\vphantom{x}\newpage} \end{document}
(where 31 is your total number of pages.)
Then create numbers.pdf
pdflatex numbers.tex
Now you need to split your documents to work single page per single page:
pdftk numbers.pdf burst output number_%03d.pdf pdftk single_file.pdf burst output page_%03d.pdf
Now stamp the numbers on the pages:
for i in $(seq -f %03g 1 31) ; do pdftk page_$i.pdf stamp number_$i.pdf output new_$i.pdf ; done
Alternatively you can try background instead of stamp but for me it didn't work on scanned documents.
And put it all back together:
pdftk new_???.pdf output single_document_numbered.pdf
Edited 2016-11-17 P. Sunthar wrote me that thanks to multistamp a single command can be used to collate and stamp multiple documents:
pdftk single_file.pdf multistamp numbers.pdf output single_document_numbered.pdf