javascript - PDFJS 查找并导航至页面

标签 javascript jquery pdf pdf.js

我们正在使用 PDFJS 突出显示/查找 PDF 文件中的文本。效果很好。另一个功能是导航到特定页面。我们正在尝试设置 PdfApplicationViewer 的 CurrentPage 属性。但它会抛出错误,例如

parentOffset not set. Cannot Scroll

附上下面的js代码。

 'use strict';

                if (!PDFJS.PDFViewer || !PDFJS.getDocument) {
                    alert('Please build the pdfjs-dist library using\n' + '  `gulp dist-install`');
                }

                // The workerSrc property shall be specified.
                //
                PDFJS.workerSrc = 'lib/pdfviewer/pdf.worker.js';

                // Some PDFs need external cmaps.
                //
                // PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
                // PDFJS.cMapPacked = true;

                var DEFAULT_URL = PdfUrl;
                var SEARCH_FOR = SearchWord; // try 'Mozilla';

                var container = document.getElementById('viewerContainer');

                // (Optionally) enable hyperlinks within PDF files.
                var pdfLinkService = new PDFJS.PDFLinkService();

                var pdfViewer = new PDFJS.PDFViewer({
                    container: container,
                    linkService: pdfLinkService,
                });

                pdfLinkService.setViewer(pdfViewer);

                // (Optionally) enable find controller.
                var pdfFindController = new PDFJS.PDFFindController({
                    pdfViewer: pdfViewer
                });
                pdfViewer.setFindController(pdfFindController);

                container.addEventListener('pagesinit', function () {
                    // We can use pdfViewer now, e.g. let's change default scale.
                    pdfViewer.currentScaleValue = 'page-width';
                    if (SEARCH_FOR) { // We can try search for things
                        pdfFindController.executeCommand('find', {
                            caseSensitive: false,
                            findPrevious: undefined,
                            highlightAll: true,
                            phraseSearch: true,
                            query: SEARCH_FOR,

                        });
                    }
                });

                // Loading document.
                PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) {
                    // Document loaded, specifying document for the viewer and
                    // the (optional) linkService.
                    pdfViewer.setDocument(pdfDocument);
                    pdfLinkService.setDocument(pdfDocument, null);
                    PDFViewerApplication.page = parseInt(SearchPageNo);

                });

最佳答案

设法使用 setCurrenPage 选项修复

关于javascript - PDFJS 查找并导航至页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768491/

相关文章:

javascript - 动画中的相对旋转

javascript - 使用 filter() thisArg 编译 TypeScript

javascript - 缩进通过 createTextNode() 创建的代码字符串?

javascript - 尝试使用 jQuery 查找在 HTML 页面中动态创建的输入变量

java - 使用 Java 将 PDF(1.4 和 1.5)可靠地打印到网络打印机

android - android开发中如何知道下载是否完成

javascript - github 中的 Codacy 功能给了我一些我无法理解的错误

jquery - javascript/jQuery setInterval/clearInterval

javascript - 具有多个数组映射的 JQuery 数组

java - 来自 mysql 连接的 itextpdf pdf