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.

To accomplish this task, do the following.

The code sample below counts all words in a document.

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