Skip to main content
You are viewing help content for a version that is no longer maintained/updated.
All docs
V21.1
  • Watermarks in Rich Text Documents

    • 3 minutes to read

    A watermark is a faded background image or text displayed behind document content. The WPF 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.

    watermarks

    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:

    text watermark

    using DevExpress.XtraRichEdit.API.Native;
    using System.Drawing;
    
    Document document = richEditControl.Document;
    foreach (Section section in document.Sections)
    {
    
        // Check whether the document sections have headers:
        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:

    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:

    • User Interface elements to organize watermarks.
    • Edit an existing watermark. Recreate the watermark with new parameters to change it.
    • Add different watermarks to document sections. Insert regular shapes (TextBox or Image) in the headers of the separate sections. Refer to the following example:

      View Example: How to add a watermark with a text or image to the document