问题描述
我嵌入了一个带有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数据,请帮帮我。
1楼
查看 ,它是一个常用的JavaScript库,其中包含许多用于PDF操作的方法。
1)PDFJS.getDocument(data).then(function(pdf){
//将您的Pdf设置为Function 2)pdf.getPage(i).then(function(page){
//阅读页数
3)page.getTextContent()。then(function(textContent){
//获取事件处理程序的数据上下文
在 ,有一些侦听器,例如:
window.addEventListener('mousedown', function mousedown(evt) {..}
window.addEventListener('mousemove', function keydown(evt) {..}
您可以使用它们来执行拖动,选择,..的逻辑。