Nested PDF Bookmarks: The Level-2 Test, Raw Readings

A six-entry bookmark tree — two chapters, four nested sub-entries — cost 1001 bytes to strip and came back within 99 bytes when rebuilt with set_toc; get_toc returned the identical nested list before and after, nesting included. Same three-state structure as our flat-outline run, now with real hierarchy: readings below, unrounded, so the repeatable claim is exact and anyone with a PyMuPDF install can verify or refute it.

The three states, as read

Stateget_toc readingFile sizeDelta vs original
0 — Bookmarked original[[1,'Chapter 1',1],[2,'Chapter 1.1',1],[2,'Chapter 1.2',2],[1,'Chapter 2',3],[2,'Chapter 2.1',3],[2,'Chapter 2.2',4]]2744 B—
1 — Stripped (set_toc([]), garbage=4 save)[]1743 B−1001 B
2 — Rebuilt from the saved listSix entries, identical levels, titles, pages2645 B−99 B

Post-rebuild equality read back as toc_identical: True — the nested levels survived exactly. Source: sandbox PyMuPDF run, 2026-11-08, byte counts read off the saved files.

What nesting itself costs (raw diff, flat vs nested)

Comparing today's nested run to yesterday's flat run is exact, not modeled — both were 4-page files saved the same way:

RunEntriesStrip costRebuild gap
Flat (all level 1)4712 B101 B below original
Nested (2×L1 + 4×L2)61001 B99 B below original

Reading it as-is, no smoothing: the extra 289 bytes of strip cost tracks the two extra entries (titles + first/last-child pointer structure per nested node), while the rebuild gap stayed in the same ~100 B band both times. We did not force those two numbers into a rule — with n=1 per configuration that would be a story, not a finding.

How entries encode nesting (and where it breaks)

Levels in set_toc are [level, title, page] triplets. The rebuild list asserted above is what preserves hierarchy; two failure modes to know:

  1. Level jump. A [2,...] entry following no [1,...] entry has no parent — conversions that flatten levels produce a broken tree rather than a nested one. Keep every entry's level ≤ previous level + 1.
  2. Page targets preserved but levels lost. Equal titles with wrong levels reconstruct a flat sidebar. Compare get_toc()[i][0] between states, not just title strings.

FAQ

Do nested bookmarks survive a PDF merge or split?

Not by default — most pipelines drop or flatten outline trees at merge boundaries. Rebuild entries per source document after the merge with the set_toc procedure; verify with get_toc and a level-by-level read-back like the table above.

Why does nesting cost more than flat entries?

Because each nested node carries first/last-child pointer objects in the outline tree — today's +289 B strip diff versus six short titles is consistent with that structure. We report the measurement; we do not claim the per-object accounting as proof.

Related: flat-outline rebuild with get_toc · the in-browser converter