PDF Bookmarks Disappeared? A Minimal Outline Rebuild, Measured

We ran it: stripping a 4-entry outline from a PDF removed 712 bytes from the file, and writing it back with a two-line-per-entry rebuild landed the file only 101 bytes below the original — bookmarks recovered with get_toc/set_toc, zero visual change, all readings below. If your PDF tool "cleaned" or re-saved a document and the sidebar bookmarks vanished, that outline is recoverable programmatically in seconds. Here is the exact procedure and the before/after evidence.

Bookmarked3259 B Stripped2547 B Rebuild3158 B
Same document, three saves: strip cost 712 B; rebuild came back within 101 B of the original.

The setup and the readings

Test file: a 4-page PDF with one first-level bookmark per page (built with powers from pymupdf on desktop, runnable with any PyMuPDF install). Three save states, each read back with get_toc and byte-counted:

Stateget_toc readingFile sizeDelta vs original
0 — Bookmarked original4 entries: Section 1–4 → pages 1–43259 B—
1 — Outline stripped (set_toc([]), garbage-collected save)[] — zero entries2547 B−712 B
2 — Outline rebuilt from the saved list4 entries, identical titles and page targets3158 B−101 B

Source: our own sandbox run today (2026-11-08), PyMuPDF set/get, byte counts straight off the saved files. Nothing estimated.

Why strip costs 712 B but rebuild costs 101

  1. The outline carries destination objects too. Each bookmark holds a pointer structure naming the target page and view. When we stripped with garbage collection (garbage=4), those pointers and the bookmark title strings went away — 712 bytes of real payload, not padding.
  2. The rebuild re-creates titles and destinations but lands on slightly different object numbering than the original save. Two runs of the same feature produce near-identical, not bit-identical, files — 3158 B vs 3259 B. The 101-byte gap is object-offset drift, not missing content: get_toc on the rebuilt file returns the same 4 entries, same titles, same pages.

The minimal rebuild procedure (works from any stripped PDF)

import pymupdf

doc = pymupdf.open("input.pdf")

# read back the entries you saved before the destructive edit
toc = [
    [1, "Section 1", 1],
    [1, "Section 2", 2],
    [1, "Section 3", 3],
    [1, "Section 4", 4],
]
doc.set_toc(toc)          # entries: [level, title, 1-based page]
doc.save("output.pdf", garbage=4, deflate=True)

check = pymupdf.open("output.pdf")
assert check.get_toc() == toc   # verify before shipping

Rules that keep a rebuild minimal:

  1. Keep the entry list before any destructive operation — one get_toc() call is your insurance policy.
  2. Levels mean nesting, not count. A [2, ...] entry nests under the previous [1, ...]. Preserve levels, not just titles.
  3. Verify with a read-back assert, not by opening the file and squinting at the sidebar. The assert caught nothing to fix in our run — but assertions are how you know that.

When a rebuild is the wrong tool

FAQ

How do I get bookmarks back on a PDF?

If you (or your script) saved the outline entries before loss, a rebuild with set_toc restores them exactly — our run recovered 4-for-4 entries and landed within 101 bytes of the original file. Without the saved list, entries can be regenerated from document headings.

Why did converting my PDF remove the bookmarks?

Many conversion and compression pipelines rebuild the document object-by-object and drop the outline tree unless they explicitly carry it. That is a pipeline behavior, not a PDF limitation — outlines survive any tool that chooses to preserve them, and ours is one of the things we keep an eye on in the walkthrough checks.

Do bookmarks add much file size?

Measured, not guessed: 712 bytes for four entries on a 3.2 KB test file. On real documents the outline is proportionally tiny — a fearsome-looking 500-entry outline is still measured in tens of kilobytes. Stripping an outline for "file size" reasons is rarely worth it.

Related: checkbox bytes: ZaDb glyph vs drawn path · Markdown to PDF · the in-browser converter