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;
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;
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
To get the selected text, you can use thethis.pdfViewer.Document.Selection.Clear();
GetSelectedText
method ofTextSelection
.
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)
{
}