Free Support Forum - aspose.com

Watermark insertion in merge handler?

I followed your example to create a watermark for the document and it works great. I have a dropdown that allows the user to pick the watermark. One request I got was to make the watermark text say whatever the merge field “PropertyInfo” says. This way a property that is sold would say sold, available would say available, etc… Is this possible? I assume the watermarking procedure would need to be moved into the FieldMergingCallback? I already have a handler defined that deals with removing table rows based upon the value of the PropertyInfo field…

Private Shared Sub InsertWatermark(ByVal doc As Document, ByVal watermarkText As String)
Dim watermark As Aspose.Words.Drawing.Shape
Dim watermarkString As String
If watermarkText = “2” Then
watermarkString = “SOLD”
Else If watermarkText = “3” Then
watermarkString = “AVAILABLE”
Else If watermarkText = “4” Then
watermarkString = “LEASED”
Else If watermarkText = “5” Then
watermarkString = “SOLD”
End If
If watermarkText = “1” Then
watermark = New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image)
watermark.ImageData.SetImage(“d:\hart11\images\hartlogo.png”)
watermark.Width = 550
watermark.Height = 750
watermark.Rotation = 0
Else
watermark = New Aspose.Words.Drawing.Shape(doc, ShapeType.TextPlainText)
watermark.TextPath.Text = watermarkString
watermark.TextPath.FontFamily = “Calibri”
watermark.TextPath.Bold = True
watermark.Width = 600
watermark.Height = 200
watermark.Rotation = -40
watermark.Fill.Color = System.Drawing.Color.LavenderBlush
watermark.StrokeColor = System.Drawing.Color.LavenderBlush
End If
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page
watermark.WrapType = WrapType.None
watermark.VerticalAlignment = VerticalAlignment.Center
watermark.HorizontalAlignment = HorizontalAlignment.Center
Dim watermarkPara As New Aspose.Words.Paragraph(doc)
watermarkPara.AppendChild(watermark)
For Each sect As Aspose.Words.Section In doc.Sections
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderPrimary)
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderEven)
Next sect
End Sub

Private Shared Sub InsertWatermarkIntoHeader(ByVal watermarkPara As Aspose.Words.Paragraph, ByVal sect As Aspose.Words.Section, ByVal headerType As Aspose.Words.HeaderFooterType)
Dim header As Aspose.Words.HeaderFooter = sect.HeadersFooters(headerType)
If header Is Nothing Then
header = New Aspose.Words.HeaderFooter(sect.Document, headerType)
sect.HeadersFooters.Add(header)
End If
header.AppendChild(watermarkPara.Clone(True))
End Sub




Private Class HandleMergeFieldSummary
Implements IFieldMergingCallback

Dim mIsMergeImage As Boolean = False

Private Sub FieldMerging(args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

If args.FieldName = “SaleTypeTxt” Then
If args.FieldValue = “Hart Exclusive Agency” Then
mIsMergeImage = True
Else
mIsMergeImage = False
End If
End If

Dim builder As New DocumentBuilder(args.Document)
If args.FieldName = “P1” Then
Dim P1s As String = args.FieldValue.ToString()
If P1s > “” AND System.IO.File.Exists(P1s) Then
Else
P1s = “D:\hart11\filecabinet\property\default.gif”
End If
Dim img As System.Drawing.Image
builder.MoveToField(args.Field, True)
img = System.Drawing.Image.FromFile(P1s)
Dim shape As Aspose.Words.Drawing.Shape = builder.InsertImage(img, 275, 125)
shape.WrapType = “5”
End If


If args.FieldName.Equals(“PropertyInfo”) Then
Dim bk1 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkTransactionInformation”)
Dim row1 As Aspose.Words.Tables.Row = CType(bk1.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk2 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkListingOffMarketDate”)
Dim row2 As Aspose.Words.Tables.Row = CType(bk2.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk3 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkTransMOM”)
Dim row3 As Aspose.Words.Tables.Row = CType(bk3.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk4 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkTransactionInfo”)
Dim row4 As Aspose.Words.Tables.Row = CType(bk4.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk5 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkSaleActualPriceInt”)
Dim row5 As Aspose.Words.Tables.Row = CType(bk5.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk6 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkPricePerSFCalc”)
Dim row6 As Aspose.Words.Tables.Row = CType(bk6.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk7 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkLeaseActualRateDec”)
Dim row7 As Aspose.Words.Tables.Row = CType(bk7.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk8 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkSellerInfo”)
Dim row8 As Aspose.Words.Tables.Row = CType(bk8.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)
Dim bk9 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkBuyerInfo”)
Dim row9 As Aspose.Words.Tables.Row = CType(bk9.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)

If args.FieldValue LIKE “Available*” Then
If row1 IsNot Nothing Then
row1.Remove()
End If
If row2 IsNot Nothing Then
row2.Remove()
End If
If row3 IsNot Nothing Then
row3.Remove()
End If
If row4 IsNot Nothing Then
row4.Remove()
End If
If row5 IsNot Nothing Then
row5.Remove()
End If
If row6 IsNot Nothing Then
row6.Remove()
End If
If row7 IsNot Nothing Then
row7.Remove()
End If
If row8 IsNot Nothing Then
row8.Remove()
End If
If row9 IsNot Nothing Then
row9.Remove()
End If
Else
row1.Range.Bookmarks(0).Remove()
row2.Range.Bookmarks(0).Remove()
row3.Range.Bookmarks(0).Remove()
row4.Range.Bookmarks(0).Remove()
row5.Range.Bookmarks(0).Remove()
row6.Range.Bookmarks(0).Remove()
row7.Range.Bookmarks(0).Remove()
row8.Range.Bookmarks(0).Remove()
row9.Range.Bookmarks(0).Remove()
End If
End If

If args.FieldName.Equals(“urlTitle”) Then
Dim bk3 As Aspose.Words.Bookmark = args.Document.Range.Bookmarks(“bkurl”)
Dim row As Aspose.Words.Tables.Row = CType(bk3.BookmarkStart.GetAncestor(NodeType.Row), Aspose.Words.Tables.Row)

If args.FieldValue = “No Map” Then
If row IsNot Nothing Then
row.Remove()
End If
Else
row.Range.Bookmarks(0).Remove()
End If
End If


End Sub

Private Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging
If (mIsMergeImage) Then
e.ImageFileName = System.IO.Path.Combine(“d:\hart11\images”, “hartlogoxsmall.gif”)
End If
End Sub
End Class

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Yes, you can use FieldMergingCallback to achieve this. For instance, see the following code:

_

Public Sub Test001()

Dim doc As New Document("C:\Temp\in.doc")

doc.MailMerge.FieldMergingCallback = New InsertWatermarkCallback()

doc.MailMerge.Execute(New String() {"WatermarkText"}, New Object() {"I am watermak"})

doc.Save("C:\Temp\out.doc")

End Sub

Private Class InsertWatermarkCallback

Implements IFieldMergingCallback

Dim mIsMergeImage As Boolean = False

Private Sub FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

If args.FieldName = "WatermarkText" And InsertWatermarkAllowed Then

InsertWatermarkText(args.Document, args.FieldValue.ToString())

' We need to insert watermakr only once.

InsertWatermarkAllowed = False

' If we do not need mergefield. we ca set its value to empty string.

args.Text = ""

End If

End Sub

Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

' Do nothing.

End Sub

'''

''' Inserts a watermark into a document.

'''

''' The input document.

''' Text of the watermark.

Private Shared Sub InsertWatermarkText(ByVal doc As 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 Shape(doc, ShapeType.TextPlainText)

' Set up the text of the watermark.

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

' Create a new paragraph and append the watermark to this paragraph.

Dim watermarkPara As New 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

Private Shared Sub InsertWatermarkIntoHeader(ByVal watermarkPara As Aspose.Words.Paragraph, ByVal sect As Aspose.Words.Section, ByVal headerType As Aspose.Words.HeaderFooterType)

Dim header As Aspose.Words.HeaderFooter = sect.HeadersFooters(headerType)

If header Is Nothing Then

header = New Aspose.Words.HeaderFooter(sect.Document, headerType)

sect.HeadersFooters.Add(header)

End If

header.AppendChild(watermarkPara.Clone(True))

End Sub

Dim InsertWatermarkAllowed As Boolean = True

End Class

Best regards,