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.

How to 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 WatermarkType enumeration 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:

Add Image Watermark

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

Image watermark can be inserted as image, string, or stream.

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:

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:

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:

Add a Watermark in 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:


FAQ

  1. Q: How do I add a text watermark to a document using Node.js?
    A: Create a Watermark object, configure a TextWatermarkOptions instance with the desired text, font, color, etc., and call watermark.setText(options). Finally, save the document.

  2. Q: Can I insert an image as a watermark, and what formats are supported?
    A: Yes. Use ImageWatermarkOptions to specify the image source (file path, stream, or base‑64 string). All image formats supported by Aspose.Words (PNG, JPEG, BMP, GIF, TIFF) can be used.

  3. Q: How can I remove a watermark that was added with the Watermark class?
    A: Call watermark.remove() on the document’s Watermark instance. This removes any watermark added via the Watermark API.

  4. Q: My watermark was added using a Shape; how do I delete it?
    A: When inserting the shape, assign a unique name (e.g., watermarkShape.setName("MyWatermark")). Later retrieve the shape by name with document.getChild(NodeType.SHAPE, true).where(s => s.getName() === "MyWatermark") and call shape.remove().

  5. Q: Is it possible to place a watermark inside a table cell but have it appear outside the cell boundaries?
    A: Set the shape’s isLayoutInCell property to false and ensure the document compatibility is set to Word 2010 or later using document.getSettings().getCompatibilityOptions().optimizeFor(CompatibilityOptions.OPTIMIZE_FOR_WORD_2010). This makes the watermark render outside the cell while being anchored to it.