Inserting a watermark image in a word document

Hi, in another post () I’ve found following code to insert a watermark image in a word document:

Document doc = new Document(MyDir + "Test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Shape watermark = new Shape(doc, ShapeType.Image);
watermark.ImageData.SetImage(MyDir + "school.JPG");
// setting image width and height
watermark.Width = 500;
watermark.Height = 100;
// Image will be directed from the bottom-left to the top-right corner.
watermark.Rotation = -40;
// Image will be placed center of page
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Top;
watermark.HorizontalAlignment = HorizontalAlignment.Left;
watermark.BehindText = false;
builder.InsertNode(watermark);
builder.Document.Save(MyDir + "test Out.docx");

It is a good sample for my needs, but I have some residual questions:

  1. How can I set a margin? For example, from the top of the page in the above example?
  2. How can I put the watermark on a desired page (the code above inserts the image on first page)?
  3. Can I set the opacity for the image?

Regards!

Hi Alessandro,

Thanks for your inquiry. Please refer to the complete article below which outlines how to Add a Watermark to a Document:
https://docs.aspose.com/words/net/working-with-watermark/

Secondly, you can use standard .NET classes to be able to change opacity of inserted Shape (see Shape.ImageData Property). Please see following article for an example:
https://www.codeproject.com/tips/201129/change-opacity-of-image-in-c

Best regards,

Thank you.
I’ve already seen your article
https://docs.aspose.com/words/net/working-with-watermark/
but I haven’t found a way to set the margin for the inserted image…

(OK for the opacity, thank you.)

Hi Alessandro,

Thanks for your inquiry. You can use Shape.Left and Shape.Top properties to set the position of the left edge/top edge of the containing block of the shape. If you want to set left/top/right/bottom margins of page, please use PageSetup.LeftMargin, PageSetup.TopMargin, PageSetup.RightMargin and PageSetup.BottomMargin properties. I hope, this helps.

Best regards,

Hi Awais, Thank you…
I’ve tried to use Shaper.Left and Top but without effect…

This is my complete code (VB) and the output file is attached:

Dim doc As New Document(MyDir & "WatermarkSampleDoc.docx")
Dim builder As New DocumentBuilder(doc)
Dim watermark As New Shape(doc, ShapeType.Image)
watermark.ImageData.SetImage(MyDir & "Barcode.tif")

' setting image width and height
watermark.Width = 300
watermark.Height = 100
' setting left and top of the shape
watermark.Left = 100
watermark.Left = 20
' Image will be placed center of page
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page
watermark.WrapType = WrapType.None
watermark.VerticalAlignment = VerticalAlignment.Top
watermark.HorizontalAlignment = HorizontalAlignment.Left
watermark.BehindText = False
builder.InsertNode(watermark)
builder.Document.Save(MyDir & "Output.docx")

Hi Alessandro,

Thanks for the additional information. You can work this around by simply commenting the following two lines of code:

'watermark.VerticalAlignment = VerticalAlignment.Top
'watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Left

I hope, this helps.

Best regards,

Thank you very much!
It works correctly!

But, the last of my needs: can I put the watermark in a specified page?
The above code puts the watermark into the first page

Thank you in advance!

Hi Alessandro,

Thanks for your inquiry. Sure, you can achieve this using Aspose.Words. First you need to find out a Paragraph on that particular page using the classes available in Aspose.Words.Layout Namespace and then anchor the watermark shape to that Paragraph. I hope, this helps.

Best regards,

Thank you again.
Regards