如何使用javascript从pdf提醒所选文本?

时间:2015-07-16 12:12:41

标签: javascript jquery

我嵌入了一个带有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 个答案:

答案 0 :(得分:0)

查看PDF.js,这是一个常用的JavaScript库,其中包含许多PDF操作方法。

1)PDFJS.getDocument(data).then(function(pdf){

//将你的Pdf设置为函数 2)pdf.getPage(i).then(function(page){

//读取页数

3)page.getTextContent()。then(function(textContent){

//获取数据上下文 对于事件处理程序,

viewer.js中,有一些听众喜欢:

window.addEventListener('mousedown', function mousedown(evt) {..}
window.addEventListener('mousemove', function keydown(evt) {..}

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