Selecting text in our UWP PDF viewer

Nutrient UWP SDK 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:

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’s 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:

private async void PDFView_InitializationCompletedHandler(PdfView pdfView, Document document)
{
    PDFView.Controller.OnTextSelectionChanged += (sender, args) =>
    {
        var selectedText = args != null ? args.Contents : "";
    };
}