Change the Watermark Image Size According to Page Size using Java

Good day.

I have a Java program where I apply an image watermark using ImageWatermarkOptions. Previously, I had been setting the size to automatically fit the pages of the document using options.setScale(0);. However, requirements have been changed so that the watermark must only cover 75% of the page, not to fit to the size of the entire page. How can this functionality be achieved?

Alternatively, is there a way to make the watermark only fit the normal content of the page, without extending to the Header and Footer of the page? As in, the watermark would never stretch long enough to go inside the area of the Header and the Footer of the page.

@mohammed.allulu

Could you please ZIP and attach your input and expected output Word documents? We will then provide you more information about your query along with code example.

Files.zip (439.6 KB)

Please find the files attached.

@mohammed.allulu

We have logged a new feature request as WORDSNET-22447 to change the size of watermark according to page size. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

You can get the Shape node in the header of document and set the image’s size as shown below. Hope this helps you.

Shape image = (Shape)doc.getFirstSection().getHeadersFooters().get(HeaderFooterType.HEADER_PRIMARY).getChild(NodeType.SHAPE, 0, true);
if (image.hasImage())
{
    image.setWidth(image.getWidth() - image.getWidth() * .50);
    image.setHeight(image.getHeight() - image.getHeight() * .50);
}

doc.save(MyDir + "21.6.java.docx");

@tahir.manzoor

Thank you for filing the request!

I have tried out your code snippet, and from what I gathered, this snippet adjusts the size of the first shape found in all the Shape type nodes in the HeaderFooter section, correct? In most cases, the watermark that I am inserting using ImageWatermarkOptions is typically not the first Shape encountered in HeaderFooter, since the document in question might have already had images/shapes in the HeaderFooter prior to adding my watermark to the document.

Keeping that in mind, is there a way to explicitly adjust the size of the inserted watermark in specific?

If not, then may you guide me towards how to manually add an image as a watermark, such that I may manually adjust its properties?

Many thanks.

@mohammed.allulu

Please use the following code example to insert the watermark into document. You can set the size of watermark according to your requirement using Shape.Width and Shape.Height properties.

Document doc = new Document(MyDir + "input.docx");
insertWatermarkImage(doc, MyDir + "Watermark.png");
doc.save(MyDir + "21.6.docx");
private static void insertWatermarkImage(Document doc, String imagepath) throws Exception {
   
    Shape watermark = new Shape(doc, ShapeType.IMAGE);

    watermark.getImageData().setImage(imagepath);

    watermark.setWidth(200);
    watermark.setHeight(300);
    
    // Place the watermark in the page center.
    watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    watermark.setWrapType(WrapType.NONE);
    watermark.setVerticalAlignment(VerticalAlignment.CENTER);
    watermark.setHorizontalAlignment(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.
    for (Section sect : doc.getSections()) {
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
    }
}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception {
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

    if (header == null) {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.getDocument(), headerType);
        sect.getHeadersFooters().add(header);
    }

    // Insert a clone of the watermark into the header.
    header.appendChild(watermarkPara.deepClone(true));
}