Monday, May 2, 2011

Watermark in a word document

I resently had to implement watermarking of a document in word. One important thing to remember is that a watermark is actually placed in your header or footer section. It might be displayed across the document but it is actually in either the header or footer.

Here is what I did.

I added an AddWatermark function that looks like this:


Private Function AddWatermark(ByVal WatermarkText As String, ByVal iSeed As Integer) As Boolean

    AddWatermark = True

    Try
        Dim wmShape As Word.Shape
        Dim oSection As Word.Section
 
        For Each oSection In m_oDoc.Sections
            For Each CurrentHeader As Word.HeaderFooter In oSection.Headers
                'Select the current header.
                CurrentHeader.Range.Select()

                'Create the watermark shape
                wmShape = CurrentHeader.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, WatermarkText, "Times New Roman", 1, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, CurrentHeader.Range)

                'Set all of the attributes of the watermark
                With wmShape
                    .Select()
                    .Name = "ExformaticsWaterMarkObject_" & iSeed.ToString()
                    .TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
                    .Fill.Solid()
                    .Fill.ForeColor.RGB = Word.WdColor.wdColorGray25
                    .Fill.Transparency = 0.4 'Make the watermark transparent
                    .Rotation = 315 'Tilt of the watermark
                    .LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue
                    .Height = m_oUtilites.WordApp.InchesToPoints(2.4)
                    .Width = m_oUtilites.WordApp.InchesToPoints(4.8)
                    .WrapFormat.AllowOverlap = CType(True, Integer)
                    .WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth
                    .WrapFormat.Type = Word.WdWrapType.wdWrapNone
                    .RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
                    .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
                    .Left = Word.WdShapePosition.wdShapeCenter
                    .Top = Word.WdShapePosition.wdShapeCenter
                End With
 
                iSeed = iSeed + 100
            Next
        Next
    Catch ex As Exception
        AddWatermark = False
    End Try
End Function

To remove the watermark I added the folowing method:

Private Sub DeleteWatermark(ByVal iSeed As Integer)

    Dim oSection As Word.Section

    For Each oSection In m_oDoc.Sections
        For Each CurrentHeader As Word.HeaderFooter In oSection.Headers
            CurrentHeader.Shapes("ExformaticsWaterMarkObject_" & iSeed.ToString()).Delete()
            iSeed = iSeed + 100
        Next
    Next
End Sub

No comments:

Post a Comment