阅读文本 / Reading text
This paper uses synthetic samples to demonstrate the processing method. The code has been verified using examples in an isolated environment; this does not guarantee effectiveness for all real files or products. Before processing important files, please retain the original copies and verify them against the boundaries and acceptance criteria outlined in this document.
Which problem does this paper address first
Italics do not necessarily ensure that the area of the searched Quad is smaller than that of its bounding rectangle. When it is necessary to preserve corner-direction information, apply highlighting directly using the results with quads=True; do not determine whether the highlighting is correct based on an arbitrary threshold for area differences.
self-contained synthetic example
dependencies are listed in the import list. The code constructs inputs in memory; runtime logs are provided separately and do not constitute a guarantee for all real-world files.
import pymupdf
with pymupdf.open() as source:
page = source.new_page(width=300, height=300)
page.insert_text((100, 250), "Searchable", fontsize=14, rotate=90)
hits = page.search_for("Searchable", quads=True)
assert len(hits) == 1
assert isinstance(hits[0], pymupdf.Quad)
page.add_highlight_annot(hits)
assert page.search_for("Missing", quads=True) == []
raw = source.tobytes()
with pymupdf.open(stream=raw, filetype="pdf") as doc:
page = doc[0] # Keep the owning page alive while using annotations.
annotations = list(page.annots())
assert len(annotations) == 1
assert len(annotations[0].vertices) == 4
synthesis example: correct output and boundary input handling
To verify this behavior, we construct a fully self-contained synthetic test case. Note that the following code generates a PDF in memory and can run without any external files.
boundary input and error handling:
If the search term spans a line break and is not recognized by the engine as a single lexical unit,
search_for
may return an empty list or match only partial fragments. In such cases, one should not forcibly merge multiple non-adjacent Quads into a single Rect, as this would lose the semantics of the intervening blank areas. The correct approach is to retain all returned Quad fragments and highlight them individually. Another common edge case occurs when the search term contains regular-expression special characters that have not been escaped; in these situations, the input string should be sanitized before invocation. Furthermore, if the page has a CropBox defined,
search_for
The returned coordinates are still based on the original, unrotated page space; after restoring the CropBox, it is necessary to verify that the content remains within the visible area—do not assume.
get_text
defaults to ignoring the cropping region.
Acceptance Criteria and Manual Review Boundary
explicitly defines the scope of inapplicability: this solution addresses only geometric positioning issues. If the PDF’s text layer is damaged, font embedding is abnormal, or hidden layers exist,
search_for
The results may be entirely unreliable. Such cases do not fall under the scope of Quads/Rects selection and should be subject to manual review or re-OCR. The code demonstration only validates geometric properties; the end-to-end performance still needs to be evaluated in conjunction with the specific document type.
References and Next Steps
PyMuPDF Page.search_for Official Documentation: https://pymupdf.readthedocs.io/en/latest/page.html#Page.search_for
PyMuPDF coordinate system description: https://pymupdf.readthedocs.io/en/latest/page.html#coordinates
The decision on handling the specific exception
observation
What should be done
should not infer
search yielded no hits
verifies the text layer against the keyword
does not create an empty highlight
rotated text
preserves Quad four corners
does not predict correctness based on area difference
multiple hits on
Retain page numbers item by item
should not process only the first
uses this checklist for document translation
first ensures that the page order, source locations, and text integrity have been thoroughly checked, then submits the same de-identified sample to the translation workflow. After translation, it is still necessary to verify that the headings, body text, and figure captions correspond correctly; one should not rely solely on whether the text flows smoothly.
OmniDoc (product entry point)
provides an entry point for translating multi-format documents, and the official website highlights its OCR, terminology database, and bilingual alignment features. For teams that wish to start processing directly from files, this is a path worth evaluating with real-world samples. Please use de-identified files that you are authorized to upload, and verify the results against the checklist provided in this document; we do not claim that OmniDoc utilizes the open-source libraries or internal algorithms referenced herein, nor do we substitute examples for product performance testing.
- 1 -
Updated 2026-10-08