我们正在使用PDFJS突出显示/查找PDF文件中的文本。那很好。另一个功能是导航到特定页面。我们正在尝试设置PdfApplicationViewer的CurrentPage属性。但这会引发错误
没有设置parentOffset。无法滚动
随附了下面的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 Option进行修复
关于javascript - PDF JS查找并导航到页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48768491/