我使用带有iframe src = viewer.html?file = …标签的PDF.js嵌入了PDF.我正在使用PDF.js及其viewer.html,因为它已经提供了我在其他任何示例中都找不到的搜索功能.
我希望用户能够点击< td>并使用包含文本搜索PDF并跳转到第一次出现. JSFiddle:http://jsfiddle.net/agyetcsj/@H_502_3@
HTML@H_502_3@
<div id="tableDiv"><table border="1" width="400px"><tr><td>6.5 Calling External Functions</td></tr></table></div> <iframe id="pdfImage" width="600px" height="600px" class="pdf" src="http://mozilla.github.com/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf"></iframe>
JavaScript的@H_502_3@
$('td').unbind('click').click(function () { alert("Find text in PDF!"); });
我在SO上发现了类似的问题,但他们无法真正回答我的问题:@H_502_3@
> https://stackoverflow.com/questions/24439701/searching-embedded-pdfs-in-iframes
> https://stackoverflow.com/questions/28322082/sencha-search-text-in-pdf-file-rendered-from-plugin-pdf-js
> Access PDF.js Viewer functions / events@H_502_3@
谢谢!@H_502_3@
解决方法
受dev-random的回答启发,我将以下代码添加到viewer.js.我通过传递url参数打开我的pdf,例如
http://localhost:3000/pdf/viewer.html?&search=your_search_term.这样,当您打开PDF文件时,将自动执行适合我的用例的搜索.
//Add this piece of code to webViewerInitialized function in viewer.js if ('search' in params) { searchPDF(params['search']); } //New function in viewer.js function searchPDF(td_text) { PDFViewerApplication.findBar.open(); PDFViewerApplication.findBar.findField.value = td_text; PDFViewerApplication.findBar.caseSensitive.checked = true; PDFViewerApplication.findBar.highlightAll.checked = true; PDFViewerApplication.findBar.findNextButton.click(); PDFViewerApplication.findBar.close(); }