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

With Silverlight-How to Replace Text in Word Document

It is possible that we may make some mistakes when writing something in Word, such as misspelling, catachresis and so on. We can correct mistakes directly, or use “Replace” function.

Replace function in Word is used to replace text or sentence with another one. Because of this function, it is very convenient for users to modify contents to make words or sentences to be used more correctly and appropriately. And it is very useful when we need to replace text which appears many times in document.

In this post, I want to introduce a method to replace text in Word with Silverlight. I prepare a document which is talking about New Zealand. I will change all “New Zealand” in this document as “NZ”.

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

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as Replace.xaml. Double click it to design UserControl. Firstly, add a label in UserControl and change content. Set format for content, including font style and size. Secondly, add a button to run. Change button background color and content font style. Thirdly, set background color for UserControl.

Replace.xaml

<UserControl x:Class="Replace.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="222" d:DesignWidth="487" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

 

    <Grid x:Name="LayoutRoot" Height="219" Width="486">

        <sdk:Label Height="31" HorizontalAlignment="Center" Margin="-4,58,0,116" Name="label1" VerticalAlignment="Center" Width="462" Content="Replace Text in Word with Silverlight" FontFamily="Times New Roman" FontSize="28" Foreground="AliceBlue" FontWeight="Bold" />

        <Button Content="RUN" Height="39" HorizontalAlignment="Center" Margin="239,154,187,26" Name="button1" VerticalAlignment="Center" Width="61" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="YellowGreen" Background="AliceBlue" Click="button1_Click" />

        <Grid.Background>

            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

                <GradientStop Color="Black" Offset="0" />

                <GradientStop Color="Cyan" Offset="1" />

                <GradientStop Color="#FF006262" Offset="0" />

            </LinearGradientBrush>

        </Grid.Background>

    </Grid>

</UserControl>

ReplaceBG

Step 2. Declare SaveFileDialog

Declare a SaveFileDialog for saving document. Then, set filter for this SaveFileDialog. Filter is used for people to choose format the document is, for example, .doc or .docx. In this example, I set the 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 to add existed item which is the document which I want to replace text. After adding, click document and change its Build Action as Embedded Resource.

Double click run button to write code. Declare document and assembly. Use foreach sentence to get name string from assembly. If the name string is the same with document name, load this document.

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith("New Zealand.docx"))

                {

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

                    {

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

                    }

                }

            }

Step 4. Replace Text

Use document.Replace method to replace text. There are four parameters passed to this method, original string the document has, new string to replace original string, bool value to judge if casing sensitive, bool value to judge if replacing whole world.

            document.Replace("New Zealand", "NZ", true, true);

Step 5. Save Document

Save document through SaveFileDialog which I declare in the first step if it can pop up.

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

            if (result.HasValue && result.Value)

            {

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

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

Full Replace.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

using System.IO;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace Replace

{

    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("New Zealand.docx"))

                {

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

                    {

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

                    }

                }

            }

 

            document.Replace("New Zealand", "NZ", true, true);

 

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

            if (result.HasValue && result.Value)

            {

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

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

RESULT

Original Document

New zealand

Result Document

Nz

____________________________________________________________________

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é