Text Search Events in JavaScript PDF Viewer
14 Jan 20263 minutes to read
The PDF Viewer triggers events during text search operations, allowing you to customize behavior and respond to different stages of the search process.
textSearchStart
The textSearchStart event fires as soon as a search begins from the toolbar interface or through the textSearch.searchText method. Use it to reset UI state, log analytics, or cancel the default search flow before results are processed.
- Event arguments: TextSearchStartEventArgs exposes:
-
searchText: the term being searched. -
matchCase: indicates whether case-sensitive search is enabled. -
isMatchWholeWord: indicates whether whole-word matching is enabled. -
name: event name. -
cancel: set totrueto stop the default search.
-
var viewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
textSearchStart: function(args) {
// args.searchText contains the term being searched
// args.cancel can be set to true to stop the default search
console.log('Text search started for: "' + args.searchText + '"');
}
});
viewer.appendTo('#pdfViewer');textSearchHighlight
The textSearchHighlight event triggers whenever a search result is brought into view, including navigation between matches. Use it to draw custom overlays or synchronize adjacent UI elements when a match is highlighted.
- Event arguments: TextSearchHighlightEventArgs exposes:
-
bounds:RectangleBoundsModel | RectangleBoundsModel[]representing the highlighted match. -
pageNumber: page index where the match is highlighted. -
searchText: the active search term. -
matchCase: indicates whether case-sensitive search was used. -
name: event name.
-
var viewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
textSearchHighlight: function(args) {
// args.bounds provides the rectangle(s) of the current match
console.log('Highlighted match bounds:', args.bounds);
}
});
viewer.appendTo('#pdfViewer');textSearchComplete
The textSearchComplete event runs after the search engine finishes scanning the document for the current query. Use it to update match counts, toggle navigation controls, or notify users when no results were found.
- Typical uses:
- Update UI with the total number of matches and enable navigation controls.
- Hide loading indicators or show a “no results” message if none were found.
- Record analytics for search effectiveness.
- Event arguments: TextSearchCompleteEventArgs exposes:
-
totalMatches: total number of occurrences found. -
isMatchFound: indicates whether at least one match was found. -
searchText: the searched term. -
matchCase: indicates whether case-sensitive search was used. -
name: event name.
-
var viewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
textSearchComplete: function(args) {
// args.totalMatches may indicate how many results were found (when available)
console.log('Text search completed.', args);
}
});
viewer.appendTo('#pdfViewer');See Also
Text Search Features
Find Text
Extract Text
Extract Text Options
Extract Text Completed