我嵌入了一个带有 pdf 和按钮的页面。我希望当用户从 pdf 中选择一些文本并单击按钮时,它应该提醒他/她选择的文本。以下是我的代码:
<html>
<head>
<script>
function getSelText(){
var txt = '';
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
} else return;
//document.Editor.selectedtext.value = txt;
alert(txt);
console.log("Text = "+txt);
}
</script>
</head>
<body>
<input type="button" value="Get selection" onmousedown="getSelText()">
<embed src="SamplePDF.pdf" width="700px" height="650px" id="pdf" onblur="CopyToClipboard(this)">
</body>
</html>
我已经尝试过,但这仅适用于非 pdf 数据,请帮助我。
最佳答案
查看PDF.js ,它是一个常用的 JavaScript 库,包含许多用于 PDF 操作的方法。
1) PDFJS.getDocument( 数据 ).then( 函数(pdf) {
//将您的 Pdf 设置为函数 2) pdf.getPage(i).then( 函数(页面){
//读取页数
3) page.getTextContent().then( 函数(textContent){
//获取数据上下文 对于事件处理程序,
在 viewer.js ,有一些听众喜欢:
window.addEventListener('mousedown', function mousedown(evt) {..}
window.addEventListener('mousemove', function keydown(evt) {..}
您可以使用它们来执行拖动、选择等逻辑。
关于javascript - 如何使用 javascript 提醒 pdf 中选定的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31453827/