pdf高亮显示

现在前端pdf需求越来越多,比如发票的显示,文件的显示,怎么实现具体步骤百度一下吧,这里不做详细介绍,主要记录下遇到的问题

1.页面pdf已经看到了,但是 iframe.contentWindow.PDFViewerApplication显示undefined

当时代码是这么写的,页面已经看到pdf了,但是PDFViewerApplication确实undefined,这样findBar方法也没法用,于是我疯狂百度,搜索原因,很不幸这5个原因里面没有我的原因,不得已,值得从头来,再啃一边pdf.js,终于给我找到了

页面加载完成之后,再赋值

很开心,不仅可以高亮,还可以自动定位到高亮的位置

最后贴一下代码

javascript 复制代码
			var data = res.data
			var ThemeInformationFileUrlInnerhtml =
				`<iframe id="noticeIf" width="100%" style="border-radius: 0"></iframe>`
			$('#notInformationId').html(ThemeInformationFileUrlInnerhtml)
			$("#noticeIf").attr("src", './js/pdf/web/viewer.html?file=' + data.notTextAnnouncementFileUrl);

			// 检查pdf是否正确加载
			var intervalId = window.setInterval(function() {
				let iframe = document.getElementById('noticeIf');
				if (iframe && iframe.contentWindow && iframe.contentWindow.PDFViewerApplication) {
					console.log('pdf已经加载')
					window.clearInterval(intervalId);
					// PDF已加载完成,执行其他操作
					let text = '主要财务数据'
					iframe.contentWindow.postMessage(text, '*')
					iframe.contentWindow.addEventListener('message', (e) => {
						console.log('addEventListener:', e)
						console.log('iframe.contentWindow:', iframe.contentWindow)
						console.log('iframe.contentWindow.PDFViewerApplication:', iframe.contentWindow.PDFViewerApplication)
						iframe.contentWindow.PDFViewerApplication.findBar.findField.value = e.data
						iframe.contentWindow.PDFViewerApplication.findBar.highlightAll.checked = true
						iframe.contentWindow.PDFViewerApplication.findBar.dispatchEvent('highlightallchange')
						console.log('延迟后 iframe.contentWindow.PDFViewerApplication:', iframe.contentWindow
							.PDFViewerApplication)
					}, false)
				} else {
					console.log('pdf还没加载')
				}
			}, 1000); // 每秒检查一次
相关推荐
优化控制仿真模型12 小时前
【2026年最新】英语四级历年真题及答案解析PDF电子版(2015-2025年12月)
经验分享·pdf
asdzx6712 小时前
使用 Python 快速为 PDF 添加背景色或背景图片
python·pdf
其实秋天的枫15 小时前
【26年3月最新】计算机二级WPS真题试题及答案14套电子版PDF(含操作题和选择题)
经验分享·pdf
DeskUI~~15 小时前
倚天剑术34--批量获取PDF文档中的图片
pdf
河北之花15 小时前
演示软件界面及快捷键、幻灯片操作、输出为PDF
pdf·wps
开开心心就好16 小时前
安卓免费证件照制作软件,无广告弹窗
linux·运维·安全·pdf·迭代器模式·依赖倒置原则·1024程序员节
YXWik616 小时前
Linux 环境 libreoffice 执行word转pdf 中文乱码问题
linux·pdf·word
这辈子谁会真的心疼你17 小时前
怎么把caj转化为pdf?批量转换的方法
pdf
南风微微吹17 小时前
【2026年最新】英语四级历年真题及答案解析PDF电子版(2015-2025年12月)
pdf·英语四级
其实秋天的枫1 天前
2025年12月大学英语六级真题及答案电子版pdf三套全
经验分享·pdf