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

Free Support Forum - aspose.com

Implement non editable text watermark content in Words

Dear Team,

Please provide the functionality of text water marking with following feature.

  1. Watermark text should be in front of image and contents.
  2. Watermark text should not be editable.
  3. Watermark text should be in center and diagonally plot on page.

@praveen043

Thanks for your inquiry.

Please use the following code to get the desired output.

Document doc = new Document(MyDir + "in.docx");
InsertWatermarkAtEachPage(doc, "watermark");
doc.Save(MyDir + "19.1.docx");

public static void InsertWatermarkAtEachPage(Document doc, string watermarkText)
{
    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 watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);

            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 500;
            watermark.Height = 100;
            // 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.Gray; // Try LightGray to get more Word-style watermark
            watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

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

            watermark.Top = PageRelativeY;
            watermark.Left = PageRelativeX;
            watermark.BehindText = false;

            para.AppendChild(watermark);

            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;

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

Do you want to make the content of watermark readonly in output document? Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

This is not a JAVA code.

@praveen043

Thanks for your inquiry. Please check the following Java code example. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
InsertWatermarkTextAtEachPage(doc, "water mark");
doc.save(MyDir + "19.1.docx");

public static void InsertWatermarkTextAtEachPage (Document doc, String watermarkText) throws Exception
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);

    int pageIndex = 1;
    for (Section section : doc.getSections())
    {
        NodeCollection paragraphs = section.getBody().getChildNodes(NodeType.PARAGRAPH, true);
        for(Paragraph para : (Iterable<Paragraph>)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);
    int PageRelativeY = 0;
    int PageRelativeX = 0;

    for (Bookmark bookmark : doc.getRange().getBookmarks())
    {
        if (bookmark.getName().startsWith("BM_"))
        {
            Paragraph para = (Paragraph)bookmark.getBookmarkStart().getParentNode();

            Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

            watermark.setTop(PageRelativeY);
            watermark.setLeft(PageRelativeX);
            watermark.getTextPath().setText(watermarkText);

            watermark.getTextPath().setFontFamily("Arial");
            watermark.setWidth(500);
            watermark.setHeight(100);
            watermark.setRotation(-40);
            watermark.getFill().setColor(Color.LIGHT_GRAY); // Try LightGray to get more Word-style watermark
            watermark.setStrokeColor(Color.LIGHT_GRAY); // Try LightGray to get more Word-style watermark

            watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
            watermark.setWrapType(WrapType.NONE);
            watermark.setVerticalAlignment(VerticalAlignment.CENTER);
            watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

            para.appendChild(watermark);

            Boolean isInCell = bookmark.getBookmarkStart().getAncestor(NodeType.CELL) != null;

            if (isInCell)
            {
                watermark.setVerticalAlignment(VerticalAlignment.DEFAULT);
                watermark.setHorizontalAlignment(HorizontalAlignment.DEFAULT);

                PageSetup pageSetup = ((Section)bookmark.getBookmarkStart().getAncestor(NodeType.SECTION)).getPageSetup();
                watermark.setTop(pageSetup.getPageHeight()/2 - watermark.getHeight());
                watermark.setLeft(0);
            }
        }
    }
}