Watermark is not visible

Hello,
I’m trying to add watermark to a Word file, watermark gets added but is not visible if there is an image on the page. the watermark is behind the image. I want it to be visible all the time no matter what content the word document has.

I’ve tried setting ZOrder to 1000 but it did not work.
Also opacity is not working. it is not changing the opacity level.

Sub InsertWatermarkText(ByVal doc As Aspose.Words.Document, ByVal watermarkText As String)
’ Create a watermark shape. This will be a WordArt shape. 
’ You are free to try other shape types as watermarks.
'Dim watermark As New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText)
Dim watermark As New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image)

’ Set up the text of the watermark.
'watermark.TextPath.Text = watermarkText
watermark.ImageData.SetImage(docDir & "watermark.jpg")
watermark.TextPath.FontFamily = "Arial"
watermark.Width = 200
watermark.Height = 200
’ 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 = System.Drawing.Color.Blue ’ Try LightGray to get more Word-style watermark
watermark.StrokeColor = System.Drawing.Color.Gray ’ Try LightGray to get more Word-style watermark
watermark.AllowOverlap = True
watermark.BehindText = False
watermark.Fill.Opacity = 0.5
watermark.ZOrder = 1000
’ Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page
watermark.WrapType = WrapType.None
watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center

’ Create a new paragraph and append the watermark to this paragraph.
Dim watermarkPara As New Aspose.Words.Paragraph(doc)
watermarkPara.AppendChild(watermark)

’ Insert the watermark into all headers of each document section.
For Each sect As Section In doc.Sections
’ There could be up to three different headers in each section, since we want
’ the watermark to appear on all pages, insert into all headers.
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven)
Next sect
End Sub

thank you.

Hi Purnima,

Thanks for your inquiry. I have tested the scenario and have not found any issue with watermark. The code you are using is correct. Please make sure that the image path is correct.

watermark.ImageData.SetImage("c:\watermark.jpg")

Please let us know if you have any more queries.

Hello,

thank for your response. I think you misunderstood my problem. The watermark is getting added as expected but it is not visible if there is already an image on the page. The watermark is behind the image.

I want the watermark to be visible all the time irrespective of pdf page content.

Thank You,

Hi Purnima,

Thanks for your inquiry. It would be great if you please share
following detail for investigation purposes.

Please attach your input Word document.
Please
create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code you used to generate
your output document
Please attach the output Word/Pdf file that shows the undesired behavior.

Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hello,

I’ve attached my project.

Please look into the folder …\TestWatermark\TestWatermark\bin\Release\Data\OutputDoc\1.png

In this png file, watermark is behind the images, it is partially visible.

My requirement is to make it visible all the time as it appears in 2.png

Thank You,
Purnima

Hi Purnima,

Thanks for your inquiry. The problem occurs because watermark shape resides inside header footer story and images are inside body story (please see Story class). If you insert a watermark using Microsoft Word 2013 in source document, you will observe the same behaviour. However, you can overcome this problem by manually inserting watermarks in each Page. You can achieve this by moving the cursor to the first Paragraph in each Page of your document and then making those Paragraphs as an anchor points for your watermarks. Here is how you can achieve this:

public static void InsertWatermarkTextAtEachPage(Document doc, string watermarkText)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    PageSetup ps = builder.PageSetup;
    NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
    LayoutCollector collector = new LayoutCollector(doc);
    Paragraph anchorPara = null;
    int pageIndex = 1;
    foreach (Paragraph para in paragraphs)
    {
        if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null)
        {
            anchorPara = para;
            Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 300;
            watermark.Height = 70;
            watermark.Left = 100;
            watermark.Top = 100;
            watermark.Rotation = -40;
            watermark.Fill.Color = Color.Gray;
            watermark.StrokeColor = Color.Gray;
            watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
            watermark.WrapType = WrapType.None;
            anchorPara.AppendChild(watermark);
            pageIndex++;
        }
    }
}

I hope, this helps.

Best regards,

It worked

Thanks

Hi,

Do the same trick will work on JAVA?
i converted the code in java, after this change, the bookmark is not visible at all on any page.
Thanks in advance…

public static void InsertWatermarkTextAtEachPage(Document doc, String watermarkText) throws Exception
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    PageSetup ps = builder.getPageSetup();
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    LayoutCollector collector = new LayoutCollector(doc);
    Paragraph anchorPara = null;
    int pageIndex = 1;
    for (int i=0; i<paragraphs.getCount();i++)
    {
        Paragraph para = (Paragraph) paragraphs.get(i);
        // System.out.println(collector.getStartPageIndex(para) );
        if ( collector.getStartPageIndex(para) == pageIndex && para.getAncestor(NodeType.GROUP_SHAPE) == null)
        {

            anchorPara = para;
            Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
            watermark.getTextPath().setText(watermarkText);
            watermark.getTextPath().setFontFamily("Arial");
            watermark.setWidth(500);
            watermark.setHeight(100);
            // Text will be directed from the bottom-left to the top-right corner.
            watermark.setRotation(-40);
            // Remove the following two lines if you need a solid black text.
            watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
            watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

            // Place the watermark in the page center.
            watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
            watermark.setWrapType(WrapType.NONE);
            watermark.setVerticalAlignment(VerticalAlignment.CENTER);
            watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
            // watermark.setName(String.format("WaterMark_{0}", UUID.randomUUID().toString()));
            // watermark.setWrapType(WrapType.NONE);
            anchorPara.appendChild(watermark);
            pageIndex++;
        }
    }
}

Hi Dhawal,

Thanks for your inquiry. I have tested the scenario and have not found the shared issue. It would be great if you please share following detail for investigation purposes.

Please attach your input Word document.
Please attach the output Word file that shows the undesired behavior.
Please
attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document. I will
investigate as to how you are expecting your final document be generated
like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi,
Thanks for reply…
Actually i misunderstood the behaviour, the waterMark is getting added in DOC but when i save it to PDF format the water mark is not visible.
The Water Mark is visible if i save Document object to DOC format.

com.aspose.words.Document lWordDocument = new com.aspose.words.Document("Data/sourceLarge.doc");
InsertWatermarkTextAtEachPage(lWordDocument, "WATERMARKED");
// lWordDocument.save("Data/output/OutPut.doc"); // This file is showing watermark
lWordDocument.save("Data/output/OutPut.pdf",SaveFormat.PDF); // This file is not showing any watermarked

ALSO,
In OutPut.doc file the Water Mark is not properly inserted on page 3.

I Have Attached the Source.doc(Source File) and both output files.

Thanks …
Dhawal

Hi Dhawal,

Thanks for sharing the detail.

Please call the Document.updatePageLayout method before saving your Pdf document. This will solve the issue which you are facing.

This method is automatically invoked when you first convert a document to PDF, XPS, image or print it. However, if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout before rendering again.

Document doc = new Document(MyDir + "Source.doc");
InsertWatermarkTextAtEachPage(doc, "WaterMark");
doc.updatePageLayout();
doc.save(MyDir + "Out.pdf");

Hello,
I have used the above mentioned code but I do not see Watermark text. I only see border lines of watermark on each page of my word document.

Could you please help me out what is the problem?? I have attached the screenshot of the watermark which appeared on my document.

Looking forward to your reply.

Best regards,

Hi Jeetendra,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

Please attach your input Word document.
Please attach the output Word file that shows the undesired behavior.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi,

What if i dnt have paragraphs in that word doc.
Is it a MS word defect, that we cannot add watermark for images in a word document.
Regards,
Arun Vasa

Hi,

What if i dnt have paragraphs in that word doc.
Is it a MS word defect, that we cannot add watermark for images in a word document.

please let us know
Regards,
Arun Vasa

Hi Arun,

Thanks for your inquiry. A valid Word document must contain a Paragraph. Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

Please find the attachment.

Hi Arun,

Thanks for sharing the document. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v15.12.0, we are unable to reproduce the shared issue on our side. Please use Aspose.Words for Java 15.12.0.

We noticed that the latest version of Aspose.Words for Java does not render the watermark correctly in output Pdf. We logged this issue as WORDSJAVA-1265. Please check the attached output Pdf. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

PLease find my output file,am getting but alignment is wrong for table and image compare with text.

i use the following code.

foreach (Aspose.Words.Paragraph para in paragraphs)
{
    if (collector.GetStartPageIndex(para) == pageIndex)
    {
        anchorPara = para;
        Aspose.Words.Rendering.PageInfo docPage = doc.GetPageInfo(pageIndex - 1);

        // Insert a floating picture.
        Aspose.Words.Drawing.Shape shape = builder.InsertImage(image);
        shape.WrapType = Aspose.Words.Drawing.WrapType.None;
        //Get the document page information.
        double myWidth = docPage.WidthInPoints;
        double myHeight = docPage.HeightInPoints;
        //
        // Determine the maximum line length. This may changed based
        // on the location of the watermark.
        //
        myMaxLineLength = myWidth;

        //
        // Determine the maximum image width.
        //
        myMaxImageWidth = myMaxLineLength;
        myImageWidth = (int)myMaxImageWidth;
        myImageHeight = (int)(myImageWidth * myImageRatio);
        // Determine the location of the watermark image.
        //
        shape.Height = myImageHeight;
        shape.Width = myImageWidth;
        shape.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
        shape.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
        shape.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
        shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Left;
        // shape.Left = 
        shape.Rotation = -45;
        anchorPara.AppendChild(shape);
        pageIndex++;
    }
}

Hi Arun,

Thanks for sharing the detail. We have tested the scenario using latest version of Aspose.Words for .NET 15.12.0 and have not found the shared issue. Please use Aspose.Words for .NET 15.12.0 and following code example to get the required output. Hope this helps you.

Document doc = new Document(MyDir + "source.doc");
InsertWatermarkTextAtEachPage(doc, "This is a watermark");
ImageSaveOptions imgOptions = new ImageSaveOptions(SaveFormat.Png);
doc.UpdatePageLayout();
imgOptions.PageCount = 1;
imgOptions.Resolution = 300;
for (int i = 0; i < doc.PageCount; i++)
{
    imgOptions.PageIndex = i;
    doc.Save(MyDir + "Out" + i + ".png", imgOptions);
}
public static void InsertWatermarkTextAtEachPage(Document doc, string watermarkText)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    PageSetup ps = builder.PageSetup;
    NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
    LayoutCollector collector = new LayoutCollector(doc);
    Paragraph anchorPara = null;
    int pageIndex = 1;
    foreach (Paragraph para in paragraphs)
    {
        if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null)
        {
            anchorPara = para;
            Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
            watermark.TextPath.Text = watermarkText;
            watermark.TextPath.FontFamily = "Arial";
            watermark.Width = 400;
            watermark.Height = 100;
            watermark.Left = 100;
            watermark.Top = 100;
            watermark.Rotation = -40;
            watermark.Fill.Color = Color.Gray;
            watermark.StrokeColor = Color.Gray;
            watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
            watermark.WrapType = WrapType.None;
            anchorPara.AppendChild(watermark);
            pageIndex++;
        }
    }
}