How C # Adds a watermark to Word documents _c# Tutorial

Source: Internet
Author: User

Like PDFs, in Word, watermarks are divided into image watermarks and text watermarks, and adding a picture watermark to a document can make the document more beautiful and appealing. A text watermark protects the document by reminding others that the document is copyrighted and cannot be copied at will. Before I shared how to add a watermark to the PDF, someone asked me how to add a watermark to the Word document, and today is free, just record how I did it.

The steps are actually very simple, in order to save time and simplify the size of the code, I downloaded a free word component from the E-iceblue official web site, after the installation, refer to the official website of the tutorial, create the project, and follow the steps of the code for simple settings. Let's take a look at how you can implement these functions through this component.

Screenshot of original document:

The first part: Add a picture watermark

First step: Add a reference.

After the component is installed, create a C # console project, add the DLL file under the installation directory to the project as a reference, and add the namespace as follows:

Using Spire.doc;
Using Spire.Doc.Documents;

Step Two: Create a new Word Document object and load the Word document to be added to the watermark;

Document doc = new document ();
Doc. LoadFromFile ("Introduction to XML file. doc");

Step three: Create a new Image Watermark object and add a picture to be set as a watermark;

Picturewatermark picture = new Picturewatermark ();
Picture. Picture = System.Drawing.Image.FromFile ("Flower _2.jpg");

Step Fourth: Set the size of the picture as needed, and then set it as a watermark for the document;

Picture. scaling = 80;
Doc. watermark = picture;

Part II: Add a text watermark

Fifth step: Create a new text watermark object, and add the text to be set as a watermark;

Textwatermark Txtwatermark = new Textwatermark ();
Txtwatermark.text = "Microsoft";

Sixth step: Set the text font size and text arrangement, I set the arrangement is diagonally arranged;

Txtwatermark.fontsize = 90;
Txtwatermark.layout = watermarklayout.diagonal;

Step Seventh: Set the text to a watermark for the Word document;

Doc. Watermark = Txtwatermark;

Step Eighth: Save the document and reopen it;

Doc. SaveToFile ("watermark. Doc");
System.Diagnostics.Process.Start ("watermark. Doc");

Add a picture watermark Effect diagram:

Add a text watermark Effect diagram:

All code:

Using Spire.doc;
Using Spire.Doc.Documents;
 
Namespace Add_watermark_to_word
{
 class program
 {
  static void Main (string[] args)
  {
   Document doc = new document ();
   Doc. LoadFromFile ("Introduction to XML file. doc");
 
   Set image watermark
   /*picturewatermark picture = new Picturewatermark ();
   Picture. Picture = System.Drawing.Image.FromFile ("Flower _2.jpg");
   Picture. scaling =;
   Doc. Watermark = picture;*/
 
   //Set text watermark
   Textwatermark txtwatermark = new Textwatermark ();
   Txtwatermark.text = "Microsoft";
   Txtwatermark.fontsize = n;
   Txtwatermark.layout = watermarklayout.diagonal;
   Doc. Watermark = Txtwatermark;
 
   Doc. SaveToFile ("watermark. Doc");
   System.Diagnostics.Process.Start ("watermark. Doc");}}

 

Tip: Run this component without having to install word ha.

I hope this article can be helpful to friends who have the same needs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.