首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDFJS查找和导航到页面

PDFJS查找和导航到页面
EN

Stack Overflow用户
提问于 2018-02-13 13:55:37
回答 1查看 2.9K关注 0票数 2

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

parentOffset未设置。不能卷轴

已经附加了下面的js代码。

代码语言:javascript
复制
 '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);

                });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-13 05:14:06

设法用setCurrenPage选项修复

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48768491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档