当前位置: 代码迷 >> JavaScript >> 如何使用javascript提醒pdf中的选定文本?
  详细解决方案

如何使用javascript提醒pdf中的选定文本?

热度:49   发布时间:2023-06-08 09:20:54.0

我嵌入了一个带有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数据,请帮帮我。

查看 ,它是一个常用的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) {..}

您可以使用它们来执行拖动,选择,..的逻辑。

  相关解决方案