将以下代码添加到了viewer.js 中。我通过传递 url 参数来打开我的 pdf。
不知道大家这个嵌入式pdf的代码是在前端还是后端,但是我在的公司这个代码是在后端的,导致我找了一上午也没有找到,温馨提示一下:前端搜不到的话,去后端全局搜索一下。
//viewer.js判断一下地址栏参数是否存在。
//PDFViewerApplication,这个变量名字是整个生成pdf的对象
var queryString = document.location.search.substring(1);
var params = PDFViewerApplication.parseQueryString(queryString);
var textSearch = 'search' in params ? params.search : null;
// *上面三行代码是因为,我这个viewer.js在后端,所有语法是在后端使用的。前端的话,直接通过路由获取就好了
if (textSearch) {
searchPDF(textSearch);
}
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();
}