Skip to main content

How to: Search Text in a Document

  • 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 demonstrates 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 code sample below counts all words in a document.

View Example

using DevExpress.Pdf;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.IO;
using System.Text;
using System.Windows.Forms;
// ...
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;
}