Can not remove watermark

Hi Team,

I refer to below code sample to watermark or remove watermark for my Word document. Could you help take a look my Failure Case and advise, thanks

Happy Case:
Step 1. Call setTextWatermark to apply the watermark. (Success)
Step 2. Call removeTextWatermark to remove the watermark. (Success)

Failure Case:
Step 1. Call setTextWatermark to apply the watermark. (Success)
Step 2. Open the file generated with watermark and try some editing and save.
Step 3. Call removeTextWatermark to remove the watermark for the file save in step 2. (Failed: watermark still existing in the file)

public static Document setTextWatermark(Document doc) throws Exception {
    // Create a watermark shape, this will be a WordArt shape.
    Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
    watermark.setName(watermarkShapeName);
    watermark.getTextPath().setText(watermarkText);
    watermark.getTextPath().setFontFamily("Arial");
    watermark.setWidth(500.0);
    watermark.setHeight(100.0);
        
    // 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.
    Color color = new Color(220, 220, 220);
    watermark.setFillColor(color);
    watermark.setStrokeColor(color);
        
    // 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 : (Iterable<Section>) doc.getSections())
    {
        // There could be up to three different headers in each section.
        // Since we want the watermark to appear on all pages, insert it into all headers.
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
    }
    return doc;
}
    
private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType)
{
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        
    if (header == null)
    {
        // There is no header of the specified type in the current section, so we need to 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));
}
    
public static Document removeTextWatermark(Document doc) throws Exception {
    for (HeaderFooter hf : (Iterable<HeaderFooter>) doc.getChildNodes(NodeType.HEADER_FOOTER, true)) {
        for (Shape shape : (Iterable<Shape>) hf.getChildNodes(NodeType.SHAPE, true)) {
            if (shape.getName().contains(watermarkShapeName))
                shape.remove();
        }
    }
    return doc;
}

@larryzeng I have tested the scenario and watermark is properly removed from the document. Here is the code I have used for testing:

Document doc = new Document("C:\\Temp\\in.docx");
setTextWatermark(doc);
doc.save("C:\\Temp\\out.docx");
        
Document doc1 = new Document("C:\\Temp\\out.docx");
removeTextWatermark(doc1);
doc1.save("C:\\Temp\\out1.docx");

Also, you can use built-in method to add and remove watermarks:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getWatermark().setText("My Watermark");
doc.save("C:\\Temp\\out.docx");
        
Document doc1 = new Document("C:\\Temp\\out.docx");
doc1.getWatermark().remove();
doc1.save("C:\\Temp\\out1.docx");

Yes, above code are OK if the Word doc is simple, but if my Word doc has an Logo icon in the header, I could not remove the watermark using the sample code provided by aspose.

But finally, I could remove watermark by revising the sample code as below, please advise if it is the right way to do so?

@larryzeng Could you please attach your problematic document here for testing? We will check the issue and provide you more information.

Also, why do not you use built-in method to add and remove watermarks?

doc.getWatermark().setText("My Watermark");
doc.getWatermark().remove();

I tried with the build-in method with no luck, and the build-in method will also remove my header logo while adding the watermark.

Actually my Word doc header logo is added by Watermark as below, and this doc is prepared by other department and I could not ask them to change the way by adding the header logo.

image.png (70.5 KB)

Please find below Word doc files for your checking, thanks.
original.docx (93.6 KB)

watermarked and update the doc then watermark could not be removed.docx (94.9 KB)

@larryzeng In both MS Word and in Aspose.Words watermarks are identified by shape name. Shape with name that starts with "PowerPlusWaterMarkObject" is text watermark and shape with name that contains "WordPictureWatermark" is a picture watermark.
So if you need to keep existing watermark and add new watermark, you should rename such shapes to make Aspose.Words and MS Word to stop identifying them as watermarks. For example see the following code:

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

// Rename existing watermarks.
Iterable<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape s : shapes)
{
    if (s.getName().contains("PowerPlusWaterMarkObject") || s.getName().contains("WordPictureWatermark"))
        s.setName("");
}

// Add new watermark
doc.getWatermark().setText("This is watermark");
doc.save("C:\\Temp\\out.docx");

// Open generated document and remove added watermark
Document doc1 = new Document("C:\\Temp\\out.docx");
// Remove watermark
doc1.getWatermark().remove();
doc1.save("C:\\Temp\\out_removed_watermark.docx");

In your code the problem might occurs because watermark shapes have the same name. Please try setting unique names for watermark shapes.

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType)
{
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        
    if (header == null)
    {
        // There is no header of the specified type in the current section, so we need to create it.
        header = new HeaderFooter(sect.getDocument(), headerType);
        sect.getHeadersFooters().add(header);
    }
    
    // Insert a clone of the watermark into the header.
    Paragraph watermark = (Paragraph) watermarkPara.deepClone(true);
    Iterable<Shape> shapes = watermark.getChildNodes(NodeType.SHAPE, true);
    for (Shape s : shapes)
    {
        if(s.getName().contains(watermarkShapeName))
            s.setName(s.getName() + HeaderFooterType.toString(headerType));
    }
        
    header.appendChild(watermark);
}

The code works for me, thanks~~~

1 Like