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 Mode property, which could be set as show in Example 1.

Example 1: Setting mode to RadPdfViewer

this.pdfViewer.Mode = Telerik.Windows.Documents.Fixed.UI.FixedDocumentViewerMode.TextSelection; 

More information on the different modes of the viewer can be found in this article.

Programmatic Selection

When it comes to programmatic selection, it can occur even if the Mode of RadPdfViewer is not set to Selection.

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. More information on TextPositions can be found here.
  • 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)

Example 2 demonstrates how the functionality of TextPosition can be combined with that of TextSelection in order to select the word that the caret is currently positioned in.

Example 2: Using TextPosition and TextSelection classes

TextPosition start = new TextPosition(this.pdfViewer.Document.CaretPosition); 
start.MoveToCurrentWordStart(); 
TextPosition end = new TextPosition(this.pdfViewer.Document.CaretPosition); 
end.MoveToCurrentWordEnd(); 
this.pdfViewer.Document.Selection.SetSelection(start, end); 
MessageBox.Show(this.pdfViewer.Document.Selection.GetSelectedText()); 

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.

Example 3: Attaching to SelectionChanged

 this.pdfViewer.Document.Selection.SelectionChanged += Selection_SelectionChanged; 

See Also

In this article