我有一个网络应用程序,它使用 primefaces 扩展中的 documentViewer 来查看 pdf 文件。
我需要使用 javascript 将查询传递给 phraseSearch,以便在找到查询的正确位置打开 pdf 文件。
我将在以下位置使用此 javascript (pdfQuery)我用来打开 documentViewer 的按钮的 oncomplete 属性:
<p:commandButton action="#{searchController.openPDF}"
oncomplete="PF('pdf_dlg').show();pdfQuery('Framework')"
value="open PDF" update="pdf" />
<p:dialog header="#{searchController.pdfTitle}"
widgetVar="pdf_dlg"
responsive="true" dynamic="true" >
<h:form id="pdf">
<pe:documentViewer value="#{pdfBean.tempPdfFile}"/>
</h:form>
</p:dialog>
我在与 PDF.js 相关的 SO 上发现了类似的问题,但与 primefaces 扩展无关:
PDF.js - Using search function on embedded PDF
Search using code on Embedded PDFJS
有什么想法可以实现这一点吗?
如何访问嵌入的 primefaces pdf.js?
提前非常感谢。
编辑:
现在我被困在这里了。这段 JavaScript 不起作用:
function triggerSearch(tx_query) {
$('iframe').on('load',
function () {
if (typeof PDFViewerApplication.findController !== 'undefined') {
PDFViewerApplication.findController.executeCommand('find', {
query: tx_query,
caseSensitive: false,
highlightAll: true,
findPrevious: true
});
}
});
}
不幸的是,来自 pdf.viewer.js
的方法 PDFViewerApplication
无法被识别。我不知道如何访问它。
最佳答案
导航至此处的展示页面: https://www.primefaces.org/showcase-ext/sections/documentviewer/advanced.jsf
现在使用 Chrome 或您喜欢的浏览器的 F12 打开控制台窗口。 输入以下代码:
window.frames[0].PDFViewerApplication.findBar.open();
注意到 FindBar 打开了吗?因此,现在您可以通过访问 PDFViewerApplication 来编写一个完全符合您要求的脚本。
var pdfViewer = window.frames[0].PDFViewerApplication;
pdfViewer.findBar.open();
pdfViewer.findBar.findField.value = "My text";
pdfViewer.findBar.highlightAll.checked= true;
pdfViewer.findBar.findNextButton.click();
关于javascript - 在嵌入式 pe 上使用 javascript 进行搜索 :documentViewer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58781170/