Add multiple watermarks to a word document

I tried to add multiple watermarks to each page in a word document, but failed. What should I do?
My code:

for (Section section : doc.getSections())
{

    double pageWidth = section.getPageSetup().getPageWidth();
    double pageHeight = section.getPageSetup().getPageHeight();

    double stepX = 200;
    double stepY = 150;

    for (double x = 0; x < pageWidth; x += stepX)
    {
        for (double y = 0; y < pageHeight; y += stepY)
        {

            Shape shape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
            shape.getTextPath().setText("Test");
            shape.setRotation(45);
            shape.setWidth(400);
            shape.setHeight(90);
            shape.setFillColor(java.awt.Color.GRAY);
            shape.setStrokeColor(java.awt.Color.GRAY);

            shape.setLeft(x);
            shape.setTop(y);
            Paragraph para = new Paragraph(doc);
            para.appendChild(shape);

            section.getBody().appendChild(para);
        }
    }
}

@serendipity.zhq As you may know MS Word document are flow by their nature and there is no “Page” concept. The consumer applications reflows the document content into pages on the fly.

Normally, 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.

You can try inserting “watermark” like shape in the main document body, in this case you will have to insert it on each page of the document. LayoutCollector can help you with this. Please try using the following 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");

Is it possible to achieve this tiling effect?

@serendipity.zhq Could you please attach the expected output in DOCX format?

There is no such file, I have only seen it on pdf files, I wonder if this effect can be achieved in docx format

And the code you gave above is strictly speaking WordArt, it is editable, I want a non-editable watermark in this file
test3.docx (12.6 KB)

@serendipity.zhq You can put such watermark as in the attached document using the following simple code:

Document doc = new Document();
doc.getWatermark().setText("TOP SECRET");
doc.save("C:\\Temp\\out.docx");

If you need not editable watermark, you can use image as a watermark using Watermark.setImage() method.

Please see our documentation to learn how to work with watermarks:
https://docs.aspose.com/words/java/working-with-watermark/

I know that the simple code above can add a watermark, but it can only add one. As in the example on the 3th floor(Add multiple watermarks to a word document - #3 by serendipity.zhq), what I want is a watermark effect that fills the entire page. I don’t know if it can be achieved.

@serendipity.zhq You can create an image with such repetitive text and add it as a watermark.

Thank you for your reply, it gave me a good idea.

1 Like