# Text Selection

**RadPdfViewer** supports selection both through the UI and programmatically.

## Selection through the UI

In order to be able to select text from the UI, **RadPdfViewer** must be in *TextSelection* mode. The mode of the viewer is specified by the **ViewerMode** property, which could be set as follows:

#### ViewerMode

```C#
this.radPdfViewer1.ViewerMode = Telerik.WinControls.UI.FixedDocumentViewerMode.TextSelection;

```

```VB.NET
Me.RadPdfViewer1.ViewerMode = Telerik.WinControls.UI.FixedDocumentViewerMode.TextSelection
```

## Programmatic Selection

When it comes to programmatic selection, it can occur even if the **ViewerMode** property of **RadPdfViewer** is not *TextSelection*.

All methods, properties and events relevant to the discussed functionality are contained in the **TextSelection** class.

* **Properties**:

  * **StartPosition**: The **TextPosition** at which the selection starts.

  * **EndPosition**: The **TextPosition** denoting the end of the selection respectively.

  * **IsEmpty**:  A boolean property showing if there is selection in the current document.

* **Methods**:The methods for manipulating the selection include:

  * **SetSelectionStart(TextPosition startPosition)**

  * **SetSelectionEnd(TextPosition endPosition)**

  * **SetSelection(TextPosition startPosition, TextPosition endPosition)**

  * **SelectAll()**

  * **Clear()**

  * **GetSelectedText()**

    * **GetSelectedTextAsync(Action\<string> callback)**

Their names show clearly the action they execute.Here is an example showing how the functionality of **TextPositions** can be combined with that of **TextSelection** in order to select the word that the caret is currently positioned in:

#### Text selection

```C#
Telerik.Documents.Fixed.Text.TextPosition start = new Telerik.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document.CaretPosition);
start.MoveToCurrentWordStart();
Telerik.Documents.Fixed.Text.TextPosition end = new Telerik.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document.CaretPosition);
end.MoveToCurrentWordEnd();
this.radPdfViewer1.Document.Selection.SetSelection(start, end);
RadMessageBox.Show(this.radPdfViewer1.Document.Selection.GetSelectedText());

```

```VB.NET
Dim start As New Telerik.Documents.Fixed.Text.TextPosition(Me.RadPdfViewer1.Document.CaretPosition)
start.MoveToCurrentWordStart()
Dim [end] As New Telerik.Documents.Fixed.Text.TextPosition(Me.RadPdfViewer1.Document.CaretPosition)
[end].MoveToCurrentWordEnd()
Me.RadPdfViewer1.Document.Selection.SetSelection(start, [end])
RadMessageBox.Show(Me.RadPdfViewer1.Document.Selection.GetSelectedText())
```

There are various methods in the API of RadPdfViewerElement which you can use to manipulate the text selection.

The **SelectAll**, **DeselectAll**, **Select** methods allow you to set the selection programmatically:

#### Select/Deselect

```C#
private void buttonSelectAll_Click(object sender, EventArgs e)
{
    this.radPdfViewer1.PdfViewerElement.SelectAll();
}

private void buttonDeselectAll_Click(object sender, EventArgs e)
{
    this.radPdfViewer1.PdfViewerElement.DeselectAll();
}

private void buttonSelect_Click(object sender, EventArgs e)
{
    Telerik.Documents.Fixed.Text.TextPosition startPosition = new Telerik.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document);
    Telerik.Documents.Fixed.Text.TextPosition endPosition = new Telerik.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document);
    startPosition.MoveToLineStart();
    endPosition.MoveToLineEnd();

    //Select the first line in the document
    this.radPdfViewer1.PdfViewerElement.Select(startPosition, endPosition);
}

```

```VB.NET
Private Sub buttonSelectAll_Click(sender As Object, e As EventArgs) Handles buttonSelectAll.Click
    Me.radPdfViewer1.PdfViewerElement.SelectAll()
End Sub

Private Sub buttonDeselectAll_Click(sender As Object, e As EventArgs) Handles buttonDeselectAll.Click
    Me.radPdfViewer1.PdfViewerElement.DeselectAll()
End Sub

Private Sub buttonSelect_Click(sender As Object, e As EventArgs) Handles buttonSelect.Click
    Dim startPosition As New Telerik.Documents.Fixed.Text.TextPosition(Me.radPdfViewer1.Document)
    Dim endPosition As New Telerik.Documents.Fixed.Text.TextPosition(Me.radPdfViewer1.Document)
    startPosition.MoveToLineStart()
    endPosition.MoveToLineEnd()

    'Select the first line in the document
    Me.radPdfViewer1.PdfViewerElement.[Select](startPosition, endPosition)
End Sub

```

You can use the **GetSelectedText**, **GetSelectedTextAsync** methods to get the currently selected text. The **GetSelectedTextAsync** method does this operation asynchronously and calls the specified callback function when ready. The **Copy** method copies the current selection to the clipboard. It executes the operation in the background, so the text is not copied to the clipboard until the waiting indicator is visible.The following sample demonstrates using these methods:

#### Get Selection Async

```C#
private void butonGetSelectedText_Click(object sender, EventArgs e)
{
    RadMessageBox.Show("The current selection: " + this.radPdfViewer1.PdfViewerElement.GetSelectedText());
}

private void buttonGetSelectedTextAsync_Click(object sender, EventArgs e)
{
    Task<string> selectedTextTask = this.radPdfViewer1.PdfViewerElement.GetSelectedTextAsync();
    selectedTextTask.ContinueWith((t) => { RadMessageBox.Show("Selected text: " + t.Result); });
}

private void buttonCopy_Click(object sender, EventArgs e)
{
    this.radPdfViewer1.PdfViewerElement.Copy();
}

```

```VB.NET
Private Sub buttonGetSelectedText_Click(sender As Object, e As EventArgs) Handles buttonGetSelectedText.Click
    RadMessageBox.Show("The current selection: " & Me.radPdfViewer1.PdfViewerElement.GetSelectedText())
End Sub

Private Sub buttonGetSelectedTextAsync_Click(sender As Object, e As EventArgs) Handles buttonGetSelectedTextAsync.Click
    Dim selectedTextTask As Task(Of String) = Me.radPdfViewer1.PdfViewerElement.GetSelectedTextAsync()
    selectedTextTask.ContinueWith(Function(t)
                                      RadMessageBox.Show("Selected text: " & t.Result)
                                  End Function)
End Sub

Private Sub buttonCopy_Click(sender As Object, e As EventArgs) Handles buttonCopy.Click
    Me.radPdfViewer1.PdfViewerElement.Copy()
End Sub

```

The **Find** and **FindPrevious** methods are used to perform text search forwards or backwards respectively. These methods return the result in a special **SearchResult** structure which provides information about the result:

#### Find Next

```C#
private void buttonFindNext_Click(object sender, EventArgs e)
{
    Telerik.Documents.Fixed.Search.SearchResult res;
    if (this.checkSearchBackwards.IsChecked)
    {
        res = this.radPdfViewer1.PdfViewerElement.FindPrevious("WinForms");
    }
    else
    {
        res = this.radPdfViewer1.PdfViewerElement.Find("WinForms");
    }

    if (res == Telerik.Documents.Fixed.Search.SearchResult.NotFound)
    {
        RadMessageBox.Show("String not found");
    }
    else
    {
        RadMessageBox.Show("Result found on page " + res.Range.StartPosition.Page.PageNo);
    }
}

```

```VB.NET
Private Sub buttonFindNext_Click(sender As Object, e As EventArgs) Handles buttonFindNext.Click
    Dim res As Telerik.Documents.Fixed.Search.SearchResult
    If Me.checkSearchBackwards.IsChecked Then
        res = Me.radPdfViewer1.PdfViewerElement.FindPrevious("WinForms")
    Else
        res = Me.radPdfViewer1.PdfViewerElement.Find("WinForms")
    End If

    If res Is Telerik.Documents.Fixed.Search.SearchResult.NotFound Then
        RadMessageBox.Show("String not found")
    Else
        RadMessageBox.Show("Result found on page " & res.Range.StartPosition.Page.PageNo)
    End If
End Sub

```

> note There are no genuine words in PDF, so the viewer uses a greedy text recognition method. That is why it is not guaranteed that it would find the exact start and end position of a word.

* **Events**:

  * **SelectionChanging**: Occurs when you start to select part of the text.

  * **SelectionChanged**: Occurs when you have finished changing the current selection.

## See Also

* [Getting Started](https://www.telerik.com/products/winforms/documentation/controls/pdfviewer/getting-started)
* [Logical Structure](https://www.telerik.com/products/winforms/documentation/controls/pdfviewer/structure/logical-structure)
* [Visual Structure](https://www.telerik.com/products/winforms/documentation/controls/pdfviewer/structure/visual-structure)
* [Properties, Methods and Events](https://www.telerik.com/products/winforms/documentation/controls/pdfviewer/properties-methods-events)
