Select and Copy Text in PDF files using WPF PDF Viewer

1 Feb 20221 minute to read

In PDF Viewer, text can be selected by clicking the mouse left button and dragging the mouse pointer over the text in any direction.

NOTE

From version 19.4.0.48, we have updated our default text extraction engine to PDFium for extracting text information from PDF documents. Based on the text information, we select text in the PDF documents. Please refer to the link for more details.

Detecting the completion of text selection

When the text selection is completed, the TextSelectionCompleted event will be raised. Refer to the following code snippet for wiring the event.

pdfViewer.TextSelectionCompleted += PdfViewer_TextSelectionCompleted;

The selected text can be retrieved as string from the TextSelectionCompletedEventArgs of the event handler.

private void PdfViewer_TextSelectionCompleted(object sender, TextSelectionCompletedEventArgs args) 
{
      //Get the whole selected text 
      string selectedText = args.SelectedText;
      //Get the selected text and its rectangular bounds for each page separately if the selection is made on multiple pages 
      Dictionary<int, Dictionary<string, Rectangle>> selectedTextInformation = args.SelectedTextInformation; 
}

Copying the selected text

The selected text can be copied by clicking the copy from the context menu, which appears when clicking the right mouse button after the text is selected.

WPF PDF Viewer Copying the Selected Text

The selected text can also be copied using the keyboard shortcut Ctrl + C.

NOTE

You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.