How to add Image watermark to document?

Hi to all,
I know how to add plaintext watermark to a document for there are some solutions or demos in the support forum. Unfortunately, I cannot find any demo for adding Image watermark to document based on Aspose.Words.dll v14 and VB.net 2008.
Who can help me?
Thanks!

Dim watermarkImage As Aspose.Words.Drawing.Shape = New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image)
// how to code the following steps to add image watermark to document?


Ducaisoft

@ducaisoft,

You can easily convert the following C# code (that inserts an Image as watermark) to VB.NET yourself by using some converter. For example:

Document doc = new Document("E:\\temp\\in.docx");
InsertWatermarkImage(doc, "E:\\temp\\aspose.words.jpg");
doc.Save("E:\\temp\\19.1.docx");

private static void InsertWatermarkImage(Document doc, string path)
{
    DocumentBuilder builder = new DocumentBuilder(doc);

    Shape watermark = builder.InsertImage(path);

    watermark.Rotation = -40;
    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.HorizontalAlignment = HorizontalAlignment.Center;

    Paragraph watermarkPara = new Paragraph(doc);
    watermarkPara.AppendChild(watermark);

    foreach (Section sect in doc.Sections)
    {
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
    }
}

private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];

    if (header == null)
    {
        header = new HeaderFooter(sect.Document, headerType);
        sect.HeadersFooters.Add(header);
    }

    header.AppendChild(watermarkPara.Clone(true));
}

Thank for your demo! I converted the demo, however, it doesn’t work! the image watermark hasn’t been inserted into the document! my function as follow:

Private Function AddWatermarkToDocument(ByRef doc As Aspose.Words.Document, ByVal WatermarkImg As String, ByRef Builder As Aspose.Words.DocumentBuilder,Byval dFile as string ) As Boolean

        Dim watermarkImage = Builder.InsertImage(WatermarkImg)
        watermarkImage.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page
        watermarkImage.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page
        watermarkImage.WrapType = Aspose.Words.Drawing.WrapType.None
        watermarkImage.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center
        watermarkImage.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center
        watermarkImage.Rotation = -40
        Dim WatermarkPara As Aspose.Words.Paragraph = New Aspose.Words.Paragraph(doc)
        WatermarkPara.AppendChild(watermarkImage)
        For Each Sect In doc.Sections
            InsertImgWatermarkIntoHeader(WatermarkPara, Sect, Aspose.Words.HeaderFooterType.HeaderPrimary)
            InsertImgWatermarkIntoHeader(WatermarkPara, Sect, Aspose.Words.HeaderFooterType.HeaderFirst)
            InsertImgWatermarkIntoHeader(WatermarkPara, Sect, Aspose.Words.HeaderFooterType.HeaderEven)
        Next

builder.save(dFile)
End Function

Private Function InsertImgWatermarkIntoHeader(ByVal Watermark As Aspose.Words.Paragraph, ByVal Sect As Aspose.Words.Section, ByVal HeaderType As Aspose.Words.HeaderFooterType)
    On Error Resume Next
    Dim Header = Sect.HeadersFooters(HeaderType)
    If Header Is Nothing Then
        Header = New Aspose.Words.HeaderFooter(Sect.Document, HeaderType)
        Sect.HeadersFooters.Add(Header)
    End If
    If Header.GetChildNodes(Aspose.Words.NodeType.Paragraph, False).Count = 0 Then
        Dim watermarkPara As Aspose.Words.Paragraph = New Aspose.Words.Paragraph(Sect.Document)
        Header.AppendChild(watermarkPara)
    End If
    Header.LastParagraph.AppendChild(Watermark.Clone(True))
    'Header.AppendChild(Watermark.Clone(True))
End Function

What’s wrong with the codes? why the image watermark hasn’t been inserted sucessfullly into document?

@ducaisoft,

Please ZIP and attach your input Word document and Aspose.Words generated output document showing the undesired behavior here for testing. We will then investigate the issue on our end and provide you more information.

I debug the code and find out the solution.
the correct code is as follow:
if text watermark is to be added, using:
Header.LastParagraph.AppendChild(watermark.clone(True))
if image watermark is to be added, using:
Header.AppendChild(watermark.clone(True)).

This will work.

@ducaisoft,

It is great that you were able to resolve this issue on your end. Please let us know any time you have any further queries.