How to Find and Highlight Text in Word with Silverlight

Actually, it is a little difficult to find specified words or sentences from lots of contents. Therefore, Microsoft Word offers Find function to users. This function can help users find text quickly and save users time. The text which users want to find will be highlighted in order to confirm location.

In this post, I will introduce a method about how to find and highlight specified text in a Word document with Silverlight.

Note: a component, Spire.Doc for Silverlight is used in this example. If you want to use the following code, freely DOWNLOAD and install it at first and then add its dll file as reference in your project.


Step 1. Design UserControl

Rename MainPage.xaml as Find.xaml and then double click it to design UserControl. Change background color and then add a label in UserControl. Change label contents which are about what I will do next. Set label background color and font style. Finally, add a button to run.

Step 2. Declare SaveFileDialog

Declare a SaveFileDialog to save document and set a filter for this SaveFileDialogue. This filter is used to choose Word format, for example, .doc or .docx. In this example, I set filter format as .docx only.

Step 3. Load Document

Right click project name and add existed item (Word document). After adding, click its name and change its Build Action as Embedded Resource.

Double click Run button to write code. Declare a document at the beginning and then load this document from assembly. Use foreach sentence to get document name. If the document is the same with embedded resource name, load this document.

Step 4. Find Text

Use document.FindAllString() method to find document. Three parameters are passed to this method, text string, a bool value to define if casing sensitive, a bool value to define if highlighting whole word. Then save this string in a TextSelection array. Use foreach sentence to get each word in TextSelection and highlight.

Step 5. Save Document

Judge if the saveFileDialog which I declare at step 1 can pop up. If yes, save the encrypted document through it.

Full Find.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

using System.IO;

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;


namespace FindandHighlight


    public partial class MainPage : UserControl


        //Declare SaveFileDialog

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()



            this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;



        private void button1_Click(object sender, RoutedEventArgs e)


            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())


                if (name.EndsWith(“Blues Introduction.docx”))


                    using (Stream docStream = assembly.GetManifestResourceStream(name))


                        document = new Document(docStream, FileFormat.Docx);





            //Find Text

            TextSelection[] textSelections = document.FindAllString(“Blues”, true, true);


            //Highlight Text

            foreach (TextSelection selection in textSelections)


                selection.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;



            //Save Document

            bool? result = this.saveFiledialog.ShowDialog();

            if (result.HasValue && result.Value)


                using (Stream stream = this.saveFiledialog.OpenFile())


                    document.SaveToStream(stream, FileFormat.Docx);








Click Here to LEARN MORE about Spire.Doc for Silverlight

Spire.Office also can be used to realize this function


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s