Merging PDF files

Here another trick I found today. I needed to merge several PDF files (supplementary figures) into one big file to ease the submission of an article. As for playing with PDF files, the pdf toolkit (pdftk) is a wonderful swiss knif, so that one can simple do

pdftk FigureS1.pdf FigureS2.pdf FigureS3.pdf cat output AllSupFigures.pdf

This does the trick. Yet it would be nice to know which page corresponds to which figure, by creating bookmarks. I found out that pdftk also handle this, with a bit more efforts:

pdftk AllSupFigures.pdf dump_data > info.txt

for i in {1..3}; do
  echo "BookmarkBegin" >> info.txt
  echo "BookmarkTitle: Figure S$i" >> info.txt
  echo "BookmarkLevel: 1" >> info.txt
  echo "BookmarkPageNumber: $i" >> info.txt
done

pdftk AllSupFigures.pdf update_info info.txt output AllSupFiguresIndexed.pdf
rm info.txt

The resulting PDF file contains one index entry per figure.

Advertisements