Search Text in PDF and Office Documents using JavaScript

Search through the UI

WebViewer, DOCX Editor, and Spreadsheet Editor each have a text search panel built into the UI to handle searching in their respective file types.

WebViewer Search UI

Search programmatically

Searching for text can be performed using programmatic APIs:

JavaScript

1WebViewer(...)
2.then(instance => {
3 const documentViewer = instance.Core.documentViewer;
4
5 documentViewer.addEventListener('documentLoaded', async () => {
6 const searchStream = documentViewer.search('search_term'); // Setting up stream using your string to search.
7 const searchResults = await searchStream.getAll(); // Wait for all results.
8 console.log('search results:', searchResults);
9 });
10});

For more precise results, you can add Search.Mode options to better refine the results:

JavaScript

1WebViewer(...)
2.then(instance => {
3 const { documentViewer, Search } = instance.Core;
4
5 documentViewer.addEventListener('documentLoaded', async () => {
6 const searchMode = [Search.Mode['WHOLE_WORD'], Search.Mode['CASE_SENSITIVE']];
7 const searchStream = documentViewer.search('search_term', searchMode); // Setting up stream using your string to search with whole word and case sensitivity.
8 const searchResults = await searchStream.getAll(); // Wait for all results.
9 console.log('search results:', searchResults);
10 });
11});

You can also progressively search for text if you don’t want to wait for the entire list of search items to be retrieved:

JavaScript

1WebViewer(...)
2.then(instance => {
3 const documentViewer = instance.Core.documentViewer;
4
5 documentViewer.addEventListener('documentLoaded', async () => {
6 const searchStream = documentViewer.search('search_term'); // Setting up stream using your string to search.
7 const searchResults= [];
8
9 for await (const result of searchStream) {
10 searchResults.push (result); // Progressively adding results to an array.
11 console.log('search result found:', result);
12 }
13 });
14});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales