Search text in PDF files using WPF PDF Viewer
28 Aug 20248 minutes to read
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 perform text search in PDF documents. Please refer to the link for more details.
The WPF PDF Viewer allows you to search and highlight the text in the PDF files. The search box appears when Ctrl+F is pressed and searches the text in the PDF document as displayed in the following screenshot.
NOTE
PdfDocumentView is used to view the PDF documents without the toolbar. So, make use of PdfViewerControl to search the text using search box.
Search text in PDF programmatically
The WPF PDF Viewer also allows a user to search for text in the PDF document programmatically using the following methods
- SearchText method
- FindText method
Search Text method
The SearchText method allows the user to search and highlight a specific text in the PDF document after loading the document in the PdfViewerControl.
Search the initial occurrence of the text
The SearchText(String) method finds the occurrences of the target text and highlights the first occurrence of the search in the UI. A user can also search the text with case sensitivity by passing the Boolean parameter as ‘true’ to the SearchText(String, Boolean) method. The below code illustrates how to search a text in a PDF programmatically.
//Handle the DocumentLoaded event of PdfViewerControl
private void PdfViewer_DocumentLoaded(object sender, System.EventArgs args)
{
//Search the text in the PDF and highlight the first occurrence in UI
pdfViewer.SearchText("Target Text");
}
Search the next occurrence of the text
The SearchNextText(String) method highlights the next occurrence of the highlighted search in the UI. If there is no highlighted search, it will highlight the first occurrence of the search. A user can also search the text with case sensitivity by passing the Boolean parameter as ‘true’ to the SearchNextText(String, Boolean) method. The below code snippet illustrates how to search the next occurrence of the text programmatically.
private void SearchNext_Click(object sender, RoutedEventArgs e)
{
//Search and highlight the next occurrence of the target text
pdfViewer.SearchNextText("Target text");
}
Search the previous occurrence of the text
The SearchPreviousText(String) method highlights the previous occurrence of the highlighted search in the UI. The user can also search the text with case sensitivity bypassing the Boolean parameter as ‘true’ to the SearchPreviousText(String, Boolean) method. The below code snippet illustrates how to search the previous occurrence of the text programmatically.
private void SearchPrevious_Click(object sender, RoutedEventArgs e)
{
//Search and highlight the previuos occurrence of the target text
pdfViewer.SearchPreviousText("Target text");
}
Find text method
NOTE
From version 27.1.x, we have used text extraction engine for find text from PDF documents. By default text extraction engine uses PDFium for extracting text information. Please refer to the link for more details.
The FindText method allows the user to search a particular text and get its bounds after loading the document in the PdfViewerControl. The FindText method returns ‘true’ when the given text is found in the document; else, it returns ‘false’.
Find and get the bounds of a text
The [FindText(String, out Dictionary<Int32, List
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
//Get the occurrences of the target text and location.
Dictionary<int, List<RectangleF>> textSearch = new Dictionary<int, List<RectangleF>>();
//Return true, if the given text is found
bool isMatchFound = pdfViewer.FindText("FindText", out textSearch);
if (isMatchFound)
{
//Get the list of bounds in the first page
List<RectangleF> bounds = textSearch[0];
}
}
Find and get the bounds of a text on the desired page
The [FindText(String, Int32, out List
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
//Get the occurrences of the target text and location.
List<RectangleF> textSearch = new List<RectangleF>();
//Return true, if the given text is found in the particular page
bool isMatchFound = pdfViewer.FindText("FindText",0, out textSearch);
if (isMatchFound)
{
//Get the bounds of the first occurance in the particluar page
RectangleF bounds = textSearch[0];
}
}
Find and get the bounds of the list of text
The [FindText(List
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
//Get the occurrences of the target text and location.
Dictionary<int, List<TextSearchResult>> searchResult = new Dictionary<int, List<TextSearchResult>>();
//List of text need to be found
List<string> findText = new List<string>() {"Find","Text"};;
//Return true, if the given text is found in the particular page
bool isMatchFound = pdfViewer.FindText(findText, out searchResult);
if (isMatchFound)
{
//Get the bounds of the first TextSearchResult
RectangleF bounds = searchResult[0][0].Bounds;
}
}
Find and get the bounds of the list of text on the desired page
The [FindText(List
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
//Get the occurrences of the target text and location.
List<MatchedItem> searchResult = new List<MatchedItem>();
//List of text need to be found
List<string> findText = new List<string>() {"Find","Text"};
//Return true, if the given text is found in the particular page
bool isMatchFound = pdfViewer.FindText(findText,0, out searchResult);
if (isMatchFound)
{
//Get the bounds of the first MatchedItem on the desired page
RectangleF bounds = searchResult[0].Bounds;
}
}
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.