---
title: "Extract selected text from PDF on Android | Nutrient SDK"
canonical_url: "https://www.nutrient.io/guides/android/extraction/selected-text/"
md_url: "https://www.nutrient.io/guides/android/extraction/selected-text.md"
last_updated: "2026-06-09T10:25:14.336Z"
description: "Add custom functionality when a user selects text by implementing TextSelectionManager.OnTextSelectionChangeListener, or programmatically select and retrieve text content by using PdfFragment#enterTextSelectionMode on a specified character range on Android."
---

# Extract selected text from PDFs on Android

Nutrient’s document viewing UI has built-in support for text selection, which was designed to exactly match the text selection behavior in stock Android components. The text selection UI includes full support for standard touch- and cursor-based text selection gestures, as well as a built-in contextual menu for quick access to operations such as copying and highlighting.

In addition to the bundled UI behaviors, it’s also possible to build additional operations on the selected text by leveraging the provided API hooks. This guide covers two convenient options.

## Text selection listener

You can create custom listeners for [`TextSelectionManager`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.special_mode.manager/-text-selection-manager/index.html) to handle changes in text selection and text selection mode (entering and exiting):

### MYACTIVITY.KT

```kt

class MyActivity : PdfActivity(), TextSelectionManager.OnTextSelectionChangeListener, TextSelectionManager.OnTextSelectionModeChangeListener {

  const val TAG = "MyActivity.TextSelection"

  override fun onCreate(savedInstanceState: Bundle?) {
    PdfFragment.addOnTextSelectionModeChangeListener(this)
    PdfFragment.addOnTextSelectionChangeListener(this)
  }

  override fun onDestroy(savedInstanceState: Bundle?) {
    PdfFragment.removeOnTextSelectionModeChangeListener(this)
    PdfFragment.removeOnTextSelectionChangeListener(this)
  }

  override fun onEnterTextSelectionMode(controller: TextSelectionController) {
    Log.i(TAG, "Text selection mode has started.")
  }

  override fun onExitTextSelectionMode(controller: TextSelectionController) {
    Log.i(TAG, "Text selection mode has ended.")
  }

  override fun onTextSelectionChange(newTextSelection: TextSelection?, currentTextSelection: TextSelection?): Boolean {
    if (newTextSelection!= null) {
      Log.i(TAG, "Selected text was: ${newTextSelection.text}")
    } else {
      Log.i(TAG, "Text selection is cleared.")
    }
    // Return `false` to prevent changes to the current selection state, if needed.
    return true
  }
}

```

### MYACTIVITY.JAVA

```java

public class MyActivity extends PdfActivity implements TextSelectionManager.OnTextSelectionChangeListener,
    TextSelectionManager.OnTextSelectionModeChangeListener{

  private static final String TAG = "MyActivity.TextSelection";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      getPdfFragment().addOnTextSelectionModeChangeListener(this);
      getPdfFragment().addOnTextSelectionChangeListener(this);
  }

  @Override
  protected void onDestroy(Bundle savedInstanceState) {
      getPdfFragment().removeOnTextSelectionModeChangeListener(this);
      getPdfFragment().removeOnTextSelectionChangeListener(this);
  }

  @Override
  public void onEnterTextSelectionMode(@NonNull TextSelectionController controller) {
      Log.i(TAG, "Text selection mode has started.");
  }

  @Override
  public void onExitTextSelectionMode(@NonNull TextSelectionController controller) {
      Log.i(TAG, "Text selection mode has ended.");
  }

  @Override
  public boolean onTextSelectionChange(@Nullable TextSelection newTextSelection, @Nullable TextSelection currentTextSelection) {
      if (newTextSelection!= null) {
          Log.i(TAG, String.format("Selected text was: %s", newTextSelection.text));
      } else {
          Log.i(TAG, "Text selection is cleared.");
      }

      // Return `false` to prevent changes to the current selection state, if needed.
      return true;
  }
}

```

By implementing these listeners, you can add custom reactions to text selection events.

This setup doesn’t override the default behavior — it only adds additional listeners. If you need to completely change the behavior (e.g. replacing the toolbar with a custom user interface), use [`PdfFragment`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-fragment/index.html) in a custom activity and bind it to the [`TextSelectionController`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.special_mode.controller/-text-selection-controller/index.html).





## Selecting and extracting text programmatically

You can programmatically select text using the [`PdfFragment#enterTextSelectionMode`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-fragment/enter-text-selection-mode.html) method and specifying the text range to be selected. You can retrieve the text of the current page by calling the [`PdfDocument#getPageText`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-pdf-document/get-page-text.html) method.

For example, to select the first occurrence of a string on the page, use the following code:

### KOTLIN

```kotlin

// Search for the position of text that should be selected.
val textToSelect = "text to select"
val textIndexOnPage = document.getPageText(pageIndex).indexOf(textToSelect)
if (textIndexOnPage >= 0) {
    // Select the text.
    fragment.enterTextSelectionMode(pageIndex, Range(textIndexOnPage, textToSelect.length))
}

```

### JAVA

```java

// Search for the position of text that should be selected.
String textToSelect = "text to select";
int textIndexOnPage = document.getPageText(pageIndex).indexOf(textToSelect);
if (textIndexOnPage >= 0) {
    // Select the text.
    fragment.enterTextSelectionMode(pageIndex, new Range(textIndexOnPage, textToSelect.length()));
}

```





Once the text is selected, the [`TextSelectionManager`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.special_mode.manager/-text-selection-manager/index.html) listeners can be used to extract the selected text.
---

## Related pages

- [PDF extraction library for Android](/guides/android/extraction.md)
- [Extract metadata from PDFs on Android](/guides/android/extraction/metadata.md)
- [Extract pages from PDFs on Android](/guides/android/extraction/page-extraction.md)
- [Effortlessly parse PDF content on Android](/guides/android/extraction/parse-content.md)
- [Easily extract text from PDFs on Android](/guides/android/features/text-extraction.md)
- [Extract the text position from PDFs on Android](/guides/android/extraction/text-position.md)

