How to add a watermark to a document using Aspose.Words tutorial

 

       Sometimes you need to insert a watermark in a Word document, for example if you want to print a draft document or mark it as confidential.cool

       In Microsoft Word, you can quickly insert a watermark using the Insert Watermark command. Not many people who use this command realize that such a "watermark" is just a shape inserted with text into a header or footer, or in the center of the page.embarrassment

       While in Aspose.Words, there is no single "Insert Watermark" command like Microsoft Word, it's easy to insert any shape or image into a header or footer, thus creating a watermark of any imaginable type.shouting

Example

Insert a watermark into a Word document.cool

using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Fields;
namespace AddWatermark
{
public class Program
{
public static void Main(string[] args)
{
// Sample infrastructure.
string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar;
string dataDir = new Uri(new Uri(exeDir), @"../../Data/").LocalPath;
Document doc = new Document(dataDir + "TestFile.doc");
InsertWatermarkText(doc, "CONFIDENTIAL");
doc.Save(dataDir + "TestFile Out.doc");
}
/// <summary>
/// Inserts a watermark into a document.
/// </summary>
/// <param name="doc">The input document.</param>
/// <param name="watermarkText">Text of the watermark.</param>
private static void InsertWatermarkText(Document doc, string watermarkText)
{
// Create a watermark shape. This will be a WordArt shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.TextPlainText);

// Set up the text of the watermark.
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = "Arial";
watermark.Width = 500;
watermark.Height = 100;
// Text will be directed from the bottom-left to the top-right corner.
watermark.Rotation = -40;
// Remove the following two lines if you need a solid black text.
watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark
watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark
// Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark);
// Insert the watermark into all headers of each document section.
foreach (Section sect in doc.Sections)
{
// There could be up to three different headers in each section, since we want
// the watermark to appear on all pages, insert into all headers.
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}
}
private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
// Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(true));
}
}
}

 

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports Aspose.Words
Imports Aspose.Words.Drawing
Imports Aspose.Words.Fields
Namespace AddWatermark
Public Class Program
Public Shared Sub Main(ByVal args() As String)
' Sample infrastructure.
Dim exeDir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar
Dim dataDir As String = New Uri(New Uri(exeDir), "../../Data/").LocalPath
Dim doc As New Document(dataDir & "TestFile.doc")
InsertWatermarkText(doc, "CONFIDENTIAL")
doc.Save(dataDir & "TestFile Out.doc")
End Sub
''' <summary>
''' Inserts a watermark into a document.
''' </summary>
''' <param name="doc">The input document.</param>
''' <param name="watermarkText">Text of the watermark.</param>
Private Shared Sub InsertWatermarkText(ByVal doc As Document, ByVal watermarkText As String)
' Create a watermark shape. This will be a WordArt shape.
' You are free to try other shape types as watermarks.
Dim watermark As New Shape(doc, ShapeType.TextPlainText)
' Set up the text of the watermark.
watermark.TextPath.Text = watermarkText
watermark.TextPath.FontFamily = "Arial"
watermark.Width = 500
watermark.Height = 100
' Text will be directed from the bottom-left to the top-right corner.
watermark.Rotation = -40
' Remove the following two lines if you need a solid black text.
watermark.Fill.Color = Color.Gray ' Try LightGray to get more Word-style watermark
watermark.StrokeColor = Color.Gray ' Try LightGray to get more Word-style watermark
' Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page
watermark.WrapType = WrapType.None
watermark.VerticalAlignment = VerticalAlignment.Center
watermark.HorizontalAlignment = HorizontalAlignment.Center
' Create a new paragraph and append the watermark to this paragraph.
Dim watermarkPara As New Paragraph(doc)
watermarkPara.AppendChild(watermark)
' Insert the watermark into all headers of each document section.
For Each sect As Section In doc.Sections
' There could be up to three different headers in each section, since we want
' the watermark to appear on all pages, insert into all headers.
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven)
Next sect
End Sub
Private Shared Sub InsertWatermarkIntoHeader(ByVal watermarkPara As Paragraph, ByVal sect As Section, ByVal headerType As HeaderFooterType)
Dim header As HeaderFooter = sect.HeadersFooters(headerType)
If header Is Nothing Then
' There is no header of the specified type in the current section, create it.
header = New HeaderFooter(sect.Document, headerType)
sect.HeadersFooters.Add(header)
End If
' Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(True))
End Sub
End Class
End Namespace

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326907922&siteId=291194637