New to Telerik UI for WinUI? Download free 30-day trial

Text Selection

PdfViewer supports text selection through the UI and also programmatically.

To select text from the UI, the Mode property of RadPdfViewer should be set to TextSelection. Read more about the available modes in the Viewer Modes article.

TextSelection is part of the RadPdfProcessing library which is used for the document model of the PdfViewer.

Setting the mode of RadPdfViewer

this.pdfViewer.Mode = FixedDocumentViewerMode.TextSelection; 
The programmatic selection is part of the RadFixedDocument API (the Document property value of RadPdfViewer). To access it, use its Selection property of the document. It gives access to a TextSelection object that provides a set of properties and methods that can be used to select text.

The TextSelection class introduces the StartPosition and EndPosition properties that can be used to access the selection range. Read more about the position objects in the Text Position article.

Accessing the current selection positions

TextPosition selectionStart = this.pdfViewer.Document.Selection.StartPosition; 
TextPosition selectionEnd = this.pdfViewer.Document.Selection.EndPosition; 
The set the selection, use one of the methods of TextSelection.

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

    Setting selection using SetSelectionStart and Set SelectionEnd methods

        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

    Setting selection using SetSelection method

        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

    Selecting all text

        this.pdfViewer.Document.Selection.SelectAll(); 
    
  • Clear

    Clearing the text selection

        this.pdfViewer.Document.Selection.Clear(); 
    
    To get the selected text, you can use the GetSelectedText method of TextSelection.

Getting the selected text

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

Events

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

Subscribe to selection changing events

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

In this article
Not finding the help you need?