Adding watermark to document is removing header from page 2

Hi,
I’m using Aspose.Words version 19.9.0 in a .Net project.

I’m adding a text watermark to all pages of the document, but i noticed that doing so is deleting the content of the header of all pages after page 1.
I would like the header of all pages to stay as they are even after adding the watermarks.

Attached you can find 2 documents with expected result and with current (wrong) result after i add the watermark:
docs.zip (53.2 KB)

Below the code i’m using in my C# project to add the watermark to all pages of the document:

private static void AddWatermark(Document document, string watermarkText)
{
    var watermark = new Shape(document, ShapeType.TextPlainText);

    watermark.TextPath.Text = watermarkText;
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 500;
    watermark.Height = 100;

    watermark.Rotation = -40;

    watermark.Fill.Color = Color.LightGray;
    watermark.StrokeColor = Color.LightGray;

    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.HorizontalAlignment = HorizontalAlignment.Center;

    var watermarkParagraph = new Paragraph(document);
    watermarkParagraph.AppendChild(watermark);

    foreach (var node in document.Sections)
    {
        if (!(node is Section section))
            continue;

        InsertWatermarkIntoHeader(watermarkParagraph, section, HeaderFooterType.HeaderPrimary);

        // There could be up to three different headers in each section
        // if you add more headers there you must increase index in all Word tests 
        // in method CheckWordAfterReplacement by number of headers you add here
        InsertWatermarkIntoHeader(watermarkParagraph, section, HeaderFooterType.HeaderFirst);
        InsertWatermarkIntoHeader(watermarkParagraph, section, HeaderFooterType.HeaderEven);
    }
}

private static void InsertWatermarkIntoHeader(Paragraph watermarkParagraph, Section section,
    HeaderFooterType headerType)
{
    var header = section.HeadersFooters[headerType];


    if (header == null && headerType == HeaderFooterType.HeaderPrimary)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(section.Document, headerType);
        section.HeadersFooters.Add(header);
    }

    if (header != null)
        // Insert a clone of the watermark into the header.
        header.AppendChild(watermarkParagraph.Clone(true));
}

I noticed that the line of code that creates the problem seems to be this one, regarding HeaderPrimary:
InsertWatermarkIntoHeader(watermarkParagraph, section, HeaderFooterType.HeaderPrimary);

Thank you for any help.

@aldi.elfo Could you please attach your input document here for testing? Also, please try using the built-in method for adding a watermark into the document:

Document doc = new Document(@"C:\Temp\in.docx");
doc.Watermark.SetText("My Cool Watermark");
doc.Save(@"C:\Temp\out.docx");

Hi, attached you can find the input document.
Input document.zip (27.1 KB)

this document has 2 pages with headers and no watermark. I add the watermarks (as shapes) on all pages using the code i posted previously, which i found on the official documentation:
https://docs.aspose.com/words/net/working-with-watermark/

@aldi.elfo I cannot reproduce the problem on my side with the attached document. Both the following code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.Watermark.SetText("My Cool Watermark");
doc.Save(@"C:\Temp\out.docx");

and the following

Document doc = new Document(@"C:\Temp\in.docx");
AddWatermark(doc, "My Cool Watermark");
doc.Save(@"C:\Temp\out.docx");

properly preserve the header on both pages.

I suspect, the problem on your side might occur because header/footer in the second section in your problematic document is inherited from the first section. In this case the following condition passes for the second section

if (header == null && headerType == HeaderFooterType.HeaderPrimary)

and empty header is added.

@alexey.noskov sorry, please retry with this other example. The difference here is that the header of second page is “linked to previous”. You should be able to reproduce with this one.
input document.zip (18.6 KB)

Also, as stated in the first post, i’m using Aspose.Words version 19.9.0, which does not contain the Watermark class which has been added in successive versions, so i cannot use the code you suggested me.

I tried anyway with the code you suggested just to see the result and the problem is still present even with your code.

@aldi.elfo The problem is not reproducible with a built-in method for adding watermark. Please modify your code like this:

private static void InsertWatermarkIntoHeader(Paragraph watermarkParagraph, Section section,
    HeaderFooterType headerType)
{
    var header = section.HeadersFooters[headerType];

    // Create header only in the first section.
    if (section == section.Document.FirstChild && header == null && headerType == HeaderFooterType.HeaderPrimary)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(section.Document, headerType);
        section.HeadersFooters.Add(header);
    }

    if (header != null)
        // Insert a clone of the watermark into the header.
        header.AppendChild(watermarkParagraph.Clone(true));
}

The code adds primary header only into the first section if it document not exist. Other section will inherit the primary header if it is not set explicitly.
out.docx (17.8 KB)

@alexey.noskov the last code you sent will not work, since for the example i sent you, the header variable does have a value at runtime and will not enter the first if branch (since header != null), but the second one.

@aldi.elfo Your document has two sections. The condition should not pass for the second section. For the first section it should not pass as well, since the first section already have primary header. I have tested the code with your document and it works as expected.

@alexey.noskov yes, for both sections the condition does not pass. What i meant was that even before adding the condition

section == section.Document.FirstChild

the code was not passing through the first IF branch, it was already going in the second IF, doing

header.AppendChild(watermarkParagraph.Clone(true));

So the result is still incorrect, with missing header of page 2.

@aldi.elfo Please see the output document I have attached: out.docx (17.8 KB)
It is produced by the modified code from your template. Both header and watermark is there on the second and on the first page.

@alexey.noskov Are you using version 19.9.0? I’m not getting a correct result.
I noticed I get the correct result if I remove the “Link to previous” setting on the header of page 2 of the input document: image.png (37.8 KB)

I also noticed that the property Document.Section.HeadersFooters.IsLinkedToPrevious = false always even when on the input document is active. I would expect it to change value if i change the setting on the input document.

@aldi.elfo I have used the latest 23.6 version of Aspose.Words for testing.

Usually, header/footer linked to previous is null . But both cases are valid, section can have header, which is explicitly linked to previous. In this case header/footer is not null and IsLinkedToPrevious returns true. For example, this can occur if you create header/footer programmatically and set IsLinkedToPrevious flag.