Text Selection

PSPDFKit for Windows makes text selection accessible through two alternatives: a method for programmatically getting the current text selection, and an event to react to its changes.

Method

Text selection for the current document can be retrieved at any time using the Controller.GetTextSelectionAsync method, which returns a TextBlock representing the selected text. This TextBlock also contains positional information about the selection.

If no text is selected, the method returns null:

Copy
1
2
var selectedTextBlock = await PDFView.Controller.GetTextSelectionAsync();
var selectedText = selectedTextBlock != null ? selectedTextBlock.Contents : "";

Event

The Controller.OnTextSelectionChanged event presents a reactive alternative to the solution above. It is invoked whenever the user selection changes, and it results in a TextBlock if text is selected. When text is deselected, the event is also fired with null as its result:

Copy
1
2
3
4
5
6
7
private async void PDFView_InitializationCompletedHandler(PdfView pdfView, Document document)
{
    PDFView.Controller.OnTextSelectionChanged += (sender, args) =>
    {
        var selectedText = args != null ? args.Contents : "";
    };
}