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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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