Skip to Content
NexPDF

Search

Find text in a PDF in the browser with NexPDF for the web, get the page and rectangle of every match, and draw or highlight the hits.

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:

const hits = doc.search('termination');
for (const hit of hits) 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

Draw the hits over a page

Map each rectangle to pixels with the page's PageView:

const dpi = 144;
const view = doc.pageView(0);
for (const hit of doc.search('termination').filter((h) => h.page === 0)) {
  const { left, top, width, height } = view.rectToView([hit.llx, hit.lly, hit.urx, hit.ury], dpi / 72);
  const mark = Object.assign(document.createElement('div'), { className: 'hit' });
  Object.assign(mark.style, { left: `${left}px`, top: `${top}px`, width: `${width}px`, height: `${height}px` });
  overlay.append(mark);
}

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] });
}
const blob = doc.toBlob();

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