Regarding watermarks in aspose.words

In aspose.words is there an option to print watermarks on the document irrespective of footer like considering page coordinates as in aspose.pdf

@sandeepvarma94

Thanks for your inquiry. We will appreciate it if you please share some more details of your requirements along with your input and expected document here. We will look into these and will guide you accordingly. Meanwhile, you may check following documentation link to add watermark in Word document using Aspose.Words.

How to Add a Watermark to a Word Document

Best Regards,

@tilal.ahmad

Thanks for your reply. I want to print the watermarks like generated date on document while printing so i want to place the watermarks on document at the end of the page. In aspose.pdf it is possible by specifying coordinates but where as in aspose words i need to insert the watermark in header or footer so if already there is an footer and if i do not specify the coordinates then the generated date is overlapping with footer content in some cases and if i specify some point it is not visible in the document but where as in the aspose.pdf i can take the whole document in x and y coordinates and can place the watermark on it irrespective of the sections but in aspose.words i am unable to do that as there is only option to place watermarks in header or footer. so to over come this problem do u have any solutions. Like i want the functionality the same way as in pdf

@sandeepvarma94,

Thanks for sharing the detail. Please note that MS Word and PDF formats are quite different. However, you can insert watermark at your desired location using Aspose.Words. Following method shows how to insert watermark on each page of document. Please change the values of PageRelativeX and PageRelativeY according to your requirement.

public static void InsertWatermarkImageAtEachPage(Document doc, string watermarkImagePath)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);

    int pageIndex = 1;
    foreach (Section section in doc.Sections)
    {
        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in paragraphs)
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {
                builder.MoveToParagraph(paragraphs.IndexOf(para), 0);
                builder.StartBookmark("BM_Page" + pageIndex);
                builder.EndBookmark("BM_Page" + pageIndex);
                pageIndex++;
            }
        }
    }

    collector = new LayoutCollector(doc);
    LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

    const int PageRelativeY = 0;
    const int PageRelativeX = 0;

    foreach (Bookmark bookmark in doc.Range.Bookmarks)
    {
        if (bookmark.Name.StartsWith("BM_"))
        {
            Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;

            Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.Image);

            textbox.Top = PageRelativeY;
            textbox.Left = PageRelativeX;
            textbox.ImageData.SetImage(watermarkImagePath);



            //Set the width height according to your requirements
            textbox.Width = 100;
            textbox.Height = 50;
            textbox.BehindText = false;

            para.AppendChild(textbox);

            textbox.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
            textbox.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;

            bool isInCell = bookmark.BookmarkStart.GetAncestor(NodeType.Cell) != null;
            if (isInCell)
            {
                var renderObject = collector.GetEntity(bookmark.BookmarkStart);
                layoutEnumerator.Current = renderObject;

                layoutEnumerator.MoveParent(LayoutEntityType.Cell);
                RectangleF location = layoutEnumerator.Rectangle;

                textbox.Top = PageRelativeY - location.Y;
                textbox.Left = PageRelativeX - location.X;
            }
        }
    }
}

@tahir.manzoor

Thanks for the reply. I want the watermark in text format as it is dynamic not in the image format and i have tried the code for text removing relative position.

@sandeepvarma94

Thanks for your feedback. If you want to add text instead of image then please change ShapeType to TextPlainText as following. It will help you to accomplish the task.

....
//Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
textbox.TextPath.Text = "Confidential";

textbox.Top = PageRelativeY;
textbox.Left = PageRelativeX;
....

Best Regards,