Free Support Forum - aspose.com

Adding watermark adds blank line to header

I use the following code to apply a watermark to a word document:
Public Sub ApplyWatermark(ByVal watermarkType As String, ByVal SourceDoc As String)
Dim doc As Document = New Document(SourceDoc)
Dim watermark As New Shape(doc, ShapeType.TextPlainText)
watermark.TextPath.Text = watermarkType
watermark.TextPath.FontFamily = “Arial”
watermark.Width = 500
watermark.Height = 100
watermark.Rotation = -40
watermark.Fill.Color = System.Drawing.Color.LightGray
watermark.StrokeColor = System.Drawing.Color.LightGray
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page
watermark.WrapType = WrapType.None
watermark.VerticalAlignment = VerticalAlignment.Center
watermark.HorizontalAlignment = HorizontalAlignment.Center
Dim watermarkPara As New Paragraph(doc)
watermarkPara.AppendChild(watermark)
For Each sect As Section In doc.Sections
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven)
Next sect
doc.Save(SourceDoc)
End Sub
Private Shared Sub InsertWatermarkIntoHeader(ByVal watermarkPara As Paragraph, ByVal sect As Section, ByVal headerType As HeaderFooterType)
Dim header As HeaderFooter = sect.HeadersFooters(headerType)
If header Is Nothing Then
header = New HeaderFooter(sect.Document, headerType)
sect.HeadersFooters.Add(header)
End If
header.AppendChild(watermarkPara.Clone(True))
End Sub

The watermark is successfully applied to the document. The problem I have is that during this process I end up with an extra blank line in my headers. This causes the document to not be the same length after the watermark is added. On some documents the page count ends up more than before because of the empty lines.
I can go into the header and remove the extra line without disturbing the watermark but I have thousands of documents that I’ll be processing.

Is there any way to keep it from putting that extra line into the header?

Thanks!



Hi Jason,

Thanks for you inquiry.

This is happening because you have a paragraph in the header already and the code above from the article inserts the watermark in it's own new paragraph. To get around this you can check if the header already contains a paragraph and append the watermark to that intead of creating a new one.

Please see the code below:

Public Shared Sub ApplyWatermark(ByVal watermarkType As String, ByVal SourceDoc As String)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Dim doc As Document = New Document(SourceDoc)

Dim watermark As New Shape(doc, ShapeType.TextPlainText)

watermark.TextPath.Text = watermarkType

watermark.TextPath.FontFamily = "Arial"

watermark.Width = 500

watermark.Height = 100

watermark.Rotation = -40

watermark.Fill.Color = System.Drawing.Color.LightGray

watermark.StrokeColor = System.Drawing.Color.LightGray

watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page

watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page

watermark.WrapType = WrapType.None

watermark.VerticalAlignment = VerticalAlignment.Center

watermark.HorizontalAlignment = HorizontalAlignment.Center

For Each sect As Section In doc.Sections

InsertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HeaderPrimary)

InsertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HeaderFirst)

InsertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HeaderEven)

Next sect

doc.Save(SourceDoc)

End Sub

Private Shared Sub InsertWatermarkIntoHeader(ByVal watermark As Shape, ByVal sect As Section, ByVal headerType As HeaderFooterType)

Dim header As HeaderFooter = sect.HeadersFooters(headerType)

If header Is Nothing Then

header = New HeaderFooter(sect.Document, headerType)

sect.HeadersFooters.Add(header)

End If

' There are no paragraphs in this header, create one

If (header.GetChildNodes(NodeType.Paragraph, False).Count = 0) Then

Dim watermarkPara As Paragraph = New Paragraph(sect.Document)

header.AppendChild(watermarkPara)

End If

' Append a copy of the watermark to the last paragraph in this header

header.LastParagraph.AppendChild(watermark.Clone(True))

End Sub

Thanks,

Fantastic! That worked perfectly! Thanks so much for the rapid response.

It would be good to update your tutorial with this information:
https://docs.aspose.com/display/wordsjava/How+to++Add+a+Watermark+to+a+Document

@European_Commission,

We always appreciate positive feedback from our customers. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.