Highlight Boxes Off on Italic PDF Text? Use Quads
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-dir
Italic text can make search highlights look slightly off, and an area-based threshold seems like a quick way to check them. This guide explains why that test is unreliable and what to do instead. It is written for developers and document teams who use PyMuPDF's search_for to locate text and draw highlights that follow the actual shape of the text.
You will get a clear rule for when to use quads, practical handling for awkward search results, and a checklist for verifying your output. The examples use synthetic samples built in memory. Keep original copies and check your outputs against the criteria below.
Should you use quads for italic PDF text? The short answer
Yes. When you need corner-direction information, highlight directly from the results returned with quads=True, and do not judge correctness by an arbitrary area-difference threshold.
| Your situation | What to do | Why |
|---|---|---|
| Highlight boxes look off on italic text | Highlight directly from the results with quads=True | Italics do not necessarily make the Quad smaller than its bounding rectangle |
| You need to keep corner-direction information | Keep the four corners of each Quad | Quads preserve the four corners, which matters for rotated text |
| A search term spans a line break or returns fragments | Keep every returned Quad and highlight each one | Merging non-adjacent Quads into one Rect loses the blank areas between them |
| The page has a CropBox | Check that content stays in the visible area after restoring the CropBox | Coordinates use the unrotated page space, and get_text ignores the cropping region by default |
| The text layer is damaged or fonts are embedded abnormally | Send the file for manual review or re-OCR | search_for results may be entirely unreliable in these cases |
Why area differences are not a reliable test
- Italics do not necessarily make the searched Quad smaller than its bounding rectangle.
- Because of this, do not decide whether a highlight is correct based on an arbitrary threshold for area differences.
- When you need corner-direction information, apply the highlight directly using the results from
quads=True.
Build a self-contained synthetic example
The dependencies are listed in the import list. The code constructs its inputs in memory.
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
Verify correct output with a synthetic test case
To check this behaviour, build a fully self-contained synthetic test case. The code above generates a PDF in memory, so it runs without any external files.
Handle tricky search results
When the search term spans a line break
If the search term spans a line break and the engine does not recognise it as a single lexical unit, search_for may return an empty list or match only partial fragments.
- Do not force multiple non-adjacent Quads into a single Rect. This would lose the meaning of the blank areas between them.
- Keep all returned Quad fragments and highlight each one individually.
When the search term contains special characters
Regular-expression special characters that are not escaped can cause problems. Sanitise the input string before you call the search.
When the page has a CropBox
- The coordinates returned by
search_forare still based on the original, unrotated page space. - After you restore the CropBox, verify that the content remains within the visible area. Do not assume it does.
get_textignores the cropping region by default.
The decision on handling specific exceptions
| 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 |
📊 At a glance: Quads versus merged Rects
| Situation | Keep Quads (quads=True) | Merge into one Rect |
|---|---|---|
| Non-adjacent fragments | Highlight each returned Quad individually | Loses the blank areas between the fragments |
| Corner-direction information | Preserved by using quads=True | Not recommended when you need that information |
Acceptance criteria and the manual review boundary
This solution addresses only geometric positioning issues.
- If the PDF's text layer is damaged, font embedding is abnormal, or hidden layers exist,
search_forresults may be entirely unreliable. - These cases fall outside the scope of Quad and Rect selection. Send them for manual review or re-OCR.
- The code demonstration validates only geometric properties. Evaluate end-to-end performance with your specific document types.
References and next steps
Checklist for document translation
- Check that the page order, source locations and text integrity are all correct.
- Submit the same sample to your translation workflow.
- Verify that headings, body text and figure captions correspond correctly. Do not rely only on whether the text flows smoothly.
OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation.
🎯 Which approach fits you?
If your highlights look off on italic text
Highlight directly from the results with quads=True instead of checking area differences.
If your search term spans a line break
Keep every returned Quad fragment and highlight each one, rather than merging them into a single Rect.
If your page has a CropBox
Restore the CropBox, then confirm that the content still falls within the visible area.
If your PDF text layer is damaged
Send the file for manual review or re-OCR, since this solution covers geometric positioning only.
❓ FAQ
1. Can I use area differences to decide whether a highlight is correct?
No. Italics do not necessarily make the Quad smaller than its bounding rectangle, so an arbitrary threshold is not a reliable test.
2. What should I do when search_for returns only partial fragments?
Keep all the returned Quad fragments and highlight them individually. Do not force them into a single Rect.
3. Does get_text take the CropBox into account?
By default, get_text ignores the cropping region. After you restore a CropBox, check that your content still falls within the visible area.
4. Does this solution fix a damaged text layer?
No. It addresses geometric positioning only. A damaged text layer, abnormal font embedding or hidden layers call for manual review or re-OCR.
📝 Summary
For italic text, use the results from quads=True and avoid area-difference thresholds. Keep every fragment of a split search term, verify your output against the checklist, and send damaged files for manual review. Always test with real samples you are authorised to use.
Try it now: upload a document and get it translated with the layout intact


