Section's ClearHeadersFooters method also removes Watermark

When ClearHeadersFooters method is called, it also removes the document’s watermark.

Is this depend on the docx file or is it a bug?

@meg.2891 This is not a bug, this is an expected behavior. Watermarks in MS Word documents are represented as shapes behind content in the document’s header. So when headers/footers are removed, the watermarks are also removed.

But in MS Word, we can remove headers and footers without removing the watermark. Is there any way to clear headers and footers items without removing watermark like MS Word?

@meg.2891 As I have mentioned, Watermarks in MS Word document are simple shape in the header behind main content. To distinguish them among other shapes MS Word and Aspose.Words use special shape names that starts either from "PowerPlusWaterMarkObject" or "WordPictureWatermark". So you can remove all content of headers/footers except the watermarks. For example see the following code:

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

foreach (HeaderFooter hf in doc.GetChildNodes(NodeType.HeaderFooter, true))
{
    // Get watermark shapes.
    List<Shape> watermarks = hf.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
        .Where(s => s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
        .ToList();

    if (watermarks.Count == 0)
    {
        hf.Remove();
    }
    else
    {
        hf.RemoveAllChildren();
        hf.AppendChild(new Paragraph(doc));
        foreach (Shape s in watermarks)
            hf.FirstParagraph.AppendChild(s);
    }
}

doc.Save(@"C:\Temp\out.docx");

Thank you. It works as expected.

In my opinion, this could be a change in future because if it would be encapsulated, it behaves more like MS Word and it is cleaner. Also, this could be an option in ClearHeadersFooters method.

Thanks either way!

1 Like

@meg.2891 ClearHeadersFooters removes all content of all headers and footers in the section. Since watermarks are also part of header, they are remove. This is an expected behavior f the method. We will consider adding an option to keep watermarks upon clearing headers/footers.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26552

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like