---
title: "Programmatically extract selected text from PDF using JavaScript | Nutrient"
canonical_url: "https://www.nutrient.io/guides/web/features/text-selection/"
md_url: "https://www.nutrient.io/guides/web/features/text-selection.md"
last_updated: "2026-11-04T00:00:00.000Z"
description: "Discover how to programmatically access and manage text selection in Nutrient Web SDK, using the reliable `getTextSelection` method for PDFs."
---

# Extract selected text from PDFs programmatically

Nutrient Web SDK provides a cross-browser API to access text selection. The API retrieves the selected text and the text lines within a given selection range.

Each Nutrient instance can have only one selection at a time. The current text selection for a document has the shape of a [`NutrientViewer.TextSelection`](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html) object.

## Getting text selection programmatically

Call the [`Instance#getTextSelection`](https://www.nutrient.io/api/web/NutrientViewer.Instance.html#getTextSelection) method at any time to retrieve the text selection for the current PDF document. It returns an instance of `TextSelection`:

### ES6+

```js

const textSelection = instance.getTextSelection();
console.log(textSelection instanceof NutrientViewer.TextSelection);
// > true

```

### JAVASCRIPT

```js

const textSelection = instance.getTextSelection();
console.log(textSelection instanceof NutrientViewer.TextSelection);
// > true

```

`TextSelection` is an immutable object, so its value doesn’t change when the user deselects the current text or selects other text. To get the updated text selection, call [`Instance#getTextSelection`](https://www.nutrient.io/api/web/NutrientViewer.Instance.html#getTextSelection) again.

Once a `TextSelection` exists, retrieve the selected text with [`getText`](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html#getText). This method is asynchronous, so it returns a `Promise`:

### ES6+

```js

const textSelection = instance.getTextSelection();
const text = await textSelection.getText();
console.log(text);

```

### JAVASCRIPT

```js

const textSelection = instance.getTextSelection();
textSelection.getText().then(function (text) {
  console.log(text);
});

```

[`TextSelection`](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html) objects expose two other methods:

- [`getSelectedTextLines`](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html#getSelectedTextLines), which returns the `TextLine`s rendered within a given selection range.

- [`getBoundingClientRect`](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html#getBoundingClientRect), which returns the bounding box of the current text selection in client coordinates.

Other useful properties, like a selection’s `startNode` and `endNode`, are available on `TextSelection`s. For further details, refer to the [API documentation](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html).

## Subscribing to change events

Text selection updates are dispatched every time a text is selected or deselected. Subscribing to the `textSelection.change` event ensures a notification when the selection changes:

### ES6+

```js

instance.addEventListener("textSelection.change", textSelection => {
  if (textSelection) {
    textSelection.getText().then(text => {
      console.log(text);
    });
  } else {
    console.log("no text is selected");
  }
});

```

### JAVASCRIPT

```js

instance.addEventListener("textSelection.change", function(textSelection) {
  if (textSelection) {
    textSelection.getText().then(function(text) {
      console.log(text);
    });
  } else {
    console.log("no text is selected");
  }
});

```

Notice that when the text is deselected, the `textSelection` argument is `null`.

## Why a selection can include text from another column

When you drag to select text, Nutrient Web SDK selects everything between the start and the end of the drag. It uses the order in which the PDF stores the text. In an untagged PDF, the Web SDK doesn’t re-sort the text by its position on the page.

In most documents, the stored order matches the reading order. It can differ in scanned documents with an optical character recognition (OCR) text layer. There, the OCR tool’s layout analysis decides the order and may split a sidebar or box into parts — for example, around an image. Selecting a passage in one column then also selects any part of the box stored between the selection’s start and end. CSS can’t change this behavior, because it changes how text is displayed, not the order in which the PDF stores it.

To keep a selection within its column, fix the order in the document:

- **Store each block together** — If you control the OCR or PDF generation step, store each column, box, or sidebar as one block.

- **Tag the box as one element** — In a tagged PDF, the Web SDK groups text by tag. It selects everything in one element together, at the position where the PDF stores that element’s first text. Tag each box or sidebar as a single element, such as `Div`, and make sure its first part isn’t stored inside another column’s text. Tagging each line separately without grouping them, or only reordering the tags, doesn’t change the selection order.
---

## Related pages

- [JavaScript PDF extraction library](/guides/web/extraction.md)
- [Extract pages from PDFs using JavaScript](/guides/web/extraction/page-extraction.md)
- [JavaScript PDF parser library](/guides/web/extraction/parse-content.md)
- [Read text from PDFs using JavaScript](/guides/web/extraction/read-text.md)
- [Extract text from PDFs using JavaScript](/guides/web/features/text-extraction.md)

