Which product are you using?
PDF.js Express Viewer
PDF.js Express Version
8.7.4
Detailed description of issue
{Description here}
I am listening to a click event so that I can get/extract the text or text lines off the page. The event generated was just functions and objects from the native js click event.
Expected behaviour
{Provide a screenshot or description of the expected behaviour}
I want to get the text that was clicked on(mouse or touch event). As pdf pages have several text lines, how can i get the particular text line that was click on?
Does your issue happen with every document, or just one?
{Answer here}
Yes
Link to document
{Provide a link to the document in question if possible}
Code snippet
{Provide a relevant code snippet}
Core.documentViewer.addEventListener("click", (event) => {
console.log(event);
});
Hello there,
PDF.js Express does not support the functionality to get text lines from just a single click.
However, you can get highlighted text from a document. You’ll need to add an event listener to know when the text selection has changed. Here’s a simple example of how you can do this:
const { documentViewer } = instance.Core;
documentViewer.addEventListener('textSelected', (quads, selectedText, pageNumber) => {
console.log(selectedText);
});
In this example, selectedText
will contain the text that the user has selected. The textSelected
event is triggered whenever the selected text changes.
If you want to get the selected text only when the selection is complete, you can use the selectionComplete
event instead along with the getSelectedText()
API to retrieve the selected text.
Here’s how you can do it:
const { documentViewer } = instance.Core;
documentViewer.getTool('TextSelect').addEventListener('selectionComplete', (startLocation, allQuads) => {
const selectedText = documentViewer.getSelectedText();
console.log(selectedText);
});
In this example, selectedText
will contain the text that the user has selected when the mouse button is released.
Hope this helps.
Best Regards,
Darian Chen
Hi,
Thanks for your response.
I am looking for text or text lines extraction after a click on document. The purpose is to get the text within the click parameter and do something with it. Is this possible with Pdf express plus?
Hello there,
Unfortunately, if you’re trying to just click and get text from the document, this is not possible with PDF.js Express Plus either.