Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
25 juin 2012

With Silverlight-How to Find and Highlight Text in Word Document

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, please download and install it at first and then add its dll file as reference in your project.

STEPS

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.

Find.xaml

<UserControlx:Class="FindandHighlight.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="184"d:DesignWidth="439"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

 

    <Gridx:Name="LayoutRoot"Background="#FFFFB9EF"Height="179"Width="436">

        <sdk:LabelHeight="100"HorizontalAlignment="Center"Margin="0,-2,0,81"Name="label1"VerticalAlignment="Center"Width="436"Content="Find and Highlight Specified Text in Word "FontSize="22"FontFamily="Arial"Foreground="DarkCyan"Background="AliceBlue" />

        <ButtonContent="RUN"Height="37"HorizontalAlignment="Left"Margin="379,142,0,0"Name="button1"VerticalAlignment="Top"Width="58"Click="button1_Click" />

    </Grid>

</UserControl>

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.

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

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

        }

 

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.

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

                    }

                }

            }

 

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.

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

            foreach (TextSelection selection in textSelections)

            {

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

            }

 

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.

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

            if (result.HasValue && result.Value)

            {

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

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

 

Full WordTable.xaml.cs

usingSystem;

usingSystem.Windows;

usingSystem.Windows.Controls;

usingSystem.Reflection;

usingSystem.IO;

usingSystem.Drawing;

usingSpire.Doc;

usingSpire.Doc.Documents;

 

namespaceFindandHighlight

{

    public partial class MainPage : UserControl

    {

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

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

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

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

                    }

                }

            }

 

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

            foreach (TextSelection selection in textSelections)

            {

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

            }

 

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

            if (result.HasValue && result.Value)

            {

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

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }    

        }

    }

}

RESULT

__________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité