Many scanned old books come with no table of contents. And Googling how to add tables of contents usually show up less helpful advice such as “buy Acrobat”. To save myself from having to Google this stuff again, it’s time to record it here.
First, create a text file in this format:
BookmarkLevel: 1 BookmarkPageNumber: 7 BookmarkBegin BookmarkTitle: Table of Contents BookmarkLevel: 1 BookmarkPageNumber: 13 BookmarkBegin BookmarkTitle: Chapter 1. Installation BookmarkLevel: 1 BookmarkPageNumber: 13 BookmarkBegin BookmarkTitle: Section 1. Getting the Source Code BookmarkLevel: 2
BookmarkPageNumber seems to refer to the “physical” page numbers instead of the “logical” ones. The page numbers don’t have to be in ascending order, so if a book containing some crazy internal reference, such as Chapter 12 requiring you to keep referencing a certain figure from Chapter 2, you can make a bookmark to jump back to it.
After finishing this step, run pdftk:
pdftk input.pdf input_toc.txt output output.pdf
And never read a long PDF without a proper table of contents any more.
References: learned the technique from here.