Add Windows OCR for image-only PDF pages (per-page and batch) - #720
Conversation
|
Known limitation: position-based bookmarks (and notes) aren't re-anchored after OCR Bookmarks are stored as document-absolute display offsets in the per-document Consequence: any bookmark (or note) added before OCR drifts by however much text OCR inserts ahead of its position. A bookmark on a page that gets OCR'd lands at the wrong character offset inside that page's new text — still the right page, but the precise line anchor is lost. OCR-first, bookmark-later is unaffected. Possible directions:
Open to direction — happy to take a pass at the delta-shift option if it'd be useful. |
|
Hey there, |
|
Thank you! I played around with this a lot before PR'ing. What I ended up liking here was just pressing enter to get any page OCR'd at any time. And I added the Batch OCR to just do the entire document in one shot. Would be curious how your proposal works, and I'd be happy to try to merge any of these ideas in at any point if they end up fitting somehow. |
|
Plan, briefly. We're doing both this and Scribe (our hosted conversion service). They answer different questions, so they get separate paths rather than one abstraction:
No pluggable provider registry. Two features, two entry points. So this is getting merged. I'm taking the fixes myself and will push to this branch:
Persisting results across reopen is next after that, not a blocker for merging. The per-page image detection change in |
b994744 to
f83d88b
Compare
|
Pushed the rebase and the fixes to this branch. 787 core tests pass, the workspace suite passes, clippy and fmt are clean. First, a correction. I said the batch worker calling What landed: Marker instead of translated text. New Related: One rebuild per batch, not per page. Position drift, covering everything. Incremental apply and cancel. The worker opens the PDF once and posts results in groups of 20, so text appears while the job runs instead of all at the end. Choosing Batch OCR while a batch is running asks "Batch OCR is running. Stop it?" rather than needing a second menu item. macOS. New Smaller ones. A missing OCR language pack gets its own message instead of a generic "OCR failed." Render size scales from the real page dimensions targeting 300 DPI, capped at whatever the engine reports as its maximum, rather than a flat 1800 px. Linux has no OS OCR engine, so it gets a stub, no menu item and placeholder wording that doesn't promise something Enter can't do. Not done, and not blocking the merge: OCR results still don't survive reopening the file. The Structure ended up as |
|
Awesome... looking forward to test this! This app is on fire :) |
OCR for image-only PDF pages (per-page and batch)
For a scanned PDF (images only, no text layer), this adds OCR on Windows using the built-in
Windows.Media.Ocrengine — both per-page and in bulk. I know it's only Windows, but hopefully this proof of concept still helps. This feature would be huge for many.What it does
Per-page (Enter):
[Image only. Press enter to OCR.]Batch (Tools → Batch OCR…, default shortcut Ctrl+Shift+O):
What's new under the hood
This introduces the document buffer's first mutation API — until now parsed documents were effectively read-only:
DocumentBuffer::replace_range(paperback-core) replaces a span in display units, rebuilds the per-char index tables, and shifts markers/id_positionsthat follow the edit.DocumentSession::{replace_range, line_bounds_at, line_text_at}— display-unit-correct line helpers (the existingget_line_texttreats its argument as a char index, which misaligns after astral characters).SoftwareBitmapfrom the pdfium RGBA render and runsOcrEngineon a worker thread, marshalling results back withwxdragon::call_after.Windows OCR notes (read before judging quality)
Windows.Media.Ocrexposes no preprocessing or accuracy settings, and we deliberately make no attempt to tune it — I don't know anything about tuning that API.Not included (out of scope for this pass)
Testing
Tested on Windows 11 with NVDA against an image-only PDF: placeholder announced per image page; Enter runs OCR and the recognized text replaces the placeholder in the richedit; line and page navigation (
p) read the OCR'd text; Tools → Batch OCR (Ctrl+Shift+O) OCRs a whole range with spoken progress and skips already-text pages. Windows-only feature.