Find and Highlight

  • Demo
  • C# source
  • VB.Net source
This demo shows you how to search text in a Word document and highlight the text matched.
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the demo you want, please contact support@e-iceblue.com for the free customized demo just for you.
using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DemoOnlineCode
{
    class FindAndHighlight
    {
        public void demoFindAndHighlight(String docFile, String searchText)
        {
            Document document = new Document(docFile, FileFormat.Auto);

            TextSelection[] textSelections
                = document.FindAllString(searchText, false, false);
            if (textSelections != null)
            {
                foreach (TextSelection selection in textSelections)
                {
                    TextRange textMatched = selection.GetAsOneRange();
                    textMatched.CharacterFormat.HighlightColor = Color.Yellow;
                }
            }

            document.SaveToFile("demo.doc", FileFormat.Doc);
        }
    }
}
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace DemoOnlineCode
    Class FindAndHighlight
        Public Sub demoFindAndHighlight(docFile As [String], searchText As [String])
            Dim document As New Document(docFile, FileFormat.Auto)

            Dim textSelections As TextSelection() = document.FindAllString(searchText, False, False)
            If textSelections IsNot Nothing Then
                For Each selection As TextSelection In textSelections
                    Dim textMatched As TextRange = selection.GetAsOneRange()
                    textMatched.CharacterFormat.HighlightColor = Color.Yellow
                Next
            End If

            document.SaveToFile("demo.doc", FileFormat.Doc)
        End Sub
    End Class
End Namespace