Add bookmarks to a scanned PDF
0
bookmarks OCRmyPDF added to our 15 scans
Searchable, yes. Bookmarked, no.
OCR first, then one command
python3 -m venv bookmarks-venv
. bookmarks-venv/bin/activate
pip install pypdf
ocrmypdf scan.pdf scan-ocr.pdf
python3 pdf_bookmarks.py scan-ocr.pdf scan-bookmarks.pdf
It finds headings by their size in the OCR text layer. It also writes scan-bookmarks.toc.txt, the list it found. pdf_bookmarks.py, MIT license.
It needs Python 3.10 or later, OCRmyPDF and poppler:
brew install ocrmypdf poppler
on a Mac.
Fix the list, then write it again
One bookmark a line: page, level, title. Fix a title, add a missed heading, delete a wrong one.
python3 pdf_bookmarks.py scan-ocr.pdf scan-bookmarks.pdf --list scan-bookmarks.toc.txt
The list mode reads no text, so you can also type a list yourself. Ours for the test motion, saved as toc.txt:
# page level title (one bookmark a line; pages start at 1, level 1 is the top)
1 1 Motion to extend the deadline
1 2 Background
1 2 Reasons for the extension
1 2 Relief requested
2 1 Exhibit A
4 1 Exhibit B
6 1 Exhibit C
python3 pdf_bookmarks.py scan-ocr.pdf motion-bookmarks.pdf --list toc.txt
Good on our scans, weak on new ones
| Way | First 10 scans (tuned on) | 5 new scans (frozen script) |
|---|---|---|
| OCRmyPDF alone | 0 of 68 | 0 of 28 |
| pdf_bookmarks.py | 55 of 68, 1 false | 13 of 28, 3 false |
| Our Actor's code, build 0.1.102 | 68 of 68, 5 false | 22 of 28, 5 false |
I set the script's size rules on the first 10 scans. Then I froze it and made 5 new ones. On those it found under half.
Where it misses
- Bold at the body size: 13 of 15 missed, then 7 of 7. An OCR text layer has no bold.
- Our double-spaced brief: 0 of 7. Its headings had no extra space above them.
- Caps at 1.1 times the body: 3 missed. The --size default is 1.2.
- Big lines: "TOTAL CLAIMED: $4,218.50" and "STATUS: OPEN" became bookmarks.
Check the list before you file or send the PDF.
Our Actor marks bold headings too
Our Apify Actor turns scans into Markdown. At build 0.1.102, its code found the most headings above, bold ones too.
Its false ones included the line under an exhibit cover. It writes no bookmarks, so save its dataset as JSON, then:
python3 -c '
import json, re
doc = json.load(open("dataset.json"))[0]
for p in doc["pages"]:
for m in re.finditer(r"(?m)^(#{1,3}) +(.+)$", p.get("markdown") or ""):
print(p["page"], len(m.group(1)), m.group(2).replace("*", "").strip())
' > toc.txt
python3 pdf_bookmarks.py scan-ocr.pdf scan-bookmarks.pdf --list toc.txt
Fix levels in toc.txt first: on 4 of our first 10 scans, every heading came out at level 1.
The free plan takes 20 pages a run, 50 a month. An OCR page costs $0.006.
Court filings: bookmarks and OCR
California Rules of Court rule 8.74 (amended effective July 1, 2026) asks for a bookmark "to each heading, subheading, and the first page of any component of the document", exhibits too.
"All bookmarks must be set to retain the reader's selected zoom setting."
pdf_bookmarks.py sets each bookmark to keep the reader's zoom. For a paper-only document, the advisory committee comment says "optical character recognition must be used, if possible."
I am not a lawyer. Check your own court's rules. The script does not make a filing compliant.
Why not pdf.tocgen?
pdf.tocgen 1.3.4 finds headings by font. In an OCR text layer each word has its own size, so a recipe from one heading found 10 of 68.
With a 1 point size tolerance: 43, and 185 false bookmarks. It is GPL-3.0 or later and needs PyMuPDF (AGPL). Write its recipe with -o: with >> recipe.toml, a warning line breaks it.
How I tested it
- 10 made-up documents, 38 pages, 68 headings: a brief, a motion with 3 exhibits, a handbook, a lease, a paper and more. Simulated scans (tilt, gray paper, noise, JPEG), saved with no text layer.
- 5 new documents, 16 pages, 28 headings, with new fonts and words. The script was frozen before I made them.
- Found: a bookmark on the right page with the heading's text. Our Actor's code at commit 528fe416 (build 0.1.102). No Apify run.
- Tesseract 5.5.4, OCRmyPDF 17.13.0, poppler 26.03.0, pypdf 6.9.1 (7.1.1 for the commands above). Bookmarks changed none of the 10,438 words OCRmyPDF found.
What I did not test
Acrobat, PDF-XChange, Preview, other PDF apps and online tools, real scans, languages other than English, born-digital PDFs, text layers from other OCR tools, and pages with a table of contents.
Questions people ask
Does OCR add bookmarks to a PDF?
OCRmyPDF did not: 0 bookmarks on all 15 of our scans. It makes the text searchable. Add the bookmarks after it, from the text layer or from a list.
How do I add bookmarks to a PDF for free?
Type a list, one bookmark a line (page, level, title), and run pdf_bookmarks.py with --list. It uses pypdf and has an MIT license.
Can a PDF be bookmarked automatically?
Partly. pdf_bookmarks.py found 55 of 68 headings on the scans it was tuned on and 13 of 28 on 5 new ones. It missed bold headings at the body size and a double-spaced brief. Check the list.
Do the bookmarks keep the reader's zoom?
Each one goes to the top of its page and is set to keep the reader's zoom (/XYZ with a null zoom). I checked the setting in the file, not in a PDF viewer.