当前位置: 首页 > Skill >

ocr识别pdf书签

ocr识别pdf书签

ocr识别pdf书签

Add navigation bookmarks (outline/TOC) to scanned PDF documents using OCR. This skill should be used when the user asks to add bookmarks, navigation, table of contents, or outlines to a PDF that is a scanned image (no text layer). Covers the full workflow: detecting scanned PDFs, setting up Tesseract OCR with Chinese language support, OCR-reading the table of contents page, OCR-scanning all pages to locate actual section headers, and writing verified bookmarks with correct page numbers. Also han

1.0.0 2026.11.21 876
获取Skill文件查看使用说明
作 者:陆澄 许可证: 更新时间:2026.11.21
本文目录展开

PDF OCR Bookmarks

Overview

Add navigation bookmarks (PDF outline/TOC) to scanned PDF documents by using OCR to read the table of contents page and locate section headers across all pages. Ensures bookmark jump targets match the actual content pages, not the logical page numbers printed in the TOC.

When to Use

  • User asks to add bookmarks / navigation / TOC / outline to a PDF
  • User asks to add 目录 / 导航栏 / 书签 to a PDF
  • The PDF is a scanned image document (no extractable text layer)
  • The PDF has a visible table of contents page but no embedded bookmarks

Workflow

Step 1: Detect PDF Type

Check whether the PDF has a text layer or is a scanned image:

import fitz
doc = fitz.open(input_pdf)
page = doc[0]
text = page.get_text().strip()
if len(text) < 10:
    # Scanned PDF - no text layer, needs OCR
else:
    # Text-layer PDF - extract text directly
  • If text-layer PDF: extract TOC entries from text, find matching pages, write bookmarks. Skip to Step 5.
  • If scanned PDF: proceed to Step 2.

Step 2: Set Up OCR Environment

Refer to references/environment_setup.md for detailed setup instructions. Key requirements:

  1. Tesseract OCR installed at C:Program FilesTesseract-OCR (Windows)
  2. Chinese language pack (chi_sim.traineddata) downloaded to a custom tessdata directory
  3. PyMuPDF installed (use pdfkit-py skill's venv if direct pip install fails)
  4. pytesseract + Pillow installed in the same venv

Set environment variables before importing pytesseract:

os.environ['TESSDATA_PREFIX'] = 'C:/Users/USERNAME/.workbuddy/tessdata'  # dir containing .traineddata files
pytesseract.pytesseract.tesseract_cmd = r'C:Program FilesTesseract-OCRtesseract.exe'

Step 3: OCR the Table of Contents Page

  1. Ask the user which page contains the table of contents (commonly page 2-4)
  2. Render the TOC page at 300 DPI and OCR with chi_sim+eng
  3. Parse OCR text to extract section titles and their printed page numbers
  4. Build a preliminary bookmark list: [[level, title, printed_page], ...]

OCR tips for TOC pages:

  • Use --psm 4 (single column, variable sizes) or --psm 6 (uniform block)
  • 300 DPI gives good accuracy for most scans
  • Chinese-numeral section headers (一、二、三...) are reliable anchors

Step 4: OCR All Pages to Find Actual Section Positions

The printed page numbers in the TOC often do NOT match PDF page indices (cover pages, unnumbered pages cause offset). To ensure correct jump targets:

  1. Run scripts/add_pdf_bookmarks.py --mode find_sections to OCR all pages (top 50% at 200 DPI)
  2. Search each page's OCR text for section header keywords from the TOC
  3. Match each TOC entry to the actual PDF page where its header first appears
  4. For unmatched entries, try alternative keywords or visual inspection

Key matching strategy:

  • Extract 1-3 distinctive keywords per TOC entry (e.g., "投标函", "开标一览表")
  • Search page OCR text for these keywords in the first 5-8 lines (header region)
  • If a keyword appears on multiple pages, prefer the earliest page where it appears as a header (short line, near top)

Step 5: Write Bookmarks to PDF

Use PyMuPDF set_toc() to write bookmarks:

import fitz
doc = fitz.open(input_pdf)
# toc = [[level, title, page_number_1based], ...]
doc.set_toc(toc)
doc.save(output_pdf, garbage=4, deflate=True)
doc.close()

Notes:

  • Page numbers in TOC are 1-based (page 1 = first page)
  • For parent-level bookmarks (level 1) without their own page, set page to the first child's page
  • garbage=4 removes unused objects; deflate=True compresses streams
  • File content is never modified - only bookmark metadata is added

Step 6: Verify and Deliver

  1. Reopen the output PDF and call doc.get_toc() to verify bookmark count and page numbers
  2. Copy to the user's desired output location
  3. Present the file to the user

Scripts

scripts/add_pdf_bookmarks.py

General-purpose script supporting three modes:

--mode ocr_titles: OCR each page's top region and generate one bookmark per page (fallback when no TOC page exists).

--mode find_sections: OCR all pages, then search for user-specified section keywords to find actual page positions. Outputs a JSON file mapping section names to found pages.

--mode write_bookmarks: Write a predefined bookmark list (from JSON) to a PDF.

Usage:

python add_pdf_bookmarks.py --input input.pdf --output output.pdf --mode find_sections --keywords keywords.json
python add_pdf_bookmarks.py --input input.pdf --output output.pdf --mode write_bookmarks --bookmarks bookmarks.json

Common Pitfalls

  1. Printed page numbers != PDF page indices: Always verify by OCR-searching for actual headers. A 3-page offset (cover + TOC) is common.
  2. Tesseract language pack missing: Default Windows install only includes eng. Download chi_sim.traineddata separately.
  3. TESSDATA_PREFIX path: Must point to the directory containing the .traineddata files, not a parent directory.
  4. PyMuPDF on Python 3.13: Direct pip install pymupdf may fail. Use the pdfkit-py skill's venv which has PyMuPDF pre-installed.
  5. Console encoding on Windows: Set sys.stdout.reconfigure(encoding='utf-8') or use PYTHONUNBUFFERED=1 to avoid GBK encoding errors with Chinese text.
  6. Sandbox file access: Writing to Desktop may fail if the file is open. Save to a temp directory first, then copy.
  7. OCR speed: ~1 page/second at 200 DPI with chi_sim+eng. A 100-page PDF takes ~2 minutes.

相关文章

更多
PHP集成Tesseract OCR实现图片文字识别

在 PHP 中集成 Tesseract OCR 识别图片文字,核心做法是调用系统已安装的 tesseract 命令行工具。通过 exec() 或 proc_open() 执行命令,传入图片路径和输出目录,即可获取识别文本。

2026.03.21 583
Skill 文件
  • SKILL.md
    • PDF OCR Bookmarks
    • Overview
    • When to Use
    • Workflow
      • Step 1: Detect PDF Type
      • Step 2: Set Up OCR Environment
      • Step 3: OCR the Table of Contents Page
      • Step 4: OCR All Pages to Find Actual Section Positions
      • Step 5: Write Bookmarks to PDF
      • Step 6: Verify and Deliver
    • Scripts
      • scripts/add_pdf_bookmarks.py
    • Common Pitfalls
  • 项目或源文件

相关 Skill

更多
Pdf To Markdown Knowledge Pdf To Markdown Knowledge 将 PDF 笔记内容抽取为结构化 Markdown 知识整理文档。支持文本型 PDF 和扫描件 OCR。自动检测当前 agent 的 PDF 读取能力,优先使用原生 PDF 阅读(视觉理解),备选使用 Python 库(PyMuPDF/pdfplumber/Tesseract OCR)提取文本和图片,再理解内容进行知识抽取整理,最后严格格式化输出 Markdown。触发词:PDF知识抽取、提取笔记、整理PDF、知识整理、扫描件OCR。 最终形成的Markdown可以导入为思维导图
金山文档 KingDoc 金山文档 KingDoc 金山文档 AI 协作助手 — 9 品类在线文档全生命周期管理 (智能文档/文字/电子表格/演示文稿/多维表格/收集表/思维导图/流程图/附件), 深度直连金山文档(WPS)开放平台原生 API,覆盖腾讯文档全部能力 + 金山独有 10 项增强 (回收站、版本历史、格式转换、纯文本提取、本地 Tesseract OCR、通知推送、Webhook、 批量任务、政企合规、硬件自适应性能、WPS AI 能力、协同冲突解决)。文字/演示/思维导图/流程图采用"本地生成→上传覆盖", 电子表格/多维表格采用 API 精细编辑。本地生成、OCR、硬件画像、WPS AI 等能力零密钥可用。
image2text image2text 使用Tesseract OCR从图像中提取文字,支持本地文件、URL和Base64输入,适用于没有视觉功能的文本AI模型。
pdf-ocr-extraction pdf-ocr-extraction 使用 Tesseract OCR 从图片或扫描 PDF 中提取文本
Tesseract Ocr Tesseract Ocr 利用Tesseract OCR引擎直接通过命令行从图像中提取文本,支持中文、英文等多种语言。