We're sorry Aspose doesn't work properply without JavaScript enabled.

# 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.