AVAILABLE NOW: Spring 2025 Release
By Garry Klooesterman | 2025 Jun 12
4 min
Tags
webviewer
Summary: Businesses today are looking for fast, reliable, and efficient solutions to help with the many day-to-day challenges they face, including searching for text in PDFs. WebViewer allows developers to easily implement customizable, full-text search capabilities into their apps, ensuring users have the tools they need. This blog discusses how to add search functionality using WebViewer and JavaScript.
Businesses face many challenges today, including constrained budgets, tight timelines, and growing demands, and they’re seeking efficient and cost-effective solutions to manage these challenges.
One such solution is full-text search in PDFs. Full-text search enables efficient and accurate search capabilities, allowing businesses to reduce time spent on manual, error-prone tasks. For example, a legal team reviewing hundreds of pages in a contract for a certain clause or a healthcare research team combing through patient consent forms looking for a specific allergy.
Automating the process with enhanced text search tools, such as those built into Apryse WebViewer, enables fast information retrieval and analysis, which helps reduce processing times, eliminate errors, and ensure compliance.
Basic text search is a feature that can be found in practically any PDF viewer and can quickly search for and highlight a specific term throughout a document. But basic search can lack certain features, such as retaining the highlighting when not searching or highlighting different terms in different colors at the same time.
This is where implementing full-text search capabilities into your apps helps deliver more robust functionality your clients need.
WebViewer UI has a built-in search panel for basic text searches, but it also provides an API for more control and customization. For example, you can implement the following functions to create a more customized text search function in your apps:
These cover the basics of adding text search through WebViewer. See our documentation for more details.
WebViewer by default has a powerful search option that can be customized in various ways, such as highlighting a second search term in a different color to display multiple search queries at the same time.
In this blog, we’ll create a basic PDF viewer with a built-in search panel. We’ll use searchTextFull to search the text and adding a searchListener callback to create annotations on top of the results which will remain after clearing the search panel.
npm install -g http-server
Download WebViewer and extract the WebViewer.zip file into your myServer folder.
Note: By default, WebViewer.zip will extract to a folder called WebViewer.
Figure 1: Folder structure.
Create an index.html file in the myServer project folder and copy and paste the following code inside:
<html>
<head>
<title>Basic PDF Viewer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<!-- Import WebViewer as a script tag -->
<script src='WebViewer/lib/webviewer.min.js'></script>
<body>
<div id='viewer' style='width: 1024px; height: 600px; margin: 0 auto;'></div>
</body>
</html>
Now add the scripts for this project by adding the following code to the body after the viewer div tag.
<script>
WebViewer.WebComponent({
path: 'WebViewer/lib', // path to the Apryse 'lib' folder on your server
licenseKey: 'YOUR_LICENSE_KEY', // sign up to get a key at https://dev.apryse.com
initialDoc: 'WebViewer/samples/files/demo.pdf',
// initialDoc: '/path/to/my/file.pdf', // You can also use documents on your server
}, document.getElementById('viewer'))
.then(instance => {
const { annotationManager, documentViewer, Annotations } = instance.Core;
const searchListener = (searchPattern, options, results) => {
// add redaction annotation for each search result
const newAnnotations = results.map(result => {
const annotation = new Annotations.RedactionAnnotation();
annotation.PageNumber = result.pageNum;
annotation.Quads = result.quads.map(quad => quad.getPoints());
annotation.StrokeColor = new Annotations.Color(136, 39, 31);
return annotation;
});
annotationManager.addAnnotations(newAnnotations);
annotationManager.drawAnnotationsFromList(newAnnotations);
};
documentViewer.addEventListener('documentLoaded', () => {
const searchPattern = 'text to search';
// searchPattern can be something like "search*m" with "wildcard" option set to true
// searchPattern can be something like "search1|search2" with "regex" option set to true
// options default values are false
const searchOptions = {
caseSensitive: true, // match case
wholeWord: true, // match whole words only
wildcard: false, // allow using '*' as a wildcard value
regex: false, // string is treated as a regular expression
searchUp: false, // search from the end of the document upwards
ambientString: true, // return ambient string as part of the result
};
instance.UI.addSearchListener(searchListener);
// start search after document loads
instance.UI.searchTextFull(searchPattern, searchOptions);
});
});
</script>
Figure 2: Location to add the script code.
Save the file.
To start your local server, open a terminal in your myServer project folder and run the command:
http-server -a localhost
Open http://localhost:8080/index.html in your browser.
Note: If you already have the page open, refresh the page.
You should see WebViewer start with a PDF opened. In this case, it will be the demo.pdf file from the sample files included with WebViewer.
Figure 3: WebViewer open with the demo PDF file.
After the document has been loaded, a searchListener callback function is added, and searchTextFull is used to search the document. When the searchListener callback function is called, it'll process the search based on the searchPattern and options specified and then perform any additional code. In the above example, it creates redaction annotations on top of the matching text that are retained even after the search is cleared.
Figure 4: WebViewer showing the results of the search with a red box added to each item found.
WebViewer has additional methods for controlling the search, including:
You can also specify options for the search mode.
See the documentation for more details on other customizations that can be done, including setting the color of the active search result to be displayed as a different color than previous search results. This would be an excellent option for searching for and highlighting different clauses in a contract. Each separate clause would be highlighted in a different color and visible at the same time.
As you can see, ensuring your users have the tailored tools they need by implementing customized, full-text search capabilities into your apps is easy when using WebViewer and JavaScript.
Feel free to check out our demo! You can also get started now or contact our sales team for any questions. And remember to hop on our Discord community for support and discussions.
Tags
webviewer
Garry Klooesterman
Senior Technical Content Creator
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content