Word Processing: WinForms, WPF, Office File API – Watermarks (v21.1)

As you may already know, our word processing product line (WinForms-WPF-Office API) now ships with watermark support. You can import and export documents with watermarks as needs dictate - and yes watermarks are displayed, printed, and exported to PDF.

This new APIs allow you to apply the following to your documents:

  • insert picture and text watermarks
  • specify watermark options
  • remove existing watermarks from a document

To explore this feature in greater detail, be sure to check out our Word (RTF) Watermarks demo.

Run Demo

Create a Watermark and Specify Watermark Options

The WatermarkManager class allows you to add and remove watermarks in DOCX, DOC, and RTF documents without access to the watermark itself. You can use text or an image as a watermark.

A watermark is located in the section header. If your document does not contain headers, you cannot add a watermark. 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 code below adds an image watermark:

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("DocumentProtection.docx"); 

    // Check whether the document sections have headers:
    foreach (Section section in wordProcessor.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 = 1.5;
    wordProcessor.Document.WatermarkManager.SetImage
        (Image.FromFile("DevExpress.png"), imageWatermarkOptions);

    wordProcessor.SaveDocument("DocumentProtection_new.docx", DocumentFormat.OpenXml);
}

Remove the Watermark

Utilize the WatermarkManager.Remove method to remove watermarks from a document. The code below demonstrates how to check watermark type and remove it:

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
   wordProcessor.LoadDocument("FirstLook.docx");
   WatermarkManager watermarkManager = wordProcessor.Document.WatermarkManager;
   if (watermarkManager.Type == WatermarkType.Image)
   {
        watermarkManager.Remove();
   }
   wordProcessor.SaveDocument("FirstLook_new.docx", DocumentFormat.OpenXml);
}

Limitations

Our current watermark implementation has the following limitations:

  • You cannot edit an existing watermark. Recreate the watermark with new parameters to change it.
  • You cannot add different watermarks to document sections.
  • Our WinForms and WPF Rich Text Editors do not contain user interface elements to insert or modify watermarks.

Your Feedback Matters

As always, we welcome your thoughts and feedback. Post a comment below or submit a ticket via The DevExpress Support Center.

FREE DevExpress Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
No Comments

Please login or register to post comments.