How to search with pdf.js?

Published
Published
Description
Slug
URL
https://stackoverflow.com/questions/35501417/how-to-search-with-pdf-js/39770115#39770115
Tags
Date
category
This is an old thread however I think people should know how the package works now. I have managed to make it work the following way. I was using an iframe to display the pdf.
const iframeDocument = document.getElementById('pdf-js-viewer').contentWindow; let searchText = "TheTextYouWantoToHighlight"; iframeDocument.PDFViewerApplication.pdfViewer.findController.executeCommand('find', { caseSensitive: false, findPrevious: undefined, highlightAll: true, phraseSearch: true, query: searchText })
With the latest version of pdfjs, this works but gives error in console to use dispatch event. This code will remove that error and works as expected.
const iframeDocument = document.getElementById('pdf-js-viewer').contentWindow; let searchText = "TheTextYouWantoToHighlight"; iframeDocument.PDFViewerApplication.eventBus.dispatch('find', { caseSensitive: false, findPrevious: undefined, highlightAll: true, phraseSearch: true, query: searchText });