user1169502
user1169502

Reputation: 398

MigraDoc Adding Watermarks Issue

I have created a PDF document via MigraDoc and so far all has gone well. The final stage is to add a watermark for 'drafts' for which I have found a few examples but I just can't seem to get them to work!

I create a section as normal

section = document.AddSection()
section.PageSetup.PageFormat = PageFormat.A4

...and then add the watermark using

Sub AddWatermark(section As Section)
    Dim imageFile = Server.MapPath("./images/draft.png")
    Dim myImage = section.Headers.EvenPage.AddImage(imageFile)
    myImage.Height = Unit.FromMillimeter(100)
    myImage.LockAspectRatio = True
    myImage.Top = Shapes.ShapePosition.Center
    myImage.Left = Shapes.ShapePosition.Center
    myImage.RelativeHorizontal = Shapes.RelativeHorizontal.Margin
    myImage.RelativeVertical = Shapes.RelativeVertical.Margin
    myImage.WrapFormat.Style = Shapes.WrapStyle.Through
End Sub

I then create the rest of the document which is mainly a table and output to the browser. It all works and I have stepped through in debug to ensure the image is picked up and no exceptions but the output PDF doesn't have a watermark. I've tried different images, sizes etc but nothing seems to actually add the watermark so any ideas welcome!

Upvotes: 1

Views: 896

Answers (1)

user1169502
user1169502

Reputation: 398

It seems that in my case the critical point is that when I set the image it needs to be set in 'section.Headers.Primary'. Setting in Even Page only seems to work if you have different even and odd pages and seems to be ignored otherwise. Anyone after a VB.NET solution please see below as this works for me and I hadn't found a VB solution elsewhere

Sub AddWatermark(section As Section)
		Dim imageFile = Server.MapPath("./images/draft.png")
		Dim header As HeaderFooter = section.Headers.Primary
		Dim myImage = header.AddImage(imageFile)
		myImage.Height = Unit.FromMillimeter(200)
		myImage.LockAspectRatio = True
		myImage.Top = Shapes.ShapePosition.Center
		myImage.Left = Shapes.ShapePosition.Center
		myImage.RelativeHorizontal = Shapes.RelativeHorizontal.Margin
		myImage.RelativeVertical = Shapes.RelativeVertical.Margin
		myImage.WrapFormat.Style = Shapes.WrapStyle.Through
	End Sub

Upvotes: 1

Related Questions