Watermark hidden by table with blue shadow background color

Hi,

we are using aspose-word licensed version 14.12.0.0. I would like to integrate a watermark into a word document. The watermark is fully shown on all pages that contain text only. If I insert tables into my word document, the watermark is hidden (fully or partial) by the table. Is there any way to avoid this behavior. How can i achieve this?. Also we attached the same project and screenshot for your review and process to quick solution.

Note: Please include the dll aspose-word version 14.12.0.0.

We are using following code for watermark.

       System.IO.MemoryStream outStream = new System.IO.MemoryStream();
        var watermark = new Shape(doc, ShapeType.TextPlainText);
        var watermarkFormattedText = new FormattedText("Test Water Mark", 
        System.Drawing.Color.LightGray, "Calibri", EncodingType.Winansi, false, 66);
        watermark.TextPath.Text = "Test Water Mark";
        watermark.TextPath.FontFamily = "Calibri";

        watermark.Height = watermarkFormattedText.TextHeight;
        watermark.Width = watermarkFormattedText.TextWidth;

        // Text will be directed from the bottom-left to the top-right corner.
        watermark.Rotation = -40;
        watermark.Font.Size = 66;
        watermark.Fill.Color = System.Drawing.Color.LightGray;
        watermark.StrokeColor = System.Drawing.Color.LightGray;

        // Place the watermark in the page center.
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        watermark.WrapType = WrapType.None;
        watermark.BehindText = true;
        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.
        var watermarkParagraph = new Paragraph(doc);
        watermarkParagraph.AppendChild(watermark);

        // Insert the watermark into all headers of each document section.
        foreach (Section sect in doc.Sections)
        {
            // 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.
            InsertNodeIntoHeader(watermarkParagraph, sect, HeaderFooterType.HeaderPrimary);
            InsertNodeIntoHeader(watermarkParagraph, sect, HeaderFooterType.HeaderFirst);
            InsertNodeIntoHeader(watermarkParagraph, sect, HeaderFooterType.HeaderEven);
        }
        doc.Save(outStream, SaveFormat.Docx);

WaterMark.zip (1022.2 KB)

WaterMarkIssue.jpg (53.4 KB)

@smani

Thanks for your inquiry. The problem occurs because watermark shape resides inside header footer story and main content is inside body story. If you insert a watermark using Microsoft Word 2016, you will observe the same behavior. All content of document’s header/footer is always behind the main content of the document.

However, you may overcome this problem by manually inserting watermarks in each Page. Please see the following code for example:

Document doc = new Document(MyDir + "in.docx");
InsertWatermarkAtEachPage(doc, "watermark");
doc.Save(MyDir + "19.1.docx");

public static void InsertWatermarkAtEachPage(Document doc, string watermarkText)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);

    int pageIndex = 1;
    foreach (Section section in doc.Sections)
    {
        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in paragraphs)
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {
                builder.MoveToParagraph(paragraphs.IndexOf(para), 0);
                builder.StartBookmark("BM_Page" + pageIndex);
                builder.EndBookmark("BM_Page" + pageIndex);
                pageIndex++;
            }
        }
    }

    collector = new LayoutCollector(doc);
    LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
    //Set the position of watermark for tables
    const int PageRelativeY = 200;
    const int PageRelativeX = 50;

    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.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 500;
            watermark.Height = 100;
            // Text will be directed from the bottom-left to the top-right corner.
            watermark.Rotation = -40;
            // Remove the following two lines if you need a solid black text.
            watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark
            watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

            // Place the watermark in the page center.
            watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            watermark.WrapType = WrapType.None;
            watermark.VerticalAlignment = VerticalAlignment.Center;
            watermark.HorizontalAlignment = HorizontalAlignment.Center;

            watermark.Top = PageRelativeY;
            watermark.Left = PageRelativeX;
            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;

                layoutEnumerator.MoveParent(LayoutEntityType.Cell);
                RectangleF location = layoutEnumerator.Rectangle;

                watermark.Top = PageRelativeY - location.Y;
                watermark.Left = PageRelativeX - location.X;
            }
        }
    }
}

Hi @ [tahir.manzoor],

Thanks for the your solution. Based on your code, the watermark is fully shown on all pages with the table. But the content text is hidden. I need to show both waternark and content text, not hidden(with table also). How can i achieve this?
Please see the attached the screenshot for your review.

WaterMarkIssue1.jpg (38.5 KB)
WaterMarkIssue.jpg (63.7 KB)

@smani

Thanks for your inquiry. In your case, we suggest you please change the watermark’s fill color as transparent. Please check the following line of code. Hope this helps you.

watermark.Fill.Color = Color.Transparent;

Hi @ [tahir.manzoor],

Thanks for the your solution. Your solution is working fine as small (2 to 3 pages) documents. But one of our customer is used large size documents(15 pages) and table with blue shadow. The large documents facing performance issues and it take some more time(around 5 min) to apply watermark in word documents.

Also few large documents we are facing following issues,

Specified argument was out of the range of valid values.

Parameter name: paraIdx

We attached the large documents(Issue one) for your reference.
How to resolve performance and out of the range of valid values issue?

Please give the solution ASAP.

Thanks in advance.

Water-Mark-Test.zip (54.3 KB)

@smani

Thanks for your inquiry.

In your case, you are getting the page number of paragraphs in your document that takes time. Please note that performance and memory usage all depend on complexity and size of the documents you are generating.

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-18129. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi tahir.manzoor,

Thanks for your quick response. Any other alternative solution for apply watermark in word documents.

Please give the solution ASAP.

Thanks in advance.

@smani

Thanks for your inquiry. Please use the following code example to fix this issue. Hope this helps you.

Document doc = new Document(MyDir + "Water-Mark-Test.docx");
InsertWatermarkAtEachPage(doc, "watermark");
doc.UpdatePageLayout();
doc.Save(MyDir + "19.2.pdf");

public static void InsertWatermarkAtEachPage(Document doc, string watermarkText)
{
    const int PageRelativeY = 200;
    const int PageRelativeX = 50;

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

    int pageIndex = 1;
    foreach (Section section in doc.Sections)
    {
        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in paragraphs)
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {

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

                watermark.TextPath.Text = watermarkText;
                watermark.TextPath.FontFamily = "Arial";
                watermark.Width = 500;
                watermark.Height = 100;
                // Text will be directed from the bottom-left to the top-right corner.
                watermark.Rotation = -40;
                // Remove the following two lines if you need a solid black text.
                watermark.Fill.Color = Color.Transparent; // Try LightGray to get more Word-style watermark
                watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

                // Place the watermark in the page center.
                watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
                watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

                watermark.VerticalAlignment = VerticalAlignment.Center;
                watermark.HorizontalAlignment = HorizontalAlignment.Center;

                watermark.Top = PageRelativeY;
                watermark.Left = PageRelativeX;
                watermark.BehindText = false;

                para.AppendChild(watermark);

                bool isInCell = para.GetAncestor(NodeType.Cell) != null;

                if (isInCell)
                {
                    watermark.VerticalAlignment = VerticalAlignment.Default;
                    watermark.HorizontalAlignment = HorizontalAlignment.Default;

                    var renderObject = collector.GetEntity(para);
                    layoutEnumerator.Current = renderObject;

                    layoutEnumerator.MoveParent(LayoutEntityType.Cell);
                    RectangleF location = layoutEnumerator.Rectangle;

                    watermark.Top = PageRelativeY - location.Y;
                    watermark.Left = PageRelativeX - location.X;
                }


                pageIndex++;
            }
        }
    }
}

Hi tahir.manzoor,

Thanks for your valuable solution. But i am facing another one issue. One of our customer word document not applied the watermark in second page. How can i achieve this?

We attached the documents(second page Issue one) for your reference.

Please give the solution ASAP.

Thanks in advance.

Water-Mark-Word.zip (38.3 KB)

@smani

Thanks for your inquiry. You are facing this issue due to vertical merged cell. Please use the following modified code to get the desired output. We have attached the output PDF with this post for your kind reference. 19.2.pdf (103.3 KB)

if (isInCell)
{

//Modified code start...
    Cell cellmerge = (Cell)para.GetAncestor(NodeType.Cell);
    if (cellmerge.CellFormat.VerticalMerge != CellMerge.None)
    {
        if (cellmerge.ParentRow.Cells.Count > 1)
        {
            para.LastChild.Remove();
            Console.WriteLine(cellmerge.CellFormat.VerticalMerge);
            cellmerge.ParentRow.Cells[1].FirstParagraph.AppendChild(watermark);
        }
    }
//Modified code end...                            
                            
    watermark.VerticalAlignment = VerticalAlignment.Default;
    watermark.HorizontalAlignment = HorizontalAlignment.Default;

    var renderObject = collector.GetEntity(para);
    layoutEnumerator.Current = renderObject;

    layoutEnumerator.MoveParent(LayoutEntityType.Cell);
    RectangleF location = layoutEnumerator.Rectangle;

    watermark.Top = PageRelativeY - location.Y;
    watermark.Left = PageRelativeX - location.X;
}

Hi tahir.manzoor,

Thanks for your valuable solution. Your latest solution resolve the my issue.

But I am facing another one issue, 4th page water mark printed twice. How can i achieve this?

We attached the documents(4th page Issue one) for your reference.

Please give the solution ASAP.

Thanks in advance.

Water-Mark-Word1.zip (41.4 KB)

@smani

Thanks for your inquiry. We have tested the scenario using the latest version of Aspose.Words for .NET 19.2 and have not found the shared issue. Please check the attached output documents. Docs.zip (135.2 KB)

Hi tahir.manzoor,

Your latest modified code we are facing some watermark issue.

//Modified code start…
Cell cellmerge = (Cell)para.GetAncestor(NodeType.Cell);
if (cellmerge.CellFormat.VerticalMerge != CellMerge.None)
{
if (cellmerge.ParentRow.Cells.Count > 1)
{
para.LastChild.Remove();
Console.WriteLine(cellmerge.CellFormat.VerticalMerge);
cellmerge.ParentRow.Cells[1].FirstParagraph.AppendChild(watermark);
}
}
//Modified code end…

Some of the pages watermark printed twice in a page. This issue facing one of our customer. But customer not shared the original documents. I create same documents and tried to replicate for testing purpose.But unable to replicate in testing environment. This issue only occurred in customer environment. Also we facing some misalignment in a watermark.

I want to any other alternative simple solution for apply watermark in word documents. One of our customer waiting for this solution.

Please give the solution ASAP.

Thanks in advance.

@smani

Thanks for your inquiry. In case you are using old version of Aspose.Words, we suggest you please use the latest version of Aspose.Words for .NET 19.2. Hope this helps you.

If you still face problem, please share your input and problematic output documents here for testing. We will investigate the issue and provide you more information on it. Unfortunately, it is difficult to say what the problem is without documents. We need your documents for testing.

We have made this forum thread as private. Now, only you and Aspose staff members can access this forum thread. You can share your documents without any hesitation. Thanks for your cooperation.

Hi tahir.manzoor,

Thanks for your quick response. We want to any other alternative simple solution for apply watermark in word documents. One of our valuable customer waiting for solution this issue. Get a chance to give me the solution ASAP.

Thanks in advance.

@smani

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. You can insert the watermark in the header of document that will display in the whole document. However, this causes an issue that was reported by you in the first post of this thread. To overcome this issue, there is another way to insert watermark i.e. insert the watermark on each page of document.

Please note that the watermark in the Word document is imported as Shape node into Aspose.Words’ DOM. You need to insert it into the Paragraph node on a page. The InsertWatermarkAtEachPage does the same.

Please share the input document for which you are facing the duplicate watermark issue. We will investigate the issue and provide you more information on it.

The issues you have found earlier (filed as WORDSNET-18129) have been fixed in this Aspose.Words for .NET 19.3 update and this Aspose.Words for Java 19.3 update.