How to search with pdf.js?

后端 未结 1 1401
温柔的废话
温柔的废话 2020-12-25 15:37

I\'m displaying a pdf file with pdf.js in my Ionic App. I do not use viewer.js and viewer.html, because I need a totally different layout. Now I have a custom search bar and

1条回答
  •  轮回少年
    2020-12-25 15:54

    Now I found the solution!

    var container = document.getElementById('viewerContainer');
    var viewer = document.getElementById('viewer');
    
    
    var pdfViewer = new PDFViewer({ 
       container: container,
       viewer: viewer
    });
    
    $scope.pdfFindController = new PDFFindController({
       pdfViewer: pdfViewer
    );
    
    pdfViewer.setFindController($scope.pdfFindController);
    
    container.addEventListener('pagesinit', function () {
        pdfViewer.currentScaleValue = 'page-width';                            
    });
    
    PDFJS.getDocument(MY_PATH_TO_THE_PDF).then(function (pdfDocument) {
        pdfViewer.setDocument(pdfDocument);
    });
    

    Search for terms:

    $scope.pdfFindController.executeCommand('find', {
        caseSensitive: false, 
        findPrevious: undefined,
        highlightAll: true, 
        phraseSearch: true, 
        query: "myQuery"
    });
    

    And I had to import the viewer.js.

    The code that I posted in the question is not needed anymore. The PDFViewer renders the pdf.

    0 讨论(0)
提交回复
热议问题