Watermark is not coming in all pages

Hi Team,

I am using .NET Aspose for creating word document. I am using watermark but it is not coming in all the pages.

@ankurkedia Watermark is inserted into the documents header, which is under the main document’s body content. This is the same behavior as in MS Word when you insert a watermark. By the way Aspose.Words has a built-in methods for inserting watermarks, please see Watermark class for more information.
Additionally, can you please share with us the code that you are using to insert the Watermark in your document, that help us to reproduce your issue in our side.

@eduardo.canal Thanks for reply.
I am using Watermark class provided by Aspose.
Strange thing is that watermark is coming in call pages in my local system but when I deploy the same code in IIS Server, than watermark is not coming in few pages.

For Reference I am pasting psedu code:-

private static Document WaterMarkingInDoc(Stream sampledocument)
{
    Document doc = new Document(sampledocument);
    LayoutCollector collector = new LayoutCollector(doc);
    int pageIndex = 1;
    foreach (Aspose.Words.Section section in doc.Sections.OfType<Aspose.Words.Section>())
    {
        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in paragraphs.OfType<Paragraph>())
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {
                Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
                watermark.TextPath.Text = "TEST WATEMARK IN WORD";
                watermark.TextPath.FontFamily = "Arial";
                // Set the width height according to your requirements
                watermark.Width = 175;
                watermark.Height = 20;
                watermark.BehindText = false;
                watermark.Rotation = -40;
                watermark.FillColor = Color.FromArgb(200, Color.LightGray);
                watermark.StrokeColor = Color.FromArgb(200, Color.LightGray);
                PageSetup pageSetup = para.ParentSection.PageSetup;

                double contentWidth = pageSetup.PageWidth - (pageSetup.LeftMargin + pageSetup.RightMargin);
                watermark.Left = (contentWidth - watermark.Width) / 2;
                watermark.HorizontalAlignment = HorizontalAlignment.None;

                double contentHeight = pageSetup.PageHeight - (pageSetup.TopMargin + pageSetup.BottomMargin);
                watermark.Top = (contentHeight - watermark.Height) / 2;
                watermark.VerticalAlignment = VerticalAlignment.None;
                para.AppendChild(watermark);

                watermark.ZOrder = 2;
                pageIndex++;
            }
        }
    }
    return doc;
}

@ankurkedia I have reviewed your code, and I believe I have identified the potential source of the problem.

Firstly, you are managing watermarks in a different way compared to the MS Word application. As I mentioned earlier, MS Word (and Aspose.Words API) inserts the watermark as a shape in the header of the document. However, in your code, the watermark is inserted as paragraphs in the body of the document.

Secondly, and more importantly, you are using the following validation:
if (collector.GetStartPageIndex(para) == pageIndex)
I understand that you are using this validation to ensure that only one watermark is set per page. However, the issue with this approach is that MS Word documents are flow documents, which means that concepts like pages are not explicitly included in the document structure. Pages are calculated during the rendering process.

This means that you could have a paragraph that is too long and spans multiple pages, or you could have pages that contain only tables, for example. In these cases, the watermark shapes will not be included in some pages, leading to unexpected behavior.

Alternatively, you can consider using the Watermark class to set a watermark to the document (the cons with this approach is that the Watermark will be inserted behind the text and not above it), please check the following example:

Document doc = new Document(@"C:\Temp\input.docx");
TextWatermarkOptions options = new TextWatermarkOptions()
{
    FontFamily = "Arial",
    FontSize = 30,
    Color = Color.Black,
    Layout = WatermarkLayout.Diagonal,
    IsSemitrasparent = true,
};

doc.Watermark.SetText("TEST WATEMARK IN WORD", options);
// Save the result.
doc.Save(@"C:\Temp\output.docx");

@eduardo.canal Thanks for your quick response. I will try code snippets given by you an will update here the result.
But I have question. If the above code which I was using is having issue, it should be not working in my local system also.

1 Like

That depends on the document, also you need to look for font substitutions, if the font are not available in the running system Aspose will replace it in order to be able to render the document, that could lead to some discrepancy in the layout of the rendered document.
Additionally, can you please attach a sample of the conflicting file.

To check for font substitutions you can implement the IWarningCallback interface:

Document doc = new Document("C:\\Temp\\input.docx", new Aspose.Words.Loading.LoadOptions
{
    LoadFormat = LoadFormat.Docx,
    WarningCallback = new IssueCallBack()
});
public class IssueCallBack : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        Console.WriteLine($"{ info.Description}");
    }
}

@eduardo.canal, Thanks for your suggestion. Yes the issue is resolved.

2 Likes