Working with Watermark

This topic discusses how to work programmatically with watermark using Aspose.Words. A watermark is a background image that displays behind the text in a document. A watermark can contain a text or an image represented by the Watermark class.

Add a Watermark to a Document

In Microsoft Word, a watermark can easily be inserted in a document using the Insert Watermark command. Aspose.Words provides the watermark class to add or remove watermark in documents. Aspose.Words provides the WatermarkTypeenumeration defining three possible types of watermarks (Text, Image, and None) to work with. 

Add Text Watermark

The following code example demonstrates how to insert a text watermark in a document by defining TextWatermarkOptions using the SetText method:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Document.docx");
TextWatermarkOptions options = new TextWatermarkOptions()
{
FontFamily = "Arial",
FontSize = 36,
Color = Color.Black,
Layout = WatermarkLayout.Horizontal,
IsSemitrasparent = false
};
doc.Watermark.SetText("Test", options);
doc.Save(ArtifactsDir + "WorkWithWatermark.AddTextWatermark.docx");

Add Image Watermark

The following code example demonstrates how to insert an image watermark in a document by defining ImageWatermarkOptions using the SetImage method:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Document.docx");
ImageWatermarkOptions options = new ImageWatermarkOptions
{
Scale = 5,
IsWashout = false
};
doc.Watermark.SetImage(Image.FromFile(ImagesDir + "Transparent background logo.png"), options);
doc.Save(ArtifactsDir + "WorkWithWatermark.AddImageWatermark.docx");

The watermark can also be inserted using shape class as well. It is very easy to insert any shape or image into a header or footer and thus create a watermark of any imaginable type.

The following code example inserts a watermark into a Word document:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
[Test]
public void AddAndRemoveWatermark()
{
Document doc = new Document(MyDir + "Document.docx");
InsertWatermarkText(doc, "CONFIDENTIAL");
doc.Save(ArtifactsDir + "WorkWithWatermark.AddWatermark.docx");
RemoveWatermarkShape(doc);
doc.Save(ArtifactsDir + "WorkWithWatermark.RemoveWatermark.docx");
}
/// <summary>
/// Inserts a watermark into a document.
/// </summary>
/// <param name="doc">The input document.</param>
/// <param name="watermarkText">Text of the watermark.</param>
private void InsertWatermarkText(Document doc, string watermarkText)
{
// Create a watermark shape, this will be a WordArt shape.
Shape watermark = new Shape(doc, ShapeType.TextPlainText) { Name = "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.FillColor = Color.Gray;
watermark.StrokeColor = Color.Gray;
// 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 it into all headers.
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}
}
private 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, so we need to 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));
}

Remove Watermark from a Document

The Watermark class provides the remove method to remove the watermark from a document.

The following code example shows how to remove a watermark from documents:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
// Add a plain text watermark.
doc.Watermark.SetText("Aspose Watermark");
// If we wish to edit the text formatting using it as a watermark,
// we can do so by passing a TextWatermarkOptions object when creating the watermark.
TextWatermarkOptions textWatermarkOptions = new TextWatermarkOptions();
textWatermarkOptions.FontFamily = "Arial";
textWatermarkOptions.FontSize = 36;
textWatermarkOptions.Color = Color.Black;
textWatermarkOptions.Layout = WatermarkLayout.Diagonal;
textWatermarkOptions.IsSemitrasparent = false;
doc.Watermark.SetText("Aspose Watermark", textWatermarkOptions);
doc.Save(ArtifactsDir + "Document.TextWatermark.docx");
// We can remove a watermark from a document like this.
if (doc.Watermark.Type == WatermarkType.Text)
doc.Watermark.Remove();
doc.Save(ArtifactsDir + "WorkWithWatermark.RemoveDocumentWatermark.docx");

If the watermarks are added using the Shape class object then to remove the watermark from a document you have to set only the name of watermark shape during inserting and then remove watermark shape by an assigned name.

The following code example show you how to set the name of the watermark shape and remove it from the document:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
// Create a watermark shape, this will be a WordArt shape.
Shape watermark = new Shape(doc, ShapeType.TextPlainText) { Name = "Watermark" };
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
private void RemoveWatermarkShape(Document doc)
{
foreach (HeaderFooter hf in doc.GetChildNodes(NodeType.HeaderFooter, true))
{
foreach (Shape shape in hf.GetChildNodes(NodeType.Shape, true))
{
if (shape.Name.Contains("Watermark"))
{
shape.Remove();
}
}
}
}

Add a Watermark into a Table Cell

Sometimes you need to insert a watermark/image into a table’s cell and display it outside the table, you can use the IsLayoutInCell property. This property gets or sets a flag indicating whether the shape is displayed inside a table or outside of it. Note that this property works only when you optimize the document for Microsoft Word 2010 using the OptimizeFor method.

The following code example shows how to use this property:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();
builder.RowFormat.Height = 100;
builder.RowFormat.HeightRule = HeightRule.Exactly;
for (int i = 0; i < 31; i++)
{
if (i != 0 && i % 7 == 0) builder.EndRow();
builder.InsertCell();
builder.Write("Cell contents");
}
builder.EndTable();
Shape watermark = new Shape(doc, ShapeType.TextPlainText)
{
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
IsLayoutInCell = true, // Display the shape outside of the table cell if it will be placed into a cell.
Width = 300,
Height = 70,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Rotation = -40
};
watermark.FillColor = Color.Gray;
watermark.StrokeColor = Color.Gray;
watermark.TextPath.Text = "watermarkText";
watermark.TextPath.FontFamily = "Arial";
watermark.Name = $"WaterMark_{Guid.NewGuid()}";
watermark.WrapType = WrapType.None;
Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run;
builder.MoveTo(run);
builder.InsertNode(watermark);
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
doc.Save(ArtifactsDir + "WorkingWithShapes.LayoutInCell.docx");