chenglong

How to search with pdf.js?

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 });

Copyright © 2024 chenglong

logo