Free Support Forum - aspose.com

Add Watermark to Every Page in Word Document | Make Watermark appear on Front of Images | Set Z-Order using C# Java

I have tried this one, but unfortunately it’s not watermarking some pages that contains images. Could someone help me with this?

@neethuan,

Please ZIP and upload your input Word document and Aspose.Words generated DOCX file showing the undesired behavior here for testing. We will then investigate the issue on our end and provide you more information.

@awais.hafeez,
Thanks for the quick response.
Input document.zip (391.2 KB)
Ouput document.zip (392.4 KB)

Please find the attached documents

The snippet that I was using is

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

                    int pageIndex = smallRuns.Count > 0 ? collector.GetStartPageIndex(smallRuns[0]) : 1;
                    foreach (Run run in smallRuns)
                    {
                        if (collector.GetStartPageIndex(run) == pageIndex)
                        {
                            var watermark = CreateWatermark("Test", doc);

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

                            pageIndex++;
                        }
                    }

@awais.hafeez, I improved the code and what I am getting now is watermark only on the last page when using paragraph instead of runs.New Output.zip (1.4 MB)

When using runs, I’m getting watermark only on first page

@neethuan,

Please try using the following code:

Document doc = new Document(@"E:\\Temp\\input document\\Internal Agenda for test 0_00 AM - Saturday, 22 February 2020 (21).docx");

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    shape.WrapType = WrapType.None;
    shape.BehindText = true;
}

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

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

        watermark.Width = 300;
        watermark.Height = 70;
        watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

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

        watermark.TextPath.Text = "Test";
        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("E:\\Temp\\input document\\20.2.docx");

@awais.hafeez, What happens to me now is, It removes an image from a page and watermarks the other (Actually it overlaps one image to another )imageResult.zip (395.8 KB)

@neethuan,

Please try specifying the display order of overlapping shapes. Hope, this helps.

...
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    shape.WrapType = WrapType.None;
    shape.ZOrder = 1;
} 
...

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

@awais.hafeez, It seems like shape.WrapType = WrapType.None; , makes both of my images to get overlapped and watermark appears on top.

And smallRuns return only one (1st) page of image

I used shapes instead of runs, which watermarked all pages of mine…Thanks @awais.hafeez for your help

@neethuan,

Thanks for your feedback. It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.

1 Like