# Extract selected text from PDFs programmatically

Nutrient Web SDK comes with a reliable, cross-browser API to access text selection and retrieve both selected text and text lines located 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

Text selection for the current PDF document can be retrieved at any time using the [`Instance#getTextSelection`](https://www.nutrient.io/api/web/NutrientViewer.Instance.html#getTextSelection) method, which 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

```

Since `TextSelection` is an immutable object, its value won’t change over time — for example, when the current text is deselected or some other text is selected. In order to get the updated text selection, [`Instance#getTextSelection`](https://www.nutrient.io/api/web/NutrientViewer.Instance.html#getTextSelection) must be invoked again.

Once a `TextSelection` exists, the selected text can be retrieved 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 selection’s `startNode` and `endNode`, are available on `TextSelection`s. Please refer to the [API docs](https://www.nutrient.io/api/web/NutrientViewer.TextSelection.html) for further details.

## 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`.
---

## Related pages

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

