How to protect word document with watermark and password

Eric
Posted by in C# category on for Beginner level | Points: 250 | Views : 888 red flag
Rating: 4 out of 5  
 1 vote(s)

We can protect our word documents with password and also add the watermarks to them to keep the word documents confidential. With using Free Spire.Doc and only several lines of codes, we can fulfill the requirements easily.

Introduction

My recent project needs to avoid our word documents to be used by other readers. Then we choose to protect the word documents with password and also add the watermarks to them to keep the word documents confidential. We intend to finish the job by ourselves and it is so complex and it may take us lots of time to fulfill it. Finally we searched out Free Spire.Doc and with only several lines of codes and we can fulfill our requirements easily.  This project will show you clearly how to protect the word documents with password and add text watermark and picture watermark to the word document.

Main codes:

Spire.Doc offers a method of Document.Encrypt() to enable developers to encrypt the word documents with password. We can easily call Spire.Doc to use the class of Spire.Doc.PictureWatermark and Spire.Doc.TextWatermark to add the picture watermark and text watermark to protect the word documents to be used by others.

How to get it?

This free word component is listed on the Nuget, you can get the Free Spire.Doc.dll directly from there and add it as the reference for your project. Firstly, review the original documents:

Code Snippets of how to protect the document and add the watermarks to it.

Step 1: Create a new word document and load the document from file.

Document document = new Document();

document.LoadFromFile("sample.docx");

Step 2: Add the text watermark to the word document

TextWatermark txtWatermark = new TextWatermark();

txtWatermark.Text = "Confidential";

txtWatermark.FontSize = 90;

txtWatermark.Layout = WatermarkLayout.Diagonal;

document.Watermark = txtWatermark;

Step 3: Encrypt the word document with password

document.Encrypt("12345");

Step 4: Save the document to file:

document.SaveToFile("Result.docx", FileFormat.Docx);

Effective screenshot of the word document with password and text watermark:



After input the password, we will get this word document with text watermark:




The code snippet of how to add the picture watermark:

PictureWatermark picture = new PictureWatermark();

picture.Picture = System.Drawing.Image.FromFile("logo.png");

picture.Scaling = 200;

document.Watermark = picture;


Effective result of the picture watermark:

Then our word document has been protected by both password and the watermark. We are pretty happy with Free Spire.Doc, the API was designed very well. It is intuitive to use and it easily does exactly what we need. 

PS: Word document only accept one kind of watermark once a time, either text watermark or picture watermark. We only use one kind of watermark for one same word document.

Full codes:

static void Main(string[] args)

{

    Document document = new Document();

    document.LoadFromFile("sample.docx");

 

    PictureWatermark picture = new PictureWatermark();

    picture.Picture = System.Drawing.Image.FromFile("logo.png");

    picture.Scaling = 300;

    document.Watermark = picture;

 

    //TextWatermark txtWatermark = new TextWatermark();

    //txtWatermark.Text = "Confidential";

    //txtWatermark.FontSize = 90;

    //txtWatermark.Layout = WatermarkLayout.Diagonal;

    //document.Watermark = txtWatermark;

 

    document.Encrypt("12345");

 

    document.SaveToFile("Result.docx", FileFormat.Docx);

 

}


Page copy protected against web site content infringement by Copyscape

About the Author

Eric
Full Name: Eric Smith
Member Level: Starter
Member Status: Member
Member Since: 3/22/2016 4:24:11 AM
Country: Tunisia


I have a Master Degree in Computer Science and currently working as a developer. Have more than 2 years experience. I have worked with some .NET framework and I'm still learning. My interest are in asp.net, c#, Silverlight,wpf and wwf.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)