Skip to content
Genom

Search a Word document and highlight every hit

Hits painted on pages that are not in the DOM.

.docx.docOne package, every generation — @genomdev/docx

Running here, in this pageNothing is uploaded

The obvious way to build a find box is to walk the DOM for a string. It is wrong here for three reasons: it cannot see the pages that are not mounted, it matches the list and page numbers the renderer computed rather than read, and what it produces is a DOM node — which cannot be stored, sent anywhere, or found again after a relayout.

The index is built from the model instead, so a hit on page four hundred is found now and painted when you reach it.

const search = await viewer.search();
const hits = search.find('invoice');

// Every hit is painted, including on pages that are not mounted:
// the index is built from the model, not from the DOM.
search.highlight(hits);

Put it in your own application

What is above is the published package running in this page, with nothing behind it. The documentation covers the same ground with the API in full.