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

Free Support Forum - aspose.com

Displaying transparent watermark on top of document content

We are using the following code to generate a watermark in our document. Unfortunately, the watermark is displayed in back of the other content and as a result looks odd (WatermarkInBack.png (59.9 KB)).

    private void InsertWaterMarkText(string waterMarkText)
    {
        var watermark = new Shape(Document, ShapeType.TextPlainText) { Name = "WaterMark" };
        watermark.TextPath.Text = waterMarkText;
        watermark.TextPath.FontFamily = "Arial";
        watermark.Width = 500;
        watermark.Height = 100;
        watermark.ZOrder = 100000;
        // 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.Red;
        watermark.Fill.Opacity = .10;
        watermark.StrokeColor = Color.Red;
        watermark.Fill.Opacity = .20;

        // 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;

        // Create a new paragraph and append the watermark to this paragraph.
        var watermarkParagraph = new Paragraph(Document);
        watermarkParagraph.AppendChild(watermark);

        // Insert the watermark into all headers of each document section.
        foreach (var node in Document.Sections)
        {
            var section = (Section)node;
            // 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(watermarkParagraph, section, HeaderFooterType.HeaderPrimary);
            InsertWatermarkIntoHeader(watermarkParagraph, section, HeaderFooterType.HeaderFirst);
            InsertWatermarkIntoHeader(watermarkParagraph, section, HeaderFooterType.HeaderEven);
        }
    }

    private void InsertWatermarkIntoHeader(Paragraph watermarkParagraph, Section section, HeaderFooterType headerType)
    {
        var header = section.HeadersFooters[headerType];

        if (header == null)
        {
            header = new HeaderFooter(section.Document, headerType);
            section.HeadersFooters.Add(header);
        }

        header.AppendChild(watermarkParagraph.Clone(true));
    }

@ajivener,

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 2019, 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 Run in each Page of your document and then making those Runs as an anchor points for your watermarks. Please see the following code for example:

Document doc = new Document(MyDir + @"input.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;
}

Hope, this helps.

This addressed our issue and our watermark now displays on top of all the other objects on the page.

Thanks!

1 Like