Selecting Text in Our UWP PDF Viewer

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.


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 : "";


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 : "";