Skip to Content
NexPDF

Search

Find text in a PDF with NexPDF for Python and get the page and rectangle of every match.

English

License module

Search is part of the viewer module and works without a key.

search_json(query) searches every page, case-insensitively, and returns a JSON array of hits:

import nexpdf

with nexpdf.Document.open("contract.pdf") as doc:
    hits = nexpdf.loads(doc.search_json("termination"))
    for hit in hits:
        print(f'page {hit["page"] + 1}: {hit["context"]}')

Each hit has:

FieldMeaning
page0-based page index
contextThe text around the match
llx, lly, urx, uryThe rectangle of the match in PDF points

Highlight every match

With the annotations module you can turn the hits into highlight annotations:

for hit in nexpdf.loads(doc.search_json("termination")):
    doc.add_annotation(hit["page"], "Highlight", "#FFD400", 1.0, "",
                       hit["llx"], hit["lly"], hit["urx"], hit["ury"])
doc.save("highlighted.pdf")

Redact every match

To remove the matches permanently instead, use redact_matches. See Redaction.

Scanned documents

Search finds only real text. Run OCR on scanned pages first.

On this page