Free Support Forum - aspose.com

Aspose.Words Watermarks- Backgrounds

Hello,

I wish my document created by aspose to have a backgound watermark in various formats(horizontal/vertical/corner to corner). How can I do this? If watermarks are not supported is there any way I can use a background image TIF to show the background on each document created by Aspose.Words.

Any sample appreciated.

Thanks,

Vinay Hattarki

Hi

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

Thanks for your inquiry. Please see the attached document to learn how to insert watermarks into the Word documents using Aspose.Words.

Hope this helps.

Best regards.

I used the code provided and modified it as below but it seems its not getting anything on the document as watermark. Actually I am saving my words object as .DOC first and then converting it to SaveAsImage TIF. I wish to get this watermark carried on both DOC file and TIF image.

Private Sub AsposeWatermark(ByVal doc As Document, ByVal watermarkText As String)

'Private Sub InsertWatermarkText(ByVal doc As Document, ByVal watermarkText As String)

'Create the whatermark shape

'This will be a WordArt shape. You are free to use any type of shape as a watermark

Dim watermark As Aspose.Words.Drawing.Shape = New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText)

'This will be text of the watermark

watermark.TextPath.Text = watermarkText

watermark.TextPath.FontFamily = "Times New Roman"

watermark.Width = 600

watermark.Height = 100

'Text dirrection will be from bottom-left corner to top-right

watermark.Rotation = -45

'Uncomment the following line if you need solid black text

watermark.Fill.Color = Color.Gray

watermark.StrokeColor = Color.Gray

'Set position of the watermark

'The watermark should be placed on the center of the page

watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page

watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page

watermark.WrapType = Aspose.Words.Drawing.WrapType.None

watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center

watermark.HorizontalAlignment = HorizontalAlignment.Center

'Create new paragraph and append the watermark to this paragraph

Dim watermarkPar As Paragraph = New Paragraph(doc)

watermarkPar.AppendChild(watermark)

Dim hfTypes As ArrayList = New ArrayList()

hfTypes.Add(HeaderFooterType.HeaderPrimary)

hfTypes.Add(HeaderFooterType.HeaderFirst)

hfTypes.Add(HeaderFooterType.HeaderEven)

'Now we should insert the watermark into the header of each section of the document

For Each sect As Section In doc.Sections

For Each hftype As HeaderFooterType In hfTypes

If (sect.HeadersFooters(hftype) Is Nothing) Then

'If there is no header of the specified type in the current section we should create new header

If (hftype.Equals(HeaderFooterType.HeaderPrimary) Or _

hftype.Equals(HeaderFooterType.HeaderFirst) And sect.PageSetup.DifferentFirstPageHeaderFooter Or _

hftype.Equals(HeaderFooterType.HeaderEven) And sect.PageSetup.OddAndEvenPagesHeaderFooter) Then

Dim hf As HeaderFooter = New HeaderFooter(doc, hftype)

'Insert clone of watermarkPar into the header

hf.AppendChild(watermarkPar.Clone(True))

sect.HeadersFooters.Add(hf)

End If

Else

'If the header of specified type exists then just insert the watermark

sect.HeadersFooters(hftype).AppendChild(watermarkPar.Clone(True))

End If

Next

Next

End Sub

Hi

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

Thanks for your request. This is the known issue #7114 in our defect database. As a workaround you can insert watermark as an image. See the following code:

Private Sub AsposeWatermarkImage(ByVal doc As Document, ByVal imagePath As String)

'Create the whatermark shape

'You are free to use any type of shape as a watermark

Dim watermark As Aspose.Words.Drawing.Shape = New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image)

'This will be text of the watermark

watermark.ImageData.SetImage(imagePath)

watermark.Width = 200

watermark.Height = 200

'Set position of the watermark

'The watermark should be placed on the center of the page

watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page

watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page

watermark.WrapType = Aspose.Words.Drawing.WrapType.None

watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center

watermark.HorizontalAlignment = HorizontalAlignment.Center

'Create new paragraph and append the watermark to this paragraph

Dim watermarkPar As Paragraph = New Paragraph(doc)

watermarkPar.AppendChild(watermark)

Dim hfTypes As ArrayList = New ArrayList()

hfTypes.Add(HeaderFooterType.HeaderPrimary)

hfTypes.Add(HeaderFooterType.HeaderFirst)

hfTypes.Add(HeaderFooterType.HeaderEven)

'Now we should insert the watermark into the header of each section of the document

For Each sect As Section In doc.Sections

For Each hftype As HeaderFooterType In hfTypes

If (sect.HeadersFooters(hftype) Is Nothing) Then

'If there is no header of the specified type in the current section we should create new header

If (hftype.Equals(HeaderFooterType.HeaderPrimary) Or hftype.Equals(HeaderFooterType.HeaderFirst) And sect.PageSetup.DifferentFirstPageHeaderFooter Or hftype.Equals(HeaderFooterType.HeaderEven) And sect.PageSetup.OddAndEvenPagesHeaderFooter) Then

Dim hf As HeaderFooter = New HeaderFooter(doc, hftype)

'Insert clone of watermarkPar into the header

hf.AppendChild(watermarkPar.Clone(True))

sect.HeadersFooters.Add(hf)

End If

Else

'If the header of specified type exists then just insert the watermark

sect.HeadersFooters(hftype).AppendChild(watermarkPar.Clone(True))

End If

Next

Next

End Sub

Also note, rotation of the image will be lost during rendering. This is the known issue #7469.

Best regards.

The issues you have found earlier (filed as 7469) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as 7114) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(16)