2014-06-11

Overlay many PDFs with each other, for example to add individual page numbers

Some times you have a large number of PDFs you wan to include in a document but that do not have the correct page numbers on them.

To overcome this problem, you can use the command line tool pdftk.

Let's assume you have a 20-page PDF document called "appendix.pdf" to which you want to add page numbers that are in the same layout as in the other pages of your document "thesis.doc", or "thesis.pdf". First, you need to create another 20-page PDF document that contains only the page numbers. You can do this in Word, or LaTeX, or whatever you are using for your original document and save the file as "page_numbers.pdf".
The only thing that is important, is that both documents have the same number of pages.

Now you have the two documents:

  • page_numbers.pdf (20 pages)
  • appendix.pdf (20 pages)


1. Burst pdfs

Use the following command to create two empty folders and the pdftk command to "burst" the PDFs into twenty single page pdf files.

mkdir page_nrs
mkdir appendix
mv page_numbers.pdf page_nrs/
mv appendix.pdf appendix/
cd page_nrs; pdftk page_numbers.pdf burst; cd ..
cd appendix; pdftk appendix.pdf burst; cd ..

You will find, that both folders contain twenty pdf files each with names like this: pg_0001.pdf.

2. overlay pdfs

If you now want to combine/overlay those files, use the following command:

mkdir combined
cd page_nrs
for FILE in pg*pdf
 do
 pdftk $FILE background ../appendix/$FILE output ../combined/$FILE
done
cd ..


3. concatenate overlayed pdfs

Now, each file is overlayed with the corresponding page number. To combine the twenty individual files into one pdf document, use:

cd combined; pdftk p*pdf cat output finished.pdf

Now you are done and can have a look, if everything is correct!



4. Only if pages do not fit: shrink appendix.pdf

If you have the problem, that some pages in your appendix have text at the same position as the page number, you can shrink the pdf content and increase the "border" around the pdf while keeping the same page size with this command:

cd appendix
for FILE in pg*pdf
 do
 gs \
 -dPDFSETTINGS=/default \
 -sDEVICE=pdfwrite \
 -o ../Text_resized/$FILE \
 -dDEVICEWIDTHPOINTS=714 \
 -dDEVICEHEIGHTPOINTS=1010 \
 -dFIXEDMEDIA \
 -c "<</PageOffset [70 50]>> setpagedevice" \
 -f $FILE

done
cd ...

Now you can continue with step number 2, and see if it looks better.
If the pages are still too large, or if they are too small, play around with the numbers marked in orange, to get a different result.