How to Add Text Watermark in Word with Silverlight

Because information is spread very quickly online, some people find that their documents are modified, even stolen by others. In order to protect copyright of document, people try to add watermark in document. In this post, I want to introduce the method to add watermark in Word with Silverlight.

Watermark can be text or image. Generally speaking, text often shows document properties, for example important, secret, while image can make document appearance more wonderful. In this example, I will add text watermark and watermark content is the title of document.

In order to realize this function more easily and quickly, I use a component, Spire.Doc for Silverlight in this example. If you want to use the following code, please DOWNLOAD and install it. Then, add its dll file as reference in your project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as Watermark.xaml. Double click it to design UserControl. At first, add a label and change label content. Set format for content, including font style, size and color. Then, change UserControl background color. Finally, add a button to run.

Step 2. Declare SaveFileDialog

Declare a new SaveFileDialog for saving Word document. Then, set filter for this SaveFileDialog. This filter is used to choose Word format (.doc or .docx). I set the format as .docx only for this filter.

Step 3. Load Document

Right click project to add existed item (Word document which I want to add watermark) and set this item’s Build Action as Embedded Resource. After that, double click run button and write code. Declare document and assembly. Use foreach sentence get name string from assembly. If the name is the same as embedded resource name, load it.

Step 4. Add Text Watermark

Declare a TextWatermark and set text, which is content, of TextWatermark as document title. Then, set format for text, including font name, size, color, watermark layout. Finally, assign this watermark as value of document’s watermark.

Step 5. Save Document

Judge if the SaveFileDialog which is declared in the first step can pop up. If so, save the document which has been added text watermark by using  SaveFileDialog.

Full Watermark.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 WordWatermark

{

    public partial class MainPage : UserControl

    {

        //Declare SaveFileDialog

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            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(“Antarctic.docx”))

                {

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

                    {

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

                    }

                }

            }

 

            //Add Text Watermark

            TextWatermark textWatermark = new TextWatermark();

            textWatermark.Text = “Antarctic”;

            textWatermark.FontName = “Impact”;

            textWatermark.FontSize=90;

            textWatermark.Color = Color.DarkOrange;

            textWatermark.Layout=WatermarkLayout.Diagonal;

            document.Watermark = textWatermark;

 

            //Save Document

            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

How to Replace Text in Word Document with Silverlight

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.

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.

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.

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.

Step 5. Save Document

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

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

    {

        //Declare SaveFileDialog

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

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

                {

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

                    {

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

                    }

                }

            }

 

            //Replace Text

            document.Replace(“New Zealand”, “NZ”, true, true);

 

            //Save Document

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

            if (result.HasValue && result.Value)

            {

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

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

RESULT

Original Document

Result Document

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.

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.

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

        {

            InitializeComponent();

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

                }

            }    

        }

    }

}


RESULT

_____________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

How to Set Page in Word Document with Silverlight

Before printing one Word document, users need to set page for having a better layout. The process to set page is called page setup in Word. Page setup includes margins, page orientation, size etc. Generally speaking, users pay more attention on margin settings because contents may not printed completely if margins are not well set.

In this post, I will share my method about how to set Word page with Silverlight. I have prepared a Word document. Then, set its margins (top, bottom, right and left), page size and page orientation. The component, Spire.Doc for Silverlight is used in this example, so I have added its dll file in project at the beginning.

STEPS

Step 1. Design UserControl

Double click MainPage.xaml to design UserControl. Firstly, set background color for UserControl. Secondly, add a label. Change its content and then set label background and content text format. Thirdly, add a button to RUN and set format.

Step 2. Declare SaveFileDialog

Declare a saveFileDialog. Then, set filter for this saveFileDialog, which is the Word format (.doc or .docx) provided with others to choose. In this example, I set the filter format as .docx only.

        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            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 add comment. After adding, click this document to change its Build Action as Embedded Resource.

Double Click RUN button to write code. Declare a new document at the beginning. Next, use foreach sentence to get document name from assembly. If the name is the same with name of embedded resource, load the 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. Page Setup

Get the first section in Word document. Then, assign value for section.PageSetup.PageSize/Orientation/Margins. Page size is set as A4 and Orientation is set as LandScapge.

            Section section = document.Sections[0];
            section.PageSetup.PageSize = PageSize.A4;
            section.PageSetup.Orientation = PageOrientation.Landscape;
            section.PageSetup.Margins.Top = 55f;
            section.PageSetup.Margins.Bottom = 55f;
            section.PageSetup.Margins.Left = 70f;
            section.PageSetup.Margins.Right = 70f;

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

using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;

namespace SilverlightPageSetup
{
    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);
                    }
                }
            }

            Section section = document.Sections[0];
            section.PageSetup.PageSize = PageSize.A4;
            section.PageSetup.Orientation = PageOrientation.Landscape;
            section.PageSetup.Margins.Top = 55f;
            section.PageSetup.Margins.Bottom = 55f;
            section.PageSetup.Margins.Left = 70f;
            section.PageSetup.Margins.Right = 70f;

            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

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

How to Add Comment in Word Document with Silverlight

Comments can be taken as reviews or additional information for an article. On one hand, comments are used to present readers’ thoughts about article and give suggestions to readers. On the other hand, comments show some information which is not the main content in article but very important. Microsoft Word offers one function to users to insert comment for some special sentences or text. And in this post, I will introduce the method about how to add comment in Word with Silverlight.

I prepare a Word document which is about Antarctic and I will insert a comment for the first paragraph. This comment presents the original source of contents in this document.

I use a component, Spire.Doc for Silverlight in this example. If you want to use the following code, please make sure that you have installed Silverlight 4 and Spire.Doc for Silverlight. What’s more, Spire.Doc for Silverlight dll file must be added as reference in project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as InsertComment.xaml. Double click it to design UserControl. I set its background as an image at the beginning. Then, I add a label to and change the contents as what I will do next and set format for label contents. Finally, add a button to run.

Step 2. Declare SaveFileDialog

Declare a saveFileDialog for saving document. Also, set filter for this saveFileDialog, which is used to choose format. And I set the format as .docx.

        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            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 add comment. After adding, click this document to change its Build Action as Embedded Resource.

Double Click RUN button to write code. Declare a new document at the beginning. Next, use foreach sentence to get document name from assembly. If the name is the same with name of embedded resource, load the document.

            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“Antarctic.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

Step 4. Add Comment

Get section in document and get paragraph which I want to add comment in this section. Then, declare a string which is the comment contents, and use paragraph.AppendComment() method to add comment. The parameter passed to this method is string. Then, set comment author and initial.

            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];

            string str = “All the contents in this document is from Wikipedia. If you want to learn more information about Antarctic, please visit http://en.wikipedia.org/wiki/Antarctic.”;
            Comment comment = paragraph.AppendComment(str);
            comment.Format.Author = “Wikipedia”;
            comment.Format.Initial = “C”;

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

using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordComment
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFileDialog = null;
        public MainPage()
        {
            InitializeComponent();
            this.saveFileDialog = new SaveFileDialog();
            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(“Antarctic.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];

            string str = “All the contents in this document is from Wikipedia. If you want to learn more information about Antarctic, please visit http://en.wikipedia.org/wiki/Antarctic.”;
            Comment comment = paragraph.AppendComment(str);
            comment.Format.Author = “Wikipedia”;
            comment.Format.Initial = “C”;

            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

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function