Back to Devexpress

How to: Search Text in a Document with DevExpress PDF Document API

officefileapi-16502-pdf-document-api-examples-extract-content-from-a-pdf-document-how-to-search-text-in-a-document.md

latest2.6 KB
Original Source

How to: Search Text in a Document with DevExpress PDF Document API

  • Sep 22, 2025
  • 2 minutes to read

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

This example describes how to count the occurrences of words in a document text.

Call the PdfDocumentProcessor.FindText method with a string parameter containing the text to search in the document.

After the method returns the results of the search operation, repeat the call until each occurrence of the specified text is found in the document.

Alternatively, use an overloaded PdfDocumentProcessor.FindText method to specify additional search parameters, such as case sensitivity and search direction.

To navigate through the text, use the PdfDocumentProcessor.PrevWord and PdfDocumentProcessor.NextWord methods. These methods return a PdfWord object that contains a word located at the current search position.

The following code snippet counts all words in a document.

View Example

csharp
using DevExpress.Pdf;
// ...

private int WordCount(string filePath, string word) {
    int count = 0;
    try {
        using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
            documentProcessor.LoadDocument(filePath);
            while (documentProcessor.FindText(word).Status == PdfTextSearchStatus.Found) {
                count++;
            }
        }
    }
    catch { }

    return count;
}
vb
Imports DevExpress.Pdf
' ...
Private Function WordCount(ByVal filePath As String, ByVal word As String) As Integer
    Dim count As Integer = 0
    Try
        Using documentProcessor As New PdfDocumentProcessor()
            documentProcessor.LoadDocument(filePath)
            Do While documentProcessor.FindText(word).Status = PdfTextSearchStatus.Found
                count += 1
            Loop
        End Using
    Catch
    End Try

    Return count
End Function