Support Center

How to add a Watermark

Currently GemBox.Document lacks an API support for Watermarks; they are supported only through preservation in a DOCX file format.

As a workaround, we recommend the following AddWatermark extension method which creates a Picture element as a BehindText image in the header to impersonate a watermark. This Picture element will start from the beginning of the page and will cover the width and height of the whole page.

C# code

public static class GemBoxDocumentHelper
    public static void AddWatermark(this Section section, string imagePath)
        var doc = section.Document;

        // Create a Picture element for watermark.
        var watermarkPicture = new Picture(doc, imagePath);

        // Set Picture’s size to full page.
        watermarkPicture.Layout =
            new FloatingLayout(
                new HorizontalPosition(HorizontalPositionType.Absolute, HorizontalPositionAnchor.Page),
                new VerticalPosition(VerticalPositionType.Absolute, VerticalPositionAnchor.Page),
                new GemBox.Document.Size(section.PageSetup.PageWidth, section.PageSetup.PageHeight))
            { WrappingStyle = TextWrappingStyle.BehindText };

        // If default header exists add Picture element to it otherwise
        // add new default header with the Picture element.
        var header = section.HeadersFooters[HeaderFooterType.HeaderDefault];
        if (header == null)
                new HeaderFooter(doc, HeaderFooterType.HeaderDefault,
                    new Paragraph(doc, watermarkPicture)));
            header.Blocks.Add(new Paragraph(doc, watermarkPicture));

        // Additionally check if first or even headers exist
        // in case they do add a copy of the Picture element.
        header = section.HeadersFooters[HeaderFooterType.HeaderFirst];
        if (header != null)
            header.Blocks.Add(new Paragraph(doc, watermarkPicture.Clone()));
        header = section.HeadersFooters[HeaderFooterType.HeaderEven];
        if (header != null)
            header.Blocks.Add(new Paragraph(doc, watermarkPicture.Clone()));

VB.NET code

Module GemBoxDocumentHelper
    Public Sub AddWatermark(section As Section, imagePath As String)
        Dim doc = section.Document

        ' Create a Picture element for watermark.
        Dim watermarkPicture = New Picture(doc, imagePath)

        ' Set Picture's size to full page.
        watermarkPicture.Layout =
            New FloatingLayout(
                New HorizontalPosition(HorizontalPositionType.Absolute, HorizontalPositionAnchor.Page),
                New VerticalPosition(VerticalPositionType.Absolute, VerticalPositionAnchor.Page),
                New GemBox.Document.Size(section.PageSetup.PageWidth, section.PageSetup.PageHeight)
            ) With {.WrappingStyle = TextWrappingStyle.BehindText}

        ' If default header exists add Picture element to it otherwise
        ' create a new default header with the Picture element.
        Dim header = section.HeadersFooters(HeaderFooterType.HeaderDefault)
        If header Is Nothing Then
                New HeaderFooter(doc, HeaderFooterType.HeaderDefault,
                    New Paragraph(doc, watermarkPicture)))
            header.Blocks.Add(New Paragraph(doc, watermarkPicture))
        End If

        ' Additionally check if first or even headers exist
        ' in case they do add a copy of the Picture element.
        header = section.HeadersFooters(HeaderFooterType.HeaderFirst)
        If header IsNot Nothing Then
            header.Blocks.Add(New Paragraph(doc, watermarkPicture.Clone()))
        End If
        header = section.HeadersFooters(HeaderFooterType.HeaderEven)
        If header IsNot Nothing Then
            header.Blocks.Add(New Paragraph(doc, watermarkPicture.Clone()))
        End If
    End Sub
End Module

And here is a sample of AddWatermark usage ("WatermarkSample.png [1024px x 1280px]"):

C# code

var document = new DocumentModel();
    new Section(document,
        new Paragraph(document, "Watermark workaround!")));



VB.NET code

Dim document = New DocumentModel()
    New Section(document,
        New Paragraph(document, "Watermark workaround!")))



The following is a resulting "Sample.docx" file:


4 of 5 people found this page helpful
Subscribe to this article to get an email notification when it is updated.

1 Comment

  • Minor change so watermark gets added to any existing header:

    public static class GemBoxDocumentHelper
    public static void AddWatermark(this Section section, string imagePath)
    var doc = section.Document;

    // Create a Picture element for watermark.
    var watermarkPicture = new Picture(doc, imagePath);

    // Set Picture’s size to full page.
    watermarkPicture.Layout = new FloatingLayout(
    new HorizontalPosition(HorizontalPositionType.Absolute, HorizontalPositionAnchor.Page),
    new VerticalPosition(VerticalPositionType.Absolute, VerticalPositionAnchor.Page),
    new GemBox.Document.Size(section.PageSetup.PageWidth, section.PageSetup.PageHeight))
    WrappingStyle = TextWrappingStyle.BehindText

    // Add watermark to headers or add a new header with the watermark.
    var gotheader = false;
    foreach (var hf in section.HeadersFooters)
    if (hf.IsHeader)
    hf.Blocks.Add(new Paragraph(doc, watermarkPicture));
    gotheader = true;
    if (!gotheader)
    new HeaderFooter(doc, HeaderFooterType.HeaderDefault,
    new Paragraph(doc, watermarkPicture)));