Some test text!

menu
Text select eventskeyboard_arrow_down

Extracting selected text for a document

linkGetting selected text

Text selected in a document can be extracted using the following functions:

getSelectedTextQuads will return an array for each line of selected text. The following is an example of it being used

Selected Text

const quads = docViewer.getSelectedTextQuads(docViewer.getCurrentPage());

In the above sample, getSelectedTextQuads will return an array of Quad objects. The array will contain three Quad objects, one for each rectangle of selected text.

It's possible for a user to select text across multiple pages so if getSelectedTextQuads is called without any parameters then Quads will be returned for every page with selected text. The result is an object with keys for each page number:

{
  2: [{ /* quad1 */ }, { /* quad2 */ }, { /* quad3 */ }]
}

Where '2' is the page number where the selected text is found.

Selected Text on multiple pages

For the image above there is text selected on pages 2 and 3 so the result of getSelectedTextQuads will look like this:

{
  2: [{ /* quad1 */ }, { /* quad2 */ }],
  3: [{ /* quad3 */ }]
};

linkGetting selected text from events

Be notified when text is selected using the textSelected event as shown below:

WebViewer({ ... }, viewerElement).then(instance => {
  const { docViewer } = instance;

  docViewer.on('textSelected', (quads, selectedText, pageNumber) => {
    // quads will be an array of 'Quad' objects
    // text is the selected text as a string
    if (selectedText.length > 0) {
      console.log(selectedText);
    }
  });
});

The textSelected event get fired whenever another character is selected or deselected. Also the textSelected event fires once for each page with selected text. So if text is selected on page A and B, and the selected text on page A changed, the textSelected event will fire once for each page even though there wasn't any changes for page B.

linkGetting selected text from browser events

Get selected text by listening for a keydown or clipboard copy event.

WebViewer({ ... }, viewerElement).then(instance => {
  const { docViewer } = instance;
  const showSelectedText = () => {
    const page = docViewer.getCurrentPage();
    const text = docViewer.getSelectedText(page);
    
    if (!!text) {
        console.log(text);
    }
  }

  // Optionally use keyDown events
  docViewer.on('keyDown', function (je, e) {
    if (e.keyCode == 67 && (e.ctrlKey || e.metaKey)) {
      showSelectedText();
      console.log('Ctrl+C pressed');
    }
  });

  // Otherwise use the copy event
  var iframeWindow = instance.iframeWindow;
  iframeWindow.addEventListener('copy', function (e) {
      showSelectedText();
      console.log('Ctrl+C (copy) pressed');
  });
});

linkGetting selection complete from events

Use the text selection complete event with the TextSelect tool and listen for selectionComplete.

WebViewer({ ... }, viewerElement).then(instance => {
  const { docViewer } = instance;

  docViewer.getTool('TextSelect').on('selectionComplete', (startQuad, allQuads) => {
    // the startQuad and allQuads will have the X and Y values you want
  });
});

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close