How to Insert Text Watermark in Word with C#/VB.NET

Word Watermark is text or image with translucence mode. It can be put with different direction, for example, diagonal or horizontal. Although we insert watermark in Word, the contents will not be influenced at all. Its main function is to protect author’s copyright.

There are two kinds of watermark, text watermark and image watermark. Text watermark often presents document properties. For example, text which is taken as watermark may be company name, document title etc. Image watermark will be picture which is related to document contents. Also, we can use any pictures to take as watermark. What’s more, some beautiful image watermark can make appearance to be more appealed.

In this post, I will introduce a method to insert watermark in Word with C#/VB.NET.

A .NET Word Component, Spire.Doc for .NET is used to help me realize this function more quickly.

Detailed Steps Shown as Following:           

  1. Load document which I want to insert watermark from computer.
  2. Insert text watermark.  Firstly, declare a new txtWatermark and assign value for txtWatermark.Text. Secondly, format watermark, including color, font size and layout. There are two kinds of layout, diagonal and horizontal. Thirdly, insert watermark in document.
  3. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertWatermark
{
    class watermark
    {
        static void Main(string[] args)
        {

            //Load File
            Document document = new Document();
            document.LoadFromFile(@”D:\work\documents\Antarctic.docx”);

            //Insert TextWatermark
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = “Wiki Article”;
            txtWatermark.Color = Color.DeepSkyBlue;
            txtWatermark.FontSize = 45;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            document.Watermark = txtWatermark;

            //Save and Launch
            document.SaveToFile(“Watermark.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Watermark.docx”);
        }
    }
}

VB.NET:

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace InsertWatermark
    Friend Class watermark
        Shared Sub Main(ByVal args() As String)

            ‘Load File
            Dim document As New Document()
            document.LoadFromFile(“D:\work\documents\Antarctic.docx”)

            ‘Insert TextWatermark
            Dim txtWatermark As New TextWatermark()
            txtWatermark.Text = “Wiki Article”
            txtWatermark.Color = Color.DeepSkyBlue
            txtWatermark.FontSize = 45
            txtWatermark.Layout = WatermarkLayout.Diagonal
            document.Watermark = txtWatermark

            ‘Save and Launch
            document.SaveToFile(“Watermark.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Watermark.docx”)

        End Sub
    End Class
End Namespace

For image watermark, we can use the following sentence:

            PictureWatermark imagewatermark = new PictureWatermark();
            Image image = Image.FromFile(@”D:\work\Documents\coffee.jpg”);
            imagewatermark.Picture = image;
            document.Watermark = imagewatermark;

Result Shown as Following:

Download Spire.Doc for .NET Here

About these ads

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