Search text in PDF files using WPF PDF Viewer

10 Jul 202611 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.

Search Text using WPF PDF Viewer

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 following code example 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 following code example 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 by passing the Boolean parameter as ‘true’ to the SearchPreviousText(String, Boolean) method. The following code example illustrates how to search the previous occurrence of the text programmatically.

private void SearchPrevious_Click(object sender, RoutedEventArgs e)
{
    //Search and highlight the previous occurrence of the target text
    pdfViewer.SearchPreviousText("Target text");
}

Find text method

NOTE

From version 27.1.x, we have used the text extraction engine for finding text from PDF documents. By default, the 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 method takes the input argument as the given text. It provides a dictionary that contains the page index and the list of rectangular coordinates (bounds) of the text found on that page. The following code example illustrates how to get the bounds of the given text:

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 method takes the input arguments as the text to be found along with the desired page index and provides a list of values that contains the rectangular coordinates (bounds) of the found text. The following code example illustrates how to get the bounds of the given text on a particular page:

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 occurrence in the particular page
        RectangleF bounds = textSearch[0];
    }
}

Find and get the bounds of the list of text

The FindText method takes the input argument as the list of text that needs to be found. It provides a Dictionary mapping each page index to a list of TextSearchResult objects, where each entry contains the found text and its rectangular coordinates (bounds). The following code example illustrates how to get the bounds of the found text from the TextSearchResult:

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 method takes the input argument as the list of text to be found along with the desired page index and provides the list of MatchedItem, which contains the matched text and its rectangular coordinates (bounds). The following code example illustrates how to get the bounds of the text from the matched item:

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;
    }
}

Find and get the bounds of the list of text on multiple lines

The FindText method provides an overload with an enableMultilineSearch parameter that allows detection of matches spanning line breaks or multiple pages. When enabled, results are returned as line-based TextSearchResult entries grouped by page, including the matched text and corresponding RectangleF bounds. The default value of enableMultilineSearch is false. Use this overload when the text to find may wrap across lines or pages.

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 the text that spans across multiple lines and pages"};

    //Enable multiline search to detect line and page breaks and return line-wise results.
    //Return true, if the given text is found across lines/pages
    bool isMatchFound = pdfViewer.FindText(findText, out searchResult, true);
    if (isMatchFound)
    {
        foreach (var entry in searchResult)
        {
            int pageIndex = entry.Key;
            List<TextSearchResult> results = entry.Value;
            foreach (TextSearchResult result in results)
            {
                //The Bounds contains the line-wise rectangle for the matched text.
                RectangleF bounds = result.Bounds;
                //The Text property contains the matched text for this line.
                string matchedText = result.Text;
            }
        }
    }
}

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.