Watermarks are text, logo, or other graphical pattern that is inserted in a document or superimposed on another image. Its purpose is to keep the identity of the original document or image so as to forbid its use without permission. As a .NET application developer, you can provide the functionality of inserting a Watermark in Word documents in your document processing C# application.
In this blog, we’ll show how to insert a watermark in a Word document in C# using the free and open-source .NET API.
Insert Watermark in Word Document using Microsoft Word
Before we can proceed to have a look at how a watermark can be inserted in a document using C#, let’s have a look at how the same can be done using Microsoft Word. Microsoft Word lets you insert text or image watermarks in your document to protect the identity of your document. You can also insert a customized watermark in your document. Watermarks appear in the background on the page behind the text.
Steps to Insert Watermark in a Word Document using Microsoft Word
You can use the following steps to create a Watermark in a Word document using Microsoft Word.
- On the Design tab, select Watermark.
- From the available options, select Text or Image as the type of watermark to be inserted in the document
- Select OK button
How to Insert Watermark in Word Document using C#?
Now that we have seen how to insert a watermark in a Word document using Microsoft Word, we can proceed towards doing the same using NPOI API for .NET in our C# application. If you haven’t already installed NPOI API, you can go through our comprehensive guide for NPOI API installation in your .NET project.
Step-by-step Guide to Inserting Watermark in Word Document using C#
At this stage, your .NET project should be ready to write code for inserting a watermark in a Word document using NPOI API in C#. The following steps can be used for this purpose.
- Create an instance of XWPFDocument class in your C# project
- Add an XWPFParagraph and XWPFRun object
- Set the watermark text
- Save the document using the XWPFDocument instance you created in Step 1
using (XWPFDocument doc = new XWPFDocument())
XWPFParagraph paragraph = doc.CreateParagraph();
XWPFRun run = paragraph.CreateRun();
var hfPolicy = doc.CreateHeaderFooterPolicy();
hfPolicy.CreateWatermark("D R A F T");
using (FileStream fs = new FileStream("watermark.docx", FileMode.Create))
In this article, we showed how to use NPOI API in C# programming language to insert a watermark in a Word document. NPOI API is an open-source API and free to use for working with Microsoft Word documents. You can have a look at other NPOI examples for working with document files in C#.