Watermarks in Rich Text Documents
- 3 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.
Create a Watermark
A watermark is located in the section header. If the document does not contain headers, you cannot add a watermark. Refer to the following article for an example on how to create headers: Headers and Footers
If the document has multiple sections, the WatermarkManager adds the same watermark to each section and to each available header type (first, primary, odd, or even).
The WatermarkManager.Type property indicates whether the document has watermarks. The property returns WatermarkType.None if the document does not contain watermarks.
Example: Create a Text Watermark
The code sample below adds a text watermark:
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
Document document = richEditControl.Document;
foreach (Section section in document.Sections)
{
if (!section.HasHeader(HeaderFooterType.Primary))
{
//If not, create an empty header
SubDocument header = section.BeginUpdateHeader();
section.EndUpdateHeader(header);
}
}
TextWatermarkOptions textWatermarkOptions = new TextWatermarkOptions();
textWatermarkOptions.Color = Color.LightGray;
textWatermarkOptions.FontFamily = "Calibri";
textWatermarkOptions.Layout = WatermarkLayout.Horizontal;
textWatermarkOptions.Semitransparent = true;
document.WatermarkManager.SetText("CONFIDENTIAL", textWatermarkOptions);
Example: Create an Image Watermark
The code sample below adds an image watermark:
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
Document document = richEditControl.Document;
//Check whether the document sections have headers:
foreach (Section section in document.Sections)
{
if (!section.HasHeader(HeaderFooterType.Primary))
{
//If not, create an empty header
SubDocument header = section.BeginUpdateHeader();
section.EndUpdateHeader(header);
}
}
ImageWatermarkOptions imageWatermarkOptions = new ImageWatermarkOptions();
imageWatermarkOptions.Washout = false;
imageWatermarkOptions.Scale = 2;
document.WatermarkManager.SetImage(Image.FromFile("Documents//DevExpress.png"),
imageWatermarkOptions);
Remove the Watermark
The code sample below checks the watermark type and removes it:
richEditControl.LoadDocument("FirstLook.docx");
WatermarkManager watermarkManager =
richEditControl.Document.WatermarkManager;
if (watermarkManager.Type == WatermarkType.Image)
{
watermarkManager.Remove();
}
richEditControl.SaveDocument("FirstLook_new.docx", DocumentFormat.OpenXml);
Limitations
The following features are not available: