Search using code on Embedded PDFJS

三世轮回 提交于 2019-12-13 02:28:08

问题


I have integrated PDFJS with my webpage. I want to search using javascript.

It's working fine for first time search. But then I try to search again with different keyword then it's not highlighting proper keyword.

Here is what I have tried:

    // search with PDF.js
    function searchPDF(td_text)
    {
        PDFViewerApplication.findBar.open();
        PDFViewerApplication.findBar.findField.value = td_text;
        PDFViewerApplication.findBar.highlightAll.checked= true;
        PDFViewerApplication.findBar.findNextButton.click();
    }


    function resetPDFSearch()
    {
        if(PDFViewerApplication.findBar.findField.value != '') {
            PDFViewerApplication.findBar.findField.value = '';
            PDFViewerApplication.findBar.highlightAll.checked= false;
            PDFViewerApplication.findController.reset();
            PDFViewerApplication.findBar.close();
            PDFViewerApplication.findController.matchCount = 0;
            PDFViewerApplication.findController.updateMatch();
        }
    }

In above function, when I call searchPDF() first time then keyword is highlighting properly. But again if I call same function with different keyword then it shows previously highlighted keyword only.

I try to create new function resetPDFSearch() to reset all previously filtered and highlighted keywords. But no luck.

Thanks in advance.


回答1:


Here is another solution for those, who uses pdf.viewer.js :

// this method uses the viewers search funtionality to highligt given text
function searchText(txt) {

    if (typeof PDFViewerApplication.findController !== 'undefined') {
        PDFViewerApplication.findController.executeCommand('find', {
            query: txt,
            caseSensitive: false,
            highlightAll: true,
            findPrevious: true
        });
    }

}



回答2:


After so much headache and brainstorming. I come to answer as below.

function searchPDF(td_text)
{
    PDFView.findBar.open();
    $(PDFView.findBar.findField).val(td_text);

    var event = document.createEvent('CustomEvent');
    event.initCustomEvent('findagain', true, true, {
        query: td_text,
        caseSensitive: $("#findMatchCase").prop('checked'),
        highlightAll: $("#findHighlightAll").prop('checked', true),
        findPrevious: undefined
    });

    PDFViewerApplication.findBar.dispatchEvent('');

    return event;
}

No need for resetPDFSearch() function.

This case is for my scenario. Hope you might have different case. But yes using event I can search as many time as I want. :)

Might be helpful to someone in future.



来源:https://stackoverflow.com/questions/34717771/search-using-code-on-embedded-pdfjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!