PDF Table of Contents Builder
Operator: Abdulla Al Fahad. How tools are built and checked
A 140-page report exported from a word processor often has no contents page, or its contents page went stale when a chapter moved. The headings are still in the file as large type, and sometimes as bookmarks, which is enough to build a new list.
This builder takes entries from the PDF's bookmarks or from lines set noticeably larger than the body text, lets you edit the list, and inserts one or more contents pages in front. The result is a page people can read and click, and the same entries can be written back as bookmarks.
Key features
- Entries from the PDF's bookmarks, with their nesting, or from headings detected by font size
- An editable list: change titles, pages and levels, add and remove rows
- Dot leaders, indentation by level, multi-page contents and clickable links to each section
- An optional write-back as bookmarks, and a rendered preview of the first contents page
Where the entries come from and how the page is laid out
Heading detection works from font size, since most PDFs carry no heading tags. The body size is the size that carries the most characters in the document. A line is a heading when its size is at least the chosen ratio (1.05 to 1.4, default 1.15) times the body size, it has 2 to 140 characters including a letter, and the same text with digits ignored appears on fewer than 30 percent of the pages (and fewer than three), so running headers and "Page 4 of 40" footers are dropped. Two consecutive lines of the same size closer than 1.8 times that size merge into one wrapped heading. Sizes within 0.75 pt count as one level, and the largest sizes, up to the number of levels you choose (1 to 4), become levels 1 to 4; smaller sizes are ignored. A run stops at 500 entries.
The contents page is set in Helvetica with first-level rows in bold and each deeper level indented 16 pt. A title too long for its line is cut with "...". Characters outside the standard Latin set, which includes accented Latin letters but not Greek, Cyrillic, CJK or symbols, are drawn as "?" and counted in a warning. The page matches page 1 or is A4 or Letter, with text from 9 to 14 pt; at 11 pt on a page 792 pt tall, 33 rows fit on the first page, which also holds the heading, and 35 on each later one. Every row carries a link to its page. Numbers are either the page as viewers count it after insertion, the default, or the original page numbers.
How it works
Reading uses the bookmarks editor's outline reader for bookmarks, which refuses a PDF that is encrypted, and pdf.js for headings, which reads every page's lines with their font size and can be cancelled. The two sources are independent, so a file with no bookmarks can still use headings.
Layout is worked out before anything is drawn, because the number of contents pages decides which physical page every entry lands on. Each row's link points at the original page object, which stays valid after pages are inserted in front, so a link and its printed number can differ only when you choose original numbering.
The original pages are copied unchanged, the new pages are inserted at the front and the file is saved. When bookmarks are requested, the outline is replaced through the writer the bookmarks editor uses, with entries pointing at the shifted pages, and the preview is rendered from the saved bytes so it is what the file holds.
Common use cases
- A 96-page proceedings assembled from 24 papers, none with bookmarks: detection at 1.25 times the body size picks up the paper titles as level 1, and deleting one stray running title leaves a clean two-page list.
- A 200-page report whose bookmarks nest three levels deep: use the bookmarks, delete the level 3 rows, and the contents fit on two pages instead of four.
- A 40-page manual scanned and OCR'd with no outline: add twelve rows by hand with their pages, and the builder supplies the layout and the links.
How to use this tool
- Drop the PDF; its bookmarks are read at once and used when there are any.
- Press Detect headings if there are none, adjusting the size ratio and the number of levels.
- Edit the entry list: fix titles, delete false hits, add rows for headings the detector missed.
- Set the heading text, page size, text size, number style and leaders, and tick bookmarks if wanted.
- Build the page, check the preview and download.
Example
Four entries for a 6-page PDF, heading Contents, 11 pt, numbers as the viewer counts them
Introduction, page 1, level 1
Background, page 2, level 2
Méthodes ✓ with symbol, page 4, level 1
Results, page 6, level 1What the saved file holds
One contents page is added, so the PDF has 7 pages.
The rows show numbers 2, 3, 5 and 7 and link to the original pages 1, 2, 4 and 6.
The é prints normally; the check mark is outside the Latin set and is drawn as "?", which the warning reports.
With bookmarks on, the outline reads Contents (1), Introduction (2), Background (3), Méthodes ✓ with symbol (5), Results (7).Tips for best results
- If the pages carry printed numbers that match their original positions, choose original numbering so the contents agree with the print; the links still jump to the right page.
- To number the new contents pages i, ii in the viewer, build the contents first and then run PDF Page Labels on the result.
- When detection picks up the running title on every page, raise the ratio or delete the rows. The repeat filter catches only text that recurs on at least three pages and 30 percent of them.
Common pitfalls
Bold headings at body size are invisible to size detection. A chapter title set in bold 11 pt in an 11 pt document is not found and has to be added by hand.
Titles use Helvetica's character set only, so Greek, Cyrillic or CJK headings show as question marks, although the bookmarks written alongside keep the real text.
Page labels the file already defines shift by the number of contents pages, which the tool warns about.
Writing bookmarks replaces the whole outline the file had, so untick it when you want to keep the existing one.
Best practices
- Detect headings, then read the list top to bottom once: a table caption or pull quote set in large type is the usual false hit, and removing it is one click.
What the bookmarks source includes
To reorganise the bookmarks themselves rather than print them, the PDF bookmarks editor here is the better tool. Bookmarks that point at a page become entries at their nesting level, clamped to six levels. Bookmarks that open a web link, another file or run an action are skipped, and the count of skipped ones is shown.
If the file has restrictions but no password to open it, heading detection still works because pdf.js can read it, and the saved copy loses those restrictions, which the tool reports. A file that needs a real password has to go through Unlock PDF first.
Picking the size ratio
The ratio compares each line's size with the dominant body size, so the right value depends on how the document was styled.
- 1.05 to 1.1 catches subheadings only a point or two larger than the text, at the cost of more false hits such as captions.
- 1.15, the default, finds 14 pt headings over 11 pt text.
- 1.25 to 1.4 keeps chapter titles only, which helps in documents full of mid-size captions and callouts.
Frequently asked questions
Why do the numbers on the contents page differ from the numbers in the original?
By default they count the new contents pages, because that is the number a viewer shows for the page. Switch the number style to original when the document's printed numbers should agree with the list.