# Text Selection

The PdfViewer supports text selection both from the UI and programmatically.

![WinUI RadPdfViewer ](https://www.telerik.com/winui/documentation/assets/f8189470cb08d1d58c8933b58cfb1a5f/radpdfviewer-text-selection-0.png)

## Selecting Text with the UI

To enable text selection from the UI, set the `Mode` property of `RadPdfViewer` to `TextSelection`. For more information refer to the article on the [PdfViewer interaction modes](https://www.telerik.com/winui/documentation/controls/radpdfviewer/user-interaction/viewer-modes).

> `TextSelection` is part of the [RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview) library which is used for the document model of the PdfViewer.

#### **Set the mode of the PdfViewer**

```C#
this.pdfViewer.Mode = FixedDocumentViewerMode.TextSelection;
```

## Programmatic Selection

The programmatic selection is part of the `RadFixedDocument` API (the `Document` property value of `RadPdfViewer`). To access it, use its `Selection` property, which gives access to a `TextSelection` object that provides a set of properties and methods for selecting text.

The `TextSelection` class introduces the `StartPosition` and `EndPosition` properties that can be used to access the selection range. For more information, refer to the article on [text positioning](https://www.telerik.com/winui/documentation/controls/radpdfviewer/user-interaction/text-position).

#### **Access the current selection positions**

```C#
TextPosition selectionStart = this.pdfViewer.Document.Selection.StartPosition;
TextPosition selectionEnd = this.pdfViewer.Document.Selection.EndPosition;
```

To set the selection, use one of the `TextSelection` methods:

> tip When setting the selection programmatically, the UI will display the selection visual even if the mode of PdfViewer is not set to `TextSelection`.

* `SetSelectionStart`
* `SetSelectionEnd`

#### **Set the selection by using the SetSelectionStart and Set SelectionEnd methods**

```C#
	RadFixedPage page = this.pdfViewer.Document.Pages[0];
	TextPosition start = new TextPosition(page, 100);            
	TextPosition end = new TextPosition(page, 200);
	this.pdfViewer.Document.Selection.SetSelectionStart(start);
	this.pdfViewer.Document.Selection.SetSelectionEnd(end);
```

* `SetSelection`

#### **Set the selection by using the SetSelection method**

```C#
RadFixedPage page = this.pdfViewer.Document.Pages[0];
TextPosition start = new TextPosition(page, 100);            
TextPosition end = new TextPosition(page, 200);
this.pdfViewer.Document.Selection.SetSelection(start, end);
```

* `SelectAll`

#### **Select all the text**

```C#
this.pdfViewer.Document.Selection.SelectAll();
```

* `Clear`

#### **Clear the text selection**

```C#
this.pdfViewer.Document.Selection.Clear();
```

To get the selected text, use the `GetSelectedText` method of `TextSelection`.

#### **Getg the selected text**

```C#
string text = this.pdfViewer.Document.Selection.GetSelectedText();
```

## Events

`TextSelection` provides the `SelectionChanging` and `SelectionChanged` events that can be used to listen for changes in the position.

#### **Subscribe to selection changing events**

```C#
void SubscribeToEvents()
{
	this.pdfViewer.Document.Selection.SelectionChanging += Selection_SelectionChanging;
	this.pdfViewer.Document.Selection.SelectionChanged += Selection_SelectionChanged;
}

private void Selection_SelectionChanging(object sender, EventArgs e)
{		
}

private void Selection_SelectionChanged(object sender, EventArgs e)
{
}
```

## See Also

* [Text Position](https://www.telerik.com/winui/documentation/controls/radpdfviewer/user-interaction/text-position)
* [Scrolling](https://www.telerik.com/winui/documentation/controls/radpdfviewer/user-interaction/scrolling)
* [Interaction Modes](https://www.telerik.com/winui/documentation/controls/radpdfviewer/user-interaction/viewer-modes)
* [Toolbar](https://www.telerik.com/winui/documentation/controls/radpdfviewer/toolbar)
