2021-02-27 Adding a table of contents to a PDF

I wanted to create small PDF with the most important pages for me, and add a table of contents, using `pdftk`.

First, create a draft of the PDF, based on a subset of pages:

pdftk ~/Documents/RPG/Traveller/CT/CT-TTB-The_Traveller_Book_\(11872828\).pdf \
  cat 45-48 101-102 115 \
  output referee.pdf

Next, create a table of content as follows:

BookmarkBegin
BookmarkTitle: Weapons and Equipment
BookmarkLevel: 1
BookmarkPageNumber: 1
BookmarkBegin
BookmarkTitle: Personal Combat
BookmarkLevel: 1
BookmarkPageNumber: 2
BookmarkBegin
BookmarkTitle: Surprise
BookmarkLevel: 2
BookmarkPageNumber: 2
BookmarkBegin
BookmarkTitle: Range
BookmarkLevel: 2
BookmarkPageNumber: 2
BookmarkBegin
BookmarkTitle: Escape
BookmarkLevel: 2
BookmarkPageNumber: 2
BookmarkBegin
BookmarkTitle: Weapons and Range Matrix
BookmarkLevel: 1
BookmarkPageNumber: 3
BookmarkBegin
BookmarkTitle: Wounds, Medical Care, Ranges
BookmarkLevel: 1
BookmarkPageNumber: 4
BookmarkBegin
BookmarkTitle: Encounters, Patrons
BookmarkLevel: 1
BookmarkPageNumber: 5
BookmarkBegin
BookmarkTitle: Rumours
BookmarkLevel: 1
BookmarkPageNumber: 6

I’m not yet ready to write a Perl script to generate this output. If I need to do this often, I’ll use Emacs keyboard macros.

Save this as a text file, e.g. `referee.txt`.

pdftk ~/Documents/RPG/Traveller/CT/CT-TTB-The_Traveller_Book_\(11872828\).pdf \
  cat 45-48 101-102 115 \
  output - \
| pdftk - update_info referee.txt \
  output referee.pdf

And now you have a file called `referee.pdf`.

You can also turn the existing table of contents into a text file, and add the necessary bookmarks:

a text file

pdftk ~/Documents/RPG/Traveller/CT/CT-TTB-The_Traveller_Book_\(11872828\).pdf \
  update_info traveller.txt output CT-TBB.pdf

Success!

Screenshot

See also: reducing PDF file size.

reducing PDF file size

​#PDF