Back to Devexpress

ShapeCollection.InsertTextWatermark(DocumentPosition, String, TextWatermarkOptions) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-shapecollection-dot-inserttextwatermark-x28-documentposition-string-textwatermarkoptions-x29.md

latest5.9 KB
Original Source

ShapeCollection.InsertTextWatermark(DocumentPosition, String, TextWatermarkOptions) Method

Inserts a text watermark into the section header.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
Shape InsertTextWatermark(
    DocumentPosition pos,
    string text,
    TextWatermarkOptions watermarkOptions
)
vb
Function InsertTextWatermark(
    pos As DocumentPosition,
    text As String,
    watermarkOptions As TextWatermarkOptions
) As Shape

Parameters

NameTypeDescription
posDocumentPosition

The position of the watermark anchor.

| | text | String |

Watermark text.

| | watermarkOptions | TextWatermarkOptions |

An object that contains text watermark options.

|

Returns

TypeDescription
Shape

The watermark inserted into the header.

|

Remarks

Watermarks are stored in shape collections (ShapeCollection) of section headers. Call the Section.BeginUpdateHeader method to access a specific section header. Use the header’s SubDocument.Shapes property to return a collection of shapes located in the header. Call the InsertTextWatermark method to add a text watermark to the header. This method displays the watermark in the center of the page.

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.

You can use properties of the Shape object to change watermark position and customize the watermark’s appearance (modify font attributes and text layout, fill and outline text).

The following example inserts a text watermark into the first document section and adds an outline to watermark text:

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

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

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

// Obtain the section's primary header.
SubDocument headerContent = firstSection.BeginUpdateHeader();
// Specify text watermark options.
TextWatermarkOptions options = new TextWatermarkOptions
{
    FontFamily = "Calibri",
    Color = Color.LightGray,
    Semitransparent = true
};
// Add a text watermark to the section.
Shape watermark = headerContent.Shapes.InsertTextWatermark(headerContent.Range.End, 
    "CONFIDENTIAL", options);
// Add an outline to watermark text.
watermark.Line.Thickness = 2;
watermark.Line.Fill.SetSolidFill(Color.Gray);
firstSection.EndUpdateHeader(headerContent);

document.SaveDocument(@"Documents\WatermarksUpd.docx", DocumentFormat.Docx);
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Drawing
' ...

document.LoadDocument("Documents\Watermarks.docx")

' Access the first document section.
Dim firstSection As Section = document.Sections(0)

' Obtain the section's primary header.
Dim headerContent As SubDocument = firstSection.BeginUpdateHeader()
' Specify text watermark options.
Dim options As New TextWatermarkOptions With {
    .FontFamily = "Calibri",
    .Color = Color.LightGray,
    .Semitransparent = True
}
' Add a text watermark to the section.
Dim watermark As Shape = headerContent.Shapes.InsertTextWatermark(headerContent.Range.End, 
    "CONFIDENTIAL", options)
' Add an outline to watermark text.
watermark.Line.Thickness = 2
watermark.Line.Fill.SetSolidFill(Color.Gray)
firstSection.EndUpdateHeader(headerContent)

document.SaveDocument("Documents\WatermarksUpd.docx", DocumentFormat.Docx)

The following image demonstrates the result:

See Also

ShapeCollection Interface

ShapeCollection Members

DevExpress.XtraRichEdit.API.Native Namespace