We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Watermark Goes Behind the Table Content

We are inserting the watermark to the word document as specified in the documentation but the watermark is getting behind the table (Aspose Words for .NET version 17.9.0.0). Is this some bug with this release or any problem with the following code.

    /// <summary>
    /// Inserts a watermark into a document.
    /// </summary>
    /// <param name="doc">The input document.</param>
    /// <param name="watermarkText">Text of the watermark.</param>
    private static void InsertWatermarkText(Document doc, string watermarkText)
    {
        // Create a watermark shape. This will be a WordArt shape. 
        // You are free to try other shape types as watermarks.
        Shape watermark = new Shape(doc, ShapeType.TextPlainText);
        watermark.Name = "WaterMark";

        // Set up the text of the watermark.
        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
        watermark.ZOrder = 1000;
        watermark.BehindText = true;

        // 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)
        {
            // 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);
        }
    }

    private 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));
    }

Output.jpg (259.2 KB)
Attached is the screen shot of first page of my word document. As you can see the watermark is getting behind the table data.

@relsys,

The problem occurs because watermark shape resides inside header footer story and main content is inside body story (please see Story class). 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. You can achieve this by moving the cursor to the first Paragraph in each Page of your document and then making those Paragraphs as an anchor points for your watermarks. Please see the following code for example:

public static void InsertWatermarkTextAtEachPage(Document doc, string watermarkText)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    PageSetup ps = builder.PageSetup;

    NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
    LayoutCollector collector = new LayoutCollector(doc);

    Paragraph anchorPara = null;
    int pageIndex = 1;
    foreach (Paragraph para in paragraphs)
    {
        if (collector.GetStartPageIndex(para) == pageIndex)
        {
            anchorPara = para;

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

            watermark.Width = 300;
            watermark.Height = 70;
            watermark.HorizontalAlignment = HorizontalAlignment.Center;
            watermark.VerticalAlignment = VerticalAlignment.Center;

            watermark.Rotation = -40;
            watermark.Fill.Color = Color.Gray;
            watermark.StrokeColor = Color.Gray;

            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";

            watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
            watermark.WrapType = WrapType.None;

            anchorPara.AppendChild(watermark);
            pageIndex++;
        }
    }
}
/////////////////////////////
Document doc = new Document(MyDir + @"in.docx");
InsertWatermarkTextAtEachPage(doc, "Draft");
doc.Save(MyDir + @"18.3.docx");

Thanks for the reply.

By inserting the watermark on each page, watermark shows correctly over the table contents but we are not able to fix the position of the watermark to the center of each page and also in some page the watermark is missing. Any suggestions to resolve this? Please see the output attached with position issue.

output2.jpg (282.0 KB)

@relsys,

Please ZIP and upload your input Word document (you are getting this problem with) here for testing. We will investigate the issue on our end and provide you more information.

Attached is my word document for your reference.

Output.zip (17.5 KB)

@relsys,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-16546. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

@relsys,

We tested the scenario with the following improved code:

Document doc = new Document(MyDir + @"output\output.doc");

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);
PageSetup ps = builder.PageSetup;

NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
foreach (Run run in smallRuns)
{
    if (collector.GetStartPageIndex(run) == pageIndex)
    {
        Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

        watermark.Width = 300;
        watermark.Height = 70;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

        watermark.Rotation = -40;
        watermark.Fill.Color = Color.Gray;
        watermark.StrokeColor = Color.Gray;

        watermark.TextPath.Text = "watermarkText";
        watermark.TextPath.FontFamily = "Arial";

        watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
        watermark.WrapType = WrapType.None;

        builder.MoveTo(run);
        builder.InsertNode(watermark);

        pageIndex++;
    }
}

doc.Save(MyDir + @"output\18.3.doc");

private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run)run.Clone(true);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring((0), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

Thanks for update but i still see the watermark not getting adding at the center from second page onward and also watermark is missing on the last page.

@relsys,

Your problem will be fixed as soon as the linked issues are resolved. We will keep you posted on further updates.

Is there a timeline available for this fix?

@relsys,

Thanks for your inquiry. Unfortunately, your issue is not resolved yet. We have asked the ETA of this issue from our product team and will update you as soon as any estimates are available. We apologize for your inconvenience.

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

@relsys,

Regarding WORDSNET-16546, we have added a new property ShapeBase.IsLayoutInCell in Aspose.Words 18.4 and you can now use the following code to place watermark at the center of Page even when the Shape is anchored to a Run inside Table.

Document doc = new Document(MyDir + @"output\output.doc");

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);
PageSetup ps = builder.PageSetup;

NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
foreach (Run run in smallRuns)
{
    if (collector.GetStartPageIndex(run) == pageIndex)
    {
        Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

        watermark.Width = 300;
        watermark.Height = 70;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

        watermark.Rotation = -40;
        watermark.Fill.Color = Color.Gray;
        watermark.StrokeColor = Color.Gray;

        watermark.TextPath.Text = "watermarkText";
        watermark.TextPath.FontFamily = "Arial";

        watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
        watermark.WrapType = WrapType.None;

        if (run.GetAncestor(NodeType.Table) != null)
        {
            doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
            watermark.IsLayoutInCell = false;
        }

        builder.MoveTo(run);
        builder.InsertNode(watermark);

        pageIndex++;
    }
}

doc.Save(MyDir + @"output\18.4.doc");

Please see the difference between 18.3.doc and 18.4.doc documents:

18.3-and-18.4-outputs.zip (36.8 KB)

However there is another unresolved issue related to your document i.e. WORDSNET-16548: /printer metrics/ Incorrect text flow causes different pagination in PDF. We will inform you via this thread as soon as this issue is resolved. We apologize for any inconvenience.