Searching Text in WPF Pdf Viewer

29 Jul 20212 minutes to read

Essential PDF Viewer allows you to search and highlight the text in the PDF document. The search box appears when Ctrl+F is pressed and searches the text in the PDF document as displayed in the following screenshot.

WPF PDF Viewer Concept and Features

NOTE

  • PdfDocumentView is used to view the PDF documents without the toolbar. So, make use of PdfViewerControl to search the text using search box.

The PDF Viewer control also supports searching text in the PDF document using the following API. The FindText method returns true when the text given is found in the document. The dictionary contains the page index and the list of rectangular coordinates of the text found in that page. The following code example explains how text search can be achieved after creating the control from the code and handled in the Loaded event of the application’s MainWindow.

private void Window_Loaded(object sender, RoutedEventArgs e)
{

	bool IsMatchFound;

	//Load the PDF.

	pdfViewerControl1.Load("../../Data/Barcode.pdf");

	//Get the occurrences of the target text and location.

	Dictionary<int, List<RectangleF>> 

          textSearch = new Dictionary<int, List<RectangleF>>();

	IsMatchFound = pdfViewerControl1.FindText("targetText", out textSearch);

}
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

	Dim IsMatchFound As Boolean

	'Load the PDF.

	pdfViewerControl1.Load("../../Data/Barcode.pdf")

	'Get the occurrences of the target text and location.

	Dim textSearch As New Dictionary(Of Integer, List(Of RectangleF))()

	IsMatchFound = pdfViewerControl1.FindText("targetText", textSearch)

End Sub

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.