Aspose Word Watermark Text Not Displaying on Some Pages

Hi, I have used below code to insert watermark text in word document on all pages. However for some pages, the watermark is not displaying. My word document is having both portrait and landscape orientation, tables, paragraphs. Below is the code used. Please let me know what’s missing in this code. Thanks in advance.
Attached is the word document I used for testing.

static void Main(string[] args)
{
    Aspose.Words.Document doc = new Aspose.Words.Document("Doc1.docx");
    NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
    var collector = new LayoutCollector(doc);
    Paragraph anchorPara = null;
    int pageIndex = 1;
    foreach (Paragraph para in paragraphs)
    {
        if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null && para.GetAncestor(NodeType.Table) == null)
        {
            anchorPara = para;
            Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
            watermark.TextPath.Text = "Strictly Confidential";
            watermark.TextPath.FontFamily = "Arial";
            watermark.TextPath.Size = 65;
            watermark.Width = 825;
            watermark.Height = 75;
            watermark.ZOrder = int.MaxValue;
            watermark.RelativeVerticalPosition = RelativeVerticalPosition.Margin;
            watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Margin;
            watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
            watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
            if (!doc.GetPageInfo(pageIndex - 1).Landscape)
                watermark.Rotation = -55;
            else
                watermark.Rotation = -35;

            watermark.Fill.Color = System.Drawing.Color.FromArgb(70, 70, 70);
            watermark.Fill.Opacity = 0.4;
            watermark.Stroked = false;
            watermark.Name = $"WaterMark_{pageIndex}";
            watermark.WrapType = WrapType.None;
            anchorPara.AppendChild(watermark);
            pageIndex++;
        }
    }
    doc.Save("Doc1withWatermark.docx");
}

Doc1.docx (24.7 KB)

Also, I have tried below code, it applies watermark on all pages, but headers are breaking and not appearing on some pages.


static void Main(string[] args)
{
    Aspose.Words.Document doc = new Aspose.Words.Document("Doc1.docx");
    Shape watermark = new Shape(doc, ShapeType.TextPlainText);
    watermark.TextPath.Text = "Strictly Confidential";
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 825;
    watermark.Height = 75;
    // Text will be directed from the bottom-left to the top-right corner.
    watermark.Rotation = -55;


    watermark.Fill.Color = System.Drawing.Color.Gray;
    watermark.StrokeColor = System.Drawing.Color.Gray;

    // Place the watermark in the page center.
    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
    watermark.HorizontalAlignment = Aspose.Words.Drawing.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.
    foreach (Section sect in doc.Sections)
    {
        if (sect.PageSetup.Orientation == Orientation.Landscape)
        {
            watermark.Rotation = -35;
        }
        else
        {
            watermark.Rotation = -55;
        }
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
    }
    doc.Save("Doc1withwaterMark.docx");
}

static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];

    if (header == null)
    {
        // There is no header of the specified type in the current section, 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));
}

@srinik16

Please use following code example to insert text watermark into document. For more detail, please read the following article.

Document doc = new Document(MyDir + "input.docx");
TextWatermarkOptions options = new TextWatermarkOptions()
{
    FontFamily = "Arial",
    FontSize = 36,
    Color = Color.Black,
    Layout = WatermarkLayout.Horizontal,
    IsSemitrasparent = false
};

doc.Watermark.SetText("Strictly Confidential", options);
doc.Save(MyDir + "21.7.docx");

Hi, Thank you for quick reply. The angle of text needs to be like left bottom to right top. But not able to align angle. Attached screenshot. 1.png (17.2 KB) We want to align like this 2.png (15.6 KB)

Also some of word document having images too. Watermark is not visible on the page if contains image. Tried BehindText property but still same issue. Attached sample word document with image. Doc1.docx (47.4 KB)
Kindly suggest.

@srinik16

We are working over your query and will get back to you soon.

1 Like

@srinik16

Please note that Aspose.Words mimics the behavior of MS Word. If you insert the watermark into document using MS Word, you will get the same output.

In this case, you need to insert the watermark on each page of document. Following code example shows how to insert watermark on each page of document.

We have attached the screenshot of output document for your kind reference.
output document.png (162.0 KB)

Document doc = new Document(MyDir + "Doc1.docx");

Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
    Run run = (Run)runs[i];
    int length = run.Text.Length;

    Run currentNode = run;
    for (int x = 1; x < length; x++)
    {
        currentNode = SplitRun(currentNode, 1);
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
foreach (Section section in doc.Sections)
{
    NodeCollection runNodes = section.Body.GetChildNodes(NodeType.Run, true);
    foreach (Run run in runNodes)
    {
        if (collector.GetStartPageIndex(run) == pageIndex)
        {
            builder.MoveTo(run);
            builder.StartBookmark("BM_Page" + pageIndex);
            builder.EndBookmark("BM_Page" + pageIndex);
            pageIndex++;
        }
    }
}

if (doc.PageCount == pageIndex)
{
    builder.MoveToDocumentEnd();
    builder.StartBookmark("BM_Page" + pageIndex);
    builder.EndBookmark("BM_Page" + pageIndex);
    pageIndex++;
}

collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    if (bookmark.Name.StartsWith("BM_"))
    {
        Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;

        Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
        watermark.IsLayoutInCell = false;

        watermark.TextPath.Text = "Strictly Confidential";
        watermark.TextPath.FontFamily = "Arial";
        watermark.TextPath.Size = 45;
        watermark.Fill.ForeColor = Color.Gray;
        watermark.StrokeColor = Color.Gray;
        watermark.Rotation = -45;
        watermark.Width = 500;
        watermark.Height = 50;
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        watermark.WrapType = WrapType.None;

        watermark.BehindText = false;

        para.AppendChild(watermark);

        bool isInCell = bookmark.BookmarkStart.GetAncestor(NodeType.Cell) != null;
        if (isInCell)
        {
            var renderObject = collector.GetEntity(bookmark.BookmarkStart);
            layoutEnumerator.Current = renderObject;
            watermark.IsLayoutInCell = true;
            layoutEnumerator.MoveParent(LayoutEntityType.Cell);
            RectangleF location = layoutEnumerator.Rectangle;

            PageSetup ps = watermark.ParentParagraph.ParentSection.PageSetup;
            watermark.Top = -location.Y + ps.PageHeight / 2 - watermark.Height / 2;
            watermark.Left = -location.X + ps.PageWidth / 2 - watermark.Width / 2;
        }
        else
        {
            watermark.VerticalAlignment = VerticalAlignment.Center;
            watermark.HorizontalAlignment = HorizontalAlignment.Center;
        }
    }
}

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.Compliance = OoxmlCompliance.Iso29500_2008_Strict;
doc.Save(MyDir + "21.7.docx", options);

@tahir.manzoor
Thanks for quick reply. Code example inserts watermark on each page of document. However still one scenario I’m finding challenge. Watermark position is not same in all page. Attached is the test document I used Doc2.docx (108.8 KB)
. Kindly help.
result1.docx (120.0 KB)
result2.docx (120.1 KB)
Result.png (107.5 KB)

@srinik16

We have tested the scenario and noticed that the Layout APIs return incorrect position of bookmark. So, you are facing this issue. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-22520. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

1 Like

@tahir.manzoor, Hi, Could you please let us know any tentative date to get this resolve. Status showing as Postponed. Please clarify.

@srinik16

Your requirement is related to watermark of document and it is hidden under images. Please note that Aspose.Words mimics the behavior of MS Word. If you insert watermark into document using MS Word, you will get the same output. The code example shared in this thread is workaround of this issue.

Your issue is related to missing feature WORDSNET- 9957 (ROOT Header contents overlap with the body content in output Pdf). The WORDSNET-22520 has been postponed due to this missing feature.

After the fix of WORDSNET- 9957, we will look into your issue. We will be sure to inform you via this forum thread as soon as this feature is available. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-22520) have been fixed in this Aspose.Words for .NET 23.5 update also available on NuGet.