How to Add a Watermark Above the Text or Image into a Word Document?

  • Now word watermark I have the same problem, I want to add the watermark above the text and image. Please help me out again
    1、input docx and output docx in zipdocx.zip (179.2 KB)
    2、here’s my codecode.zip (1.2 KB)
    3、Aspose.Words version is 22.9.0image.png (1.9 KB)
    4、windows 10
    5、jdk 1.8

@good_luck Watermark is inserted into the documents header, which is under the main document’s body content. This is the same behavior as in MS Word when you insert a watermark. By the way Aspose.Words has a built-in methods for inserting watermarks, please see Watermark class for more information.

To insert a watermark in front of the content you have to insert it in the main body, but in this case you will have to insert it on each page of the document. LayoutCollector can help you with this. For example see the following simple code:

Document doc = new Document("C:\\Temp\\in.docx");

// Determine the maximum ZOrted of shapes in the document.
int zOrder = 0;
Iterable<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape s : shapes)
{
    zOrder = Math.max(zOrder, s.getZOrder());
}
zOrder++;

// Create a watermark shape.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.setName("WaterMark");
// Set up the text of the watermark.
watermark.getTextPath().setText("In Front Watermark");
watermark.getTextPath().setBold(true);
watermark.getTextPath().setSize(100);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500);
watermark.setHeight(100);
watermark.getFill().setForeColor(Color.lightGray);
watermark.setStrokeColor(Color.lightGray);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(45);
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setZOrder(zOrder);

// Get paragraphs in the document.
Iterable<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

LayoutCollector layoutCollector = new LayoutCollector(doc);
int pageToInsert = 1;
for (Paragraph para : paragraphs)
{
    // Process only paragraphs in the main body.
    if (para.getAncestor(NodeType.BODY) == null)
        continue;

    int paraPage = layoutCollector.getEndPageIndex(para);
    if (paraPage == pageToInsert)
    {
        pageToInsert++;
        para.appendChild(watermark.deepClone(true));
    }
}

doc.save("C:\\Temp\\out.docx");

// Since we have used LayoutCollector, Aspose.Words cached layout of the document
// and the modifications we have made are not in the cache.
// Update page layout to reflect the changes in the output fixed Page formats.
doc.updatePageLayout();
doc.save("C:\\Temp\\out.pdf");

Thank you so much for your assistance

1 Like

A post was split to a new topic: How to Add a Watermark Above the Text or Image into a Excel Document?

Why can the watermark you added in this way be removed after you open the document? Please reply as soon as possible.

@good_luck The watermark is a simple shape, so the end user can work with it as with regular shape in the document.
You can protect your document and make it read-only to restrict editing. Please see our documentation to learn more:
https://docs.aspose.com/words/java/protect-or-encrypt-a-document/

Watermarking does not limit the editing capabilities of the document, but can you provide other ways to add watermarks? Add the watermark to everything in the document and to the images. The watermark.class that comes with aspose can only add one watermark, but we need to add the watermark in different places like in the document I provided above.

@good_luck I am afraid, there is no way to add a watermark above documents content and make this watermark not editable/removable. This cannot not be done neither in MS Word nor in Aspose.Words. MS Word formats simply does not allow to achieve this.

There are other ways to add a watermark without using aspose.word.shape.class, but make sure the watermark is always above the content and image, And add multiple watermarks

@good_luck No, unfortunately, there is no other way to add watermarks into the document.

What is the method of setting transparency of Shape in Word? The shape.setOpacity() method is deprecated.

@good_luck You can use Shape.Fill.setOpacity to specify opacity of shape fill and Shape.Fill.setOpacity.setTransparency to set transparency of shape fill. These properties are opposite to each other.

Thanks for your help!

1 Like

Why does it not work when I set it? I also set the value of transparency when I set the color (v in the red box in the picture) and it doesn’t work either.
image.png (56.1 KB)
image.png (61.6 KB)

I use waterShape.setStrokeTransparency(1-opacity) is ok

@good_luck You are right, transparency of shape fill and shape stroke are set separately. Please let us know if you need more assistance. We will be glad to help you.

Thank you again

1 Like