Free Support Forum - aspose.com

Overlay Watermark Shapes on Word Document Level Images in All Pages using C# .NET

When adding a document level watermark using the code below, the watermark overlays text but does not overlay images:

var options = new TextWatermarkOptions
{
    FontFamily = "Arial",
    FontSize = 36,
    Color = Color.Red,
    Layout = WatermarkLayout.Diagonal,
    IsSemitrasparent = true
};
doc.Watermark.SetText("Watermark Test", options);

I have seen older forum topics suggestions for manually adding a shape instead of using the watermark funtionality but this causes tables to loose definition. As the watermark is being added at the document level I would have thought this would work ok.

Thanks

@pjb_maintology,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word document
  • Aspose.Words generated output document showing the undesired behavior
  • Your expected Word file showing the desired output. You can create this file manually by using MS Word. It would be great if you please also list the complete steps that you performed in MS Word to create the expected file
  • Please also create a standalone simplified Console Application (source code without compilation errors) that helps us to reproduce this problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will then start investigation into your particular issue and provide you more information.

Hi,
As requested the attached zip contians a console application reproducing the issue plus expected and actual word documents. The source document is HTML rather then Word.

AsposeWatermarkIssue.zip (641.8 KB)

Thanks

@pjb_maintology,

Please use the following code to get the desired output:

var doc = new Document();
var builder = new DocumentBuilder(doc);

builder.InsertHtml(File.ReadAllText("C:\\Temp\\AsposeWatermarkIssue\\Input.html", Encoding.UTF8));

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

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.IsLayoutInCell = false;

        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

        watermark.Width = 8.89 * 72;
        watermark.Height = 0.56 * 72;
        watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

        watermark.Rotation = -45;
        watermark.Fill.Color = Color.Red;
        watermark.StrokeColor = Color.Red;

        watermark.TextPath.Text = "Watermark Text Should Overlay Images";
        watermark.TextPath.FontFamily = "Arial";
        watermark.TextPath.Size = 36;


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

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

        pageIndex++;
    }
}

doc.Save("C:\\Temp\\AsposeWatermarkIssue\\21.6.docx");

Thanks Awais, that works well.

1 Like