Skip to content
Advertisement

How do I retrieve text from user selection in pdf.js?

This question is specific to pdf.js, a javascript based pdf renderer. I’m building a custom version where I need to extract the text that I select inside the pdf.

There are other posts where you can fetch the text from one page or the whole pdf document such as the one here , but I’m looking to grab a specific text that the user selects and perhaps alert it or print it in the console.

Advertisement

Answer

What you are looking for is window.getSelection() method. This method returns a specific Selection object with the range of the selected text on the web page.

Here is how you can use getSelection() together with pdf.js:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement