PDF.js - Using search function on embedded PDF

前端 未结 4 541
野趣味
野趣味 2020-12-16 19:51

I embedded a PDF using PDF.js with the iframe src=viewer.html?file=... tag. I\'m using PDF.js and its viewer.html as it already provides a search function that

4条回答
  •  温柔的废话
    2020-12-16 20:13

    Inspired by dev-random's answer I added following code to viewer.js. I open my pdf by passing url parameters e.g. http://localhost:3000/pdf/viewer.html?&search=your_search_term. This way when you open the PDF file, the search is automatically performed which suits my usecase.

    //Add this piece of code to webViewerInitialized function in viewer.js
    if ('search' in params) {
        searchPDF(params['search']);
    }
    
    //New function in viewer.js
    function searchPDF(td_text) {
        PDFViewerApplication.findBar.open();
        PDFViewerApplication.findBar.findField.value = td_text;
        PDFViewerApplication.findBar.caseSensitive.checked = true;
        PDFViewerApplication.findBar.highlightAll.checked = true;
        PDFViewerApplication.findBar.findNextButton.click();
        PDFViewerApplication.findBar.close();
    }
    

提交回复
热议问题