Free Support Forum - aspose.com

Adding Watermark(Image) to Header

Having problems with a particular Document. This Document has a table in the header. The first active field is one of the cells in the table that is in the header. When we insert our image into the header to be a watermark. It auto shrinks the image down to what it would be as if we were inserting into the cell so our watermark image is really small on the document. If I add a line above the table so the cursor defaults outside of the table the image shows correctly. Below is our code we are using and I have attached both our image we use and a test document with a table in the header.


Excerpt from the calling function.

'Prepare file for watermark
If bFileIsOpen = False Then
Dim fURL As String = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), waterMarkFileName)
Dim fImage As New IO.FileInfo(fURL)
Dim dImage As System.Drawing.Image = System.Drawing.Image.FromFile(fURL)
Dim fStream As New System.IO.FileStream(fURL, IO.FileMode.Open, System.IO.FileAccess.Read)

'Add background/watermark
Select Case True
Case (IO.Path.GetExtension(mFullFileName).ToLower = “.doc”) Or (IO.Path.GetExtension(mFullFileName).ToLower = “.docx”)
If fImage.Exists = True Then
removeWaterMark(mFullFileName, waterMarkType)

appObject = New Aspose.Words.Document(mFullFileName)
Dim builder As Aspose.Words.DocumentBuilder = New Aspose.Words.DocumentBuilder(appObject)

'The best place for the watermark image is in the header or footer so it is shown on every page.
InsertWatermarkIntoHeader(builder, dImage, HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(builder, dImage, HeaderFooterType.HeaderEven)
InsertWatermarkIntoHeader(builder, dImage, HeaderFooterType.HeaderPrimary)

-----------------------------------------------------------------------------------------------------------

Private Sub InsertWatermarkIntoHeader(ByVal builder As Aspose.Words.DocumentBuilder, ByVal dImage As System.Drawing.Image, ByVal headerType As HeaderFooterType)
Try
builder.MoveToHeaderFooter(headerType)
'Insert a floating picture.
Dim shape As Aspose.Words.Drawing.Shape = builder.InsertImage(dImage)
shape.Name = “mq1WaterMark1”
shape.WrapType = Aspose.Words.Drawing.WrapType.None
shape.BehindText = True
shape.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page
shape.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page
’ Calculate image left and top position so it appears in the centre of the page.
shape.Left = (builder.PageSetup.PageWidth - shape.Width) / 2
shape.Top = (builder.PageSetup.PageHeight - shape.Height) / 2
shape = Nothing
Catch ex As Exception
’ do not do anything in case of exception
End Try
End Sub