jquery - How to alert selected text from pdf using javascript? -


i have embedded page pdf , button. want when user select text pdf , click on button should alert text he/she has selected. following code :

<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> 

i have tried work non-pdf data, please me out.

check out pdf.js, it's commonly used javascript library contains lot of methods pdf manipulation.

1) pdfjs.getdocument( data ).then( function(pdf) {

// set pdf function 2) pdf.getpage(i).then( function(page){

// read number of pages

3) page.gettextcontent().then( function(textcontent){

//get data context event handler,

in viewer.js, there listeners like:

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

you can use logic of dragging, selecting, ..


Comments