PDFJS 查找并导航到页面
PDFJS Find and Naviage to page
我们正在使用 PDFJS highlight/Find 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 选项修复
我们正在使用 PDFJS highlight/Find 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 选项修复