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
this.radPdfViewer1.ViewerMode = Telerik.WinControls.UI.FixedDocumentViewerMode.TextSelection;
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
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
Telerik.Windows.Documents.Fixed.Text.TextPosition start = new Telerik.Windows.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document.CaretPosition);
start.MoveToCurrentWordStart();
Telerik.Windows.Documents.Fixed.Text.TextPosition end = new Telerik.Windows.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document.CaretPosition);
end.MoveToCurrentWordEnd();
this.radPdfViewer1.Document.Selection.SetSelection(start, end);
RadMessageBox.Show(this.radPdfViewer1.Document.Selection.GetSelectedText());
Dim start As New Telerik.Windows.Documents.Fixed.Text.TextPosition(Me.RadPdfViewer1.Document.CaretPosition)
start.MoveToCurrentWordStart()
Dim [end] As New Telerik.Windows.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
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.Windows.Documents.Fixed.Text.TextPosition startPosition = new Telerik.Windows.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document);
Telerik.Windows.Documents.Fixed.Text.TextPosition endPosition = new Telerik.Windows.Documents.Fixed.Text.TextPosition(this.radPdfViewer1.Document);
startPosition.MoveToLineStart();
endPosition.MoveToLineEnd();
//Select the first line in the document
this.radPdfViewer1.PdfViewerElement.Select(startPosition, endPosition);
}
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.Windows.Documents.Fixed.Text.TextPosition(Me.radPdfViewer1.Document)
Dim endPosition As New Telerik.Windows.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
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)
{
this.radPdfViewer1.PdfViewerElement.GetSelectedTextAsync(delegate(string text) { MessageBox.Show("Selected text: " + text); });
}
private void buttonCopy_Click(object sender, EventArgs e)
{
this.radPdfViewer1.PdfViewerElement.Copy();
}
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
Me.radPdfViewer1.PdfViewerElement.GetSelectedTextAsync(Function(text As String)
MessageBox.Show("Selected text: " & text)
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
private void buttonFindNext_Click(object sender, EventArgs e)
{
Telerik.Windows.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.Windows.Documents.Fixed.Search.SearchResult.NotFound)
{
RadMessageBox.Show("String not found");
}
else
{
RadMessageBox.Show("Result found on page " + res.Range.StartPosition.Page.PageNo);
}
}
Private Sub buttonFindNext_Click(sender As Object, e As EventArgs) Handles buttonFindNext.Click
Dim res As Telerik.Windows.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.Windows.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
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.