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
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
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
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
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
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
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.