Add Watermark in Word document using Java

Hi,


I want to insert a watermark in a Word file and convert it to PDF.

I’m using this code:

Document doc = new Document(“test.docx”);
insertWatermarkText(doc, “BORRADOR”);
doc.save(“test.pdf”);

public static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(“Arial”);
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

// 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.getParagraphBreakFont().setSize(0);
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);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));
sect.getHeadersFooters().add(header);
}

However, the watermark is inserting an space in the header (between the horizontal line and the text “Test Plantillas”). You can see it in the attached files.

Some zones of the Word document are protected, but if you need to unprotect them, the password is 1234.

Can you help me to avoid the watermark inserts this space?

Thank you very much.

We are also experiencing the same issue when we perform multiple operations on a document like (first apply DRAFT watermark, then remove DRAFT & apply APPROVED and then remove it to add EFFECTIVE etc…). This is pushing the text down and if we see the header, it has spaces in it…


Appreciate if any resolution on this…thx

Hi there,


Thanks for your inquiry. In the shared code example, you are inserting empty paragraph in the header of document. The watermark shape node is inside that paragraph.

In your case, we suggest you please do not insert new paragraph in the header if it already contains the paragraphs. Please use the following modified code example to get the desired output. Hope this helps you.

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>private static void insertWatermarkIntoHeader_modified(Shape watermark, 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);

Paragraph watermarkPara = new Paragraph(watermark.getDocument());
watermarkPara.getParagraphBreakFont().setSize(0);
watermarkPara.appendChild(watermark);
header.appendChild(watermarkPara.deepClone(true));
sect.getHeadersFooters().add(header);
}
else
{
Paragraph watermarkPara = header.getFirstParagraph();
watermarkPara.appendChild(watermark.deepClone(true));
}

<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>}
------------------------------------------------------------------------------------------------------------

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”> public static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(“Arial”);
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

// 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.getParagraphBreakFont().setSize(0);
// 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_modified(watermark, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader_modified(watermark, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader_modified(watermark, sect, HeaderFooterType.HEADER_EVEN);
}
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”> }

Thank you very much!!


I have changed the method insertWatermarkIntoHeader_modified because when the code enters in the first case (when the header is null) the above code delete the headers of all the pages but the first. However, if i change it for:

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

if (header != null) {
Paragraph watermarkPara = header.getFirstParagraph();
watermarkPara.appendChild(watermark.deepClone(true));
}
}

It works fine and adds the watermark in all the pages without adding any extra space.

So thank you very much for your help!!!

Best regards
Hi there,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

@CinfaSA

We have introduced new feature in Aspose.Words for Java 20.5 to add watermark (text and image) into document. Please refer to the following article for more detail.
Working with Watermark