Watermark
Watermarks are text or pictures that appear behind document content and often identify the document status, for example, by marking it as Draft.
Watermark Overview
The class representing a watermark is Watermark and exposes the following properties:
-
WatermarkType: The type of the watermark, described with theWatermarkTypeenumeration.Image: Watermark containing an image.Text: Watermark containing text.
-
ImageSettings: Determines the settings of the watermark if it is of typeImage. Derives fromWatermarkSettingsBaseand exposes a property of typeImageSourcespecifying the source of the image. -
TextSettings: Determines the settings of the watermark if it is of typeText. Derives fromWatermarkSettingsBaseand exposes additional propertiesText,FontFamily, andColorspecifying the appearance of the text.
WatermarkSettingsBase is the base class for text and image watermark settings and defines the appearance of the watermark on a page. It exposes the following properties:
Width: The width of the watermark.Height: The height of the watermark.Angle: The angle of the watermark towards the horizontal direction.
Create a Watermark
To create a watermark through the constructor of the class, pass an object of type TextWatermarkSettings or ImageWatermarkSettings as a parameter, depending on the type of watermark you want to create.
Example 1 demonstrates the creation of a text watermark.
Example 1: Create text watermark
TextWatermarkSettings settings = new TextWatermarkSettings()
{
Angle = 12,
Width = 200,
Height = 300,
Opacity = 0.4,
FontFamily = new FontFamily("Verdana"),
ForegroundColor = Colors.Red,
Text = "DRAFT"
};
Watermark textWatermark = new Watermark(settings);
Creating an image watermark is similar to creating a text one. Example 2 shows how to create an image watermark.
Example 2: Create image watermark
Watermark imageWatermark = new Watermark(new ImageWatermarkSettings()
{
Angle = 45,
Width = 50,
Height = 75,
ImageSource = new Telerik.Windows.Documents.Media.ImageSource(new FileStream("sample.jpeg", FileMode.Open), "jpeg")
});
Set Watermark
Watermarks are preserved in the header of the section to which the watermark is applied. More information on Header elements and how you can use them is available in the Headers and Footers article.
Example 3 demonstrates how you can add the watermark created in Example 1 to a RadFlowDocument by creating a Header for its first Section.
Example 3: Add watermark to header
Header header = document.Sections.First().Headers.Add(HeaderFooterType.Default);
header.Watermarks.Add(textWatermark);
By default, if a header is omitted for a
Sectionother than the first one, it is inherited from the previousSection. The watermark set in Example 3 is implicitly inherited by all sections following the first one because watermarks are preserved in the header.
You can also set a watermark in a document through the RadFlowDocumentEditor class. RadFlowDocumentEditor exposes two overloads of the SetWatermark() method that provide a simplified way to set a watermark.
Example 4 demonstrates how to set the watermark created in Example 2 through RadFlowDocumentEditor to the first page of a section. The method creates the Header element for you, and you only need to specify its type.
Example 4: Set watermark with RadFlowDocumentEditor
Section section = editor.Document.Sections.AddSection();
editor.SetWatermark(imageWatermark, section, HeaderFooterType.First);