Adding Text Stamp Watermark to DOCX or PDF Files using C# .NET Code

@awais.hafeez…Got one more question…I think I’m using Aspose.Words.Document doc = new Aspose.Words.Document("") instead of Aspose.Pdf.Document, which makes my watermark incorrectly aligned. Could you have a look, please?

@neethuan,

Please check the following article explains the standard way of adding Text Stamp or Watermark by using Aspose.PDF for .NET API:

I am also moving your query in Aspose.PDF forum where you will be guided appropriately.

@neethuan

You may please use the code snippet given in the linked article and in case you face any issue, please feel free to let us know.

@awais.hafeez… Sorry, I should be more clear…I was saying that code here puts watermark correctly if the input is pdf, but not for docx I1.zip (2.6 MB)

@neethuan,

The following code does not display watermark at the center of 23rd page (see 20.8.zip (2.6 MB)).

Document doc = new Document("C:\\Temp\\I1\\I1.docx");

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

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

ArrayList pagesWithNoRuns = new ArrayList();
ArrayList firstRunsOnPages = new ArrayList();
for (int i = 1; i <= doc.PageCount; i++)
{
    bool flag = false;
    foreach (Run run in smallRuns)
    {
        if (collector.GetStartPageIndex(run) == i)
        {
            firstRunsOnPages.Add(run);
            flag = true;
            break;
        }
    }

    if (!flag)
        pagesWithNoRuns.Add(i);
}

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
ArrayList firstShapesOnPages = new ArrayList();
for (int i = 1; i <= doc.PageCount; i++)
{
    foreach (Shape shape in shapes)
    {
        if (collector.GetStartPageIndex(shape) == i)
        {
            firstShapesOnPages.Add(shape);
            break;
        }
    }
}

Shape watermark = CreateWatermark("Confidential", doc, false);
watermark.IsLayoutInCell = false;
foreach (Run run in firstRunsOnPages)
{
    builder.MoveTo(run);
    builder.InsertNode(watermark.Clone(true));
}

foreach (Shape shape in firstShapesOnPages)
{
    builder.MoveTo(shape);
    builder.InsertNode(watermark.Clone(true));
}

doc.UpdatePageLayout();
doc.Save("C:\\Temp\\I1\\20.8-pdf.docx");

private static Shape CreateWatermark(string watermarkText, Document doc, bool behindText = true)
{
    Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
    watermark.ZOrder = 2;

    watermark.TextPath.Text = watermarkText;
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 500;
    watermark.Height = 100;

    watermark.AllowOverlap = true;
    watermark.Rotation = -40;

    watermark.BehindText = behindText;

    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.HorizontalAlignment = HorizontalAlignment.Center;

    return watermark;
}

For the sake of any correction, we have logged this problem in our issue tracking system with ID WORDSNET-20988. We will further look into the details of this problem and will keep you updated on the status of linked issue. We apologize for your inconvenience.

1 Like

@neethuan,

We are inserting Shape before first occurrence of Run in every page of the document. The problem is first Run at the problematic page is located inside a Cell of the Table. In this case MS Word displays the Shape incorrectly. However, Aspose.Words writes all requested properties into the corresponding document.xml part as expected:

<v:shape id="_x0000_s1025" type="#_x0000_t136" style="width:500pt;height:100pt;margin-top:0;margin-left:0;mso-position-horizontal:center;mso-position-horizontal-relative:page;mso-position-vertical:center;mso-position-vertical-relative:page;position:absolute;rotation:-40;z-index:251658240" o:allowincell="f">

So, we believe WORDSNET-20988 is not a bug in Aspose.Words’ API. And to achieve the desired output, the problematic Shape should be positioned absolutely at the Page by using code similar to following:

...
Shape watermarkTemplate = CreateWatermark("Confidential", doc, false);
watermarkTemplate.IsLayoutInCell = false;
foreach (Run run in firstRunsOnPages)
{
    builder.MoveTo(run);
    Shape watermark = (Shape)watermarkTemplate.Clone(true);

    if (run.ParentParagraph.IsInCell)
    {
        PageSetup pageSetup = run.ParentParagraph.ParentSection.PageSetup;

        double contentWidth = pageSetup.PageWidth - (pageSetup.LeftMargin + pageSetup.RightMargin);
        watermark.Left = (contentWidth - watermark.Width) / 2;
        watermark.HorizontalAlignment = HorizontalAlignment.None;

        double contentHeight = pageSetup.PageHeight - (pageSetup.TopMargin + pageSetup.BottomMargin);
        watermark.Top = (contentHeight - watermark.Height) / 2;
        watermark.VerticalAlignment = VerticalAlignment.None;
    }

    builder.InsertNode(watermark);
}
...

In case if this is not an acceptable solution for you, then please provide us with your desired output. Please create your expected document by using MS Word and share it here for our reference. Please also list the complete steps that you performed in MS Word to create the expected document on your end. We will then further investigate into your issue and provide you more information.