Search
Find text in a PDF with NexPDF for PHP and get the page and rectangle of every match.
License module
Search is part of the viewer module and works without a key.
searchJson($query) searches every page, case-insensitively, and returns a JSON array of hits:
use Nexdevit\NexPdf\Document;
$doc = Document::open('contract.pdf');
$hits = json_decode($doc->searchJson('termination'), true);
foreach ($hits as $hit) {
echo 'page ', $hit['page'] + 1, ': ', $hit['context'], "\n";
}Each hit has:
| Field | Meaning |
|---|---|
page | 0-based page index |
context | The text around the match |
llx, lly, urx, ury | The rectangle of the match in PDF points |
Highlight every match
With the annotations module you can turn the hits into highlight annotations:
foreach (json_decode($doc->searchJson('termination'), true) as $hit) {
$doc->addAnnotation($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 redactMatches. See
Redaction.
Scanned documents
Search finds only real text. Run OCR on scanned pages first.