Skip to main content
A newer version of this page is available. .
All docs
V21.2

Watermarks in Rich Text Documents

  • 11 minutes to read

A watermark is a faded background image or text displayed behind document content. The WinForms Rich Text Editor allows you to create and remove watermarks in DOCX, DOC, and RTF documents. You can also print and export documents with watermarks to PDF.

Add a Watermark to All Document Pages

The WinForms Rich Text Editor ships with the WatermarkManager interface that allows you to insert and remove text and image watermarks. Use the Document.WatermarkManager property to access the WatermarkManager object.

Watermarks are located in section headers. If a document does not contain headers, WatermarkManager adds headers of the Primary type to all document sections.

The WatermarkManager.Type property indicates whether a document has watermarks. This property returns None if the document does not contain watermarks.

Create a Text Watermark

Call one of the WatermarkManager.SetText method overloads to add a text watermark to the document. Pass a TextWatermarkOptions class instance to this method to specify text watermark options.

The code sample below adds a text watermark to all document pages.

Text Watermark

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Define text watermark options.
TextWatermarkOptions textWatermarkOptions = new TextWatermarkOptions();
textWatermarkOptions.Color = System.Drawing.Color.LightGray;
textWatermarkOptions.FontFamily = "Calibri";
textWatermarkOptions.Layout = WatermarkLayout.Diagonal;
textWatermarkOptions.Semitransparent = true;

// Add a text watermark to the document.
document.WatermarkManager.SetText("CONFIDENTIAL", textWatermarkOptions);

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Create an Image Watermark

Call one of the WatermarkManager.SetImage method overloads to add an image watermark to the document. Pass an ImageWatermarkOptions class instance to this method to specify image watermark options.

The code sample below adds an image watermark to all document pages.

Image Watermark

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Define image watermark options.
ImageWatermarkOptions imageWatermarkOptions = new ImageWatermarkOptions();
imageWatermarkOptions.Washout = false;
imageWatermarkOptions.Scale = 2.5;

// Add an image watermark to the document.
document.WatermarkManager.SetImage(Image.FromFile(@"Images\DevExpressLogo.png"), imageWatermarkOptions);

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Add a Watermark to an Individual Document Section

The WinForms Rich Text Editor allows you to insert watermarks into headers of individual sections.

Note

When you add a watermark to a section header, ensure that the Section.IsHeaderLinkedToPrevious and Section.IsHeaderLinkedToNext methods return false for this header. Otherwise, the header has the same content as the header of the previous or next section, respectively.

The WinForms Rich Text Editor offers two ways to add watermarks to document sections.

Method 1. Use WatermarkManager to Insert Watermarks

Use the WatermarkManager.SetText and WatermarkManager.SetImage method overloads with the section and headerType parameters to add text and image watermarks to headers of specific document sections. If a section you pass to these methods does not have a header of the specified type, WatermarkManager creates this header. Consider the following when you create section headers:

The code sample below adds different watermarks to document sections. The SetImage method creates the first page header for the first section and adds an image watermark to the header. The SetText method creates a primary header (the same header for all pages) for the second section and inserts a text watermark into this header.

Image and Text Watermarks

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

var firstSection = document.Sections[0];
var secondSection = document.Sections[1];

// Define image watermark options.
var imageWatermarkOptions = new ImageWatermarkOptions
{ Washout = false };

// Add an image watermark to the first page header. 
firstSection.DifferentFirstPage = true;
document.WatermarkManager.SetImage(firstSection, HeaderFooterType.First, 
    Image.FromFile(@"Images\DevExpressLogo.png"), imageWatermarkOptions);

// Add a text watermark to the second section's header.
document.WatermarkManager.SetText(secondSection, HeaderFooterType.Primary, "DRAFT");

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Method 2. Use ShapeCollection to Insert Watermarks

Watermarks are stored in shape collections (ShapeCollection) of section headers. Call one of the Section.BeginUpdateHeader method overloads to access a header of the section in which you want to insert a watermark. Use the header’s SubDocument.Shapes property to return a collection of shapes located in this header. Use one of the following methods to add a text or image watermark to the header: ShapeCollection.InsertTextWatermark or ShapeCollection.InsertImageWatermark.

After a watermark is inserted, you can use properties of the Shape object to change watermark position and size, rotate the watermark, and change its appearance.

The example below adds an image watermark to the first document section, customizes the watermark’s appearance, and rotates the watermark counterclockwise by 45 degrees.

Image Watermark

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Access the first document section.
Section firstSection = document.Sections[0];

// Obtain the section's primary header.
var headerContent = firstSection.BeginUpdateHeader();
// Add an image watermark to the section.
Shape watermark = headerContent.Shapes.InsertImageWatermark(headerContent.Range.End,
    Image.FromFile(@"Images\DevExpressLogo.png"));
// Remove the washout effect.
watermark.WatermarkFormat.ImageOptions.Washout = false;
// Add a border to the watermark image.
watermark.Line.Thickness = 3;
watermark.Line.Fill.SetSolidFill(Color.Gray);
// Rotate the watermark.
watermark.RotationAngle = -45;
firstSection.EndUpdateHeader(headerContent);

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Modify a Watermark

Call one of the Section.BeginUpdateHeader method overloads to access a section header that contains a watermark you want to modify. Use the header’s SubDocument.Shapes property to return a collection of shapes located in this header. Use the Shape.Type property to find a watermark in the collection. This property returns the Watermark value if a shape is a watermark.

The Shape.WatermarkFormat property allows you to obtain and modify watermark settings. Refer to the sections below for details on available options. You can use other properties of the Shape object to change watermark position, resize the watermark, and customize its appearance (add a border to a watermark image, fill and outline watermark text).

Change Text Watermark Settings

The WatermarkFormat.TextOptions property allows you to access and modify a watermark’s text settings (font attributes, text color, and layout).

The following code iterates through all document sections and updates settings for text watermarks in primary headers. The WatermarkFormat.Type property is used to check the watermark type.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
using System.Linq;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Add a text watermark to the document.
document.WatermarkManager.SetText("CONFIDENTIAL");

foreach (Section section in document.Sections)
{
    // Obtain the section's primary header.
    var headerContent = section.BeginUpdateHeader();
    // Retrieve the header's watermark.
    Shape watermark = headerContent.Shapes.SingleOrDefault(x => x.Type == ShapeType.Watermark);
    // Change text watermark options.
    if (watermark != null && watermark.WatermarkFormat.Type == WatermarkType.Text)
    {
        watermark.WatermarkFormat.TextOptions.Color = Color.SlateGray;
        watermark.WatermarkFormat.TextOptions.FontFamily = "Segoe UI";
        watermark.WatermarkFormat.TextOptions.Layout = WatermarkLayout.Horizontal;
        watermark.WatermarkFormat.TextOptions.Semitransparent = true;
    }
    section.EndUpdateHeader(headerContent);
}
document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

The following image demonstrates the result:

Text Watermarks

Replace Watermark Text

Use the WatermarkFormat.SetText method to assign new text to a watermark.

The code sample below adds a text watermark to all document sections and changes watermark text in the primary header of the first section.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Linq;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Add a text watermark to the document.
document.WatermarkManager.SetText("DO NOT COPY");

// Access the first document section.
var firstSection = document.Sections[0];

// Obtain the section's primary header.
var headerContent = firstSection.BeginUpdateHeader();
// Retrieve the header's watermark.
var watermark = headerContent.Shapes.SingleOrDefault(x => x.Type == ShapeType.Watermark);
// Change watermark text.
if (watermark != null && watermark.WatermarkFormat.Type == WatermarkType.Text)
{
    watermark.WatermarkFormat.SetText("CONFIDENTIAL");
}
firstSection.EndUpdateHeader(headerContent);

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Change Image Watermark Settings

Use the WatermarkFormat.ImageOptions property to access and modify image watermark settings. You can change the image scale percentage (Scale) and apply or remove the washout effect (Washout).

The following code iterates through all document sections and updates settings for image watermarks in primary headers. The WatermarkFormat.Type property is used to check the watermark type.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
using System.Linq;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Add an image watermark to the document.
document.WatermarkManager.SetImage(Image.FromFile(@"Images\DevExpressLogo.png"));

foreach (Section section in document.Sections)
{
    // Obtain the section's primary header.
    var headerContent = section.BeginUpdateHeader();
    // Retrieve the header's watermark.
    Shape watermark = headerContent.Shapes.SingleOrDefault(x => x.Type == ShapeType.Watermark);
    // Change watermark image options.
    if (watermark != null && watermark.WatermarkFormat.Type == WatermarkType.Image)
    {
        watermark.WatermarkFormat.ImageOptions.Washout = false;
        watermark.WatermarkFormat.ImageOptions.Scale = 3;
        watermark.RotationAngle = -45;
    }
    section.EndUpdateHeader(headerContent);
}
document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

The following image demonstrates the result:

Image Watermarks

Replace a Watermark Image

The WatermarkFormat.SetImage method allows you to replace an existing watermark image with another image.

The following code adds an image watermark to all document sections and changes the image for the watermark located in the primary header of the first section:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
using System.Linq;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

// Add an image watermark to the document.
document.WatermarkManager.SetImage(Image.FromFile(@"Images\DevExpressLogo.png"));

// Access the first document section.
Section firstSection = document.Sections[0];

// Obtain the section's primary header.
var headerContent = firstSection.BeginUpdateHeader();
// Retrieve the header's watermark.
var watermark = headerContent.Shapes.SingleOrDefault(x => x.Type == ShapeType.Watermark);
// Change the watermark image.
if (watermark != null && watermark.WatermarkFormat.Type == WatermarkType.Image)
{
    watermark.WatermarkFormat.SetImage(Image.FromFile(@"Images\DevAVLogo.png"));
}
firstSection.EndUpdateHeader(headerContent);

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Remove a Watermark

You can remove watermarks from a document in one of the following ways:

  • Use the WatermarkManager.Remove method overloads. Call this method without parameters to remove all watermarks from the document. Pass a document section and the type of the section header to the method to remove a watermark from an individual section.

  • Call the ShapeCollection.Remove and ShapeCollection.RemoveAt methods to remove a watermark from a shape collection of a specific section header. Use the Shape.Type property to determine whether an object in the collection is a watermark.

The following example removes all watermarks from the document:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
// ...

Document document = richEditControl1.Document;
document.LoadDocument(@"Documents\Watermarks.docx");

document.WatermarkManager.Remove();

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.OpenXml);

Limitations

The WinForms Rich Text Editor does not contain user interface elements to insert watermarks.

See Also