I’m using this tutorial to add a shape watermark into my document.
The document is created using HTML templates. The document has header and footer. I’m using Aspose.Words 25.3.
Describe the issue
The result is that the watermark is added to every page, but the header is removed from page 2 and onwards.
When I add watermark using document.Watermark.SetText method, headers are present on every page, but I do not like the word-art way it looks.
What is wrong in my code?
Is the documentation correct?
Maybe there is something missing there?
Reproduction steps
You can find the solution and reproduction project in attachments. Add a valid licence to \wordsissue\Licences\Aspose.Words.Net.lic and run it. Results will be stored in \wordsissue\bin\results folder.
It seems that you are experiencing an issue where adding a shape watermark to your document is causing the header to disappear from page 2 and onwards. This can happen due to how the watermark is being inserted into the document’s headers.
When you add a watermark, it is essential to ensure that it is inserted into all headers of each section in the document. If the watermark is not correctly added to the headers, it may lead to unexpected behavior, such as the header disappearing on subsequent pages.
Here’s a general approach to ensure that the watermark is added correctly:
Create the Watermark Shape: Define the shape for your watermark, including its text, font, size, and rotation.
Insert the Watermark into Headers: Make sure to insert the watermark into all types of headers (primary, first, and even) for each section of the document. This ensures that the watermark appears consistently across all pages.
Here is a code snippet that demonstrates how to insert a watermark into all headers:
private void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
// Create the header if it does not exist
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
// Insert a clone of the watermark into the header
header.AppendChild(watermarkPara.Clone(true));
}
Make sure to call this method for each header type in your document sections. This should help maintain the headers while adding the watermark.
If you continue to face issues, please check the structure of your document and ensure that the headers are not being inadvertently modified or removed during the watermark insertion process.
If you have a reproduction project, reviewing the specific implementation might also help identify any discrepancies.
Feel free to reach out if you need further assistance!
@mlamla The problem occurs because the code creates header/footer if it does not exists. In MS Word if header/footer is not present, it is inherited from the previous section. Please modify your code like this:
private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
// Do not create header/footer if the section is not first.
if (sect != ((Document)sect.Document).FirstSection)
return;
// There is no header of the specified type in the current section, so we need to create it.
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
// Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(true));
}
Thank you @alexey.noskov - this actually fixes the issue.
Maybe you could consider adding this part to the documentation, that I was using? It seems like important addition for multi-page documents.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.