Free Support Forum - aspose.com

How to Add or Remove watermarks

Hi, to all
I have the question to how to add or remove watermarks by aspose.words.dll v14.2 based on VB.net 2008.
the codes to remove watermarks as follow:

Public Function DelWaterArt(ByVal filename As String, ByVal dFile As String,Optional byval WaterText as string ="") As Boolean
    Dim doc As New Aspose.Words.Document(filename)
     Dim Nodes As NodeCollection
    Nodes = doc.GetChildNodes(NodeType.Shape, True)
    If Nodes Is Nothing Or Nodes.Count < 1 Then
        doc = Nothing
        On Error Resume Next
        FileCopy(filename, dFile)
    Else
        Dim Shapes() = Nodes.ToArray()
        Dim count As Long = Shapes.Length
        For i As Long = 0 To count - 1
            Dim shape = Shapes(i)
            Dim t = shape.GetText()
             If t="" or t=WaterText Then
                shape.Remove()
             End If
            Application.DoEvents()
        Next
        On Error Resume Next
        doc.Save(dFile)
    End If

End Function

As shown for the above codes, all the shapes besides the true watermark shapes in the documents have been deleted! Actually, the shapes like customized shapes, WaterArt shapes, TextBoxs must not be considered as watermark and they cannot be deleted!
Any one knows how to remove true watermarks and keep the customized shapes, WaterArt shapes, TextBoxs shapes?

In addition, after clearing the watermarks, what codes can add/update new watermarks(plain text or image) to the main document besides the head/footer sections.

Thanks in advance!

Ducaisoft

@ducaisoft

Thanks for your inquiry. To remove the watermark, you need to set the name or alternative text of Shape node and use ShapeBase.AlternativeText or Shape.Name property to identify the watermark and remove it using Shape.Remove method. We suggest you please read the following article. Hope this helps you.
Working with Watermark

Thanks for your answer!
But the names or alternative texts of shapes are unknown so that the codes cannot remove the given watermarks by identifying its name or alternative text. Is there any solution for this objective?

@ducaisoft

Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information about your query.

Thanks for your reply!
Please download the demo files for your reference at http://yicai.88pe.com/downs/DemoFiles.rar.
one file is for input file, one file is for desired and correct output file, and one file is not expected and incorrect output file generated by aspose.words.dll version 14.2. based on VB.net 2008.

@ducaisoft

Thanks for sharing the detail. Please use the following code example to get the desired output. Hope this helps you.

Dim doc As Document = New Document(MyDir + "Test File for Removing watermarks.docx")
For Each section As Section In doc.Sections
    For Each headerFooter As HeaderFooter In section.HeadersFooters
        For Each shape As Shape In headerFooter.GetChildNodes(NodeType.Shape, true)
            If shape.IsWordArt Then
                shape.Remove
            End If
            
        Next
    Next
Next
doc.Save(MyDir + "18.11.docx")

It’s done! Thank you very much!