Skip to Content
NexPDF

Search

Find text in a PDF with NexPDF for Node.js 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(query) searches every page, case-insensitively, and returns an array of hits:

import { PdfDocument } from '@nexdevit/nexpdf-node';

const doc = await PdfDocument.open('contract.pdf');
for (const hit of doc.search('termination')) {
  console.log(`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 (const hit of doc.search('termination')) {
  doc.addAnnotation({ page: hit.page, kind: 'Highlight', color: '#FFD400', rect: [hit.llx, hit.lly, hit.urx, hit.ury] });
}
await doc.save('highlighted.pdf');

Redact every match

To remove the matches permanently instead, use doc.raw.redactMatches. See Redaction.

Scanned documents

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

On this page