With Silverlight-How to Insert Text Watermark in Word
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.
Watermark.xaml
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="232" d:DesignWidth="465" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
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.
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 (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.
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 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.
TextWatermark textWatermark = new TextWatermark();
textWatermark.Text = "Antarctic";
textWatermark.FontName = "Impact";
textWatermark.FontSize=90;
textWatermark.Color = Color.DarkOrange;
textWatermark.Layout=WatermarkLayout.Diagonal;
document.Watermark = textWatermark;
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.
bool? result = this.saveFiledialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = this.saveFiledialog.OpenFile())
{
document.SaveToStream(stream, FileFormat.Docx);
}
}
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
{
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("Antarctic.docx"))
{
using (Stream docStream = assembly.GetManifestResourceStream(name))
{
document = new Document(docStream, FileFormat.Docx);
}
}
}
TextWatermark textWatermark = new TextWatermark();
textWatermark.Text = "Antarctic";
textWatermark.FontName = "Impact";
textWatermark.FontSize=90;
textWatermark.Color = Color.DarkOrange;
textWatermark.Layout=WatermarkLayout.Diagonal;
document.Watermark = textWatermark;
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