Skip to main content
A newer version of this page is available.
All docs
V18.2

How to: Specify Document Properties

  • 3 minutes to read

This example demonstrates how to append document properties in code.

Specify Standard Document Properties

Use the Document.DocumentProperties property to specify standard document properties, as illustrated below.

This code snippet demonstrates how to set standard document properties with the Document.DocumentProperties property and show them in a document using specific fields.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

document.BeginUpdate();

document.DocumentProperties.Creator = "John Doe";
document.DocumentProperties.Title = "Inserting Custom Properties";
document.DocumentProperties.Category = "TestDoc";
document.DocumentProperties.Description = "This code demonstrates API to modify and display standard document properties.";

document.Fields.Create(document.AppendText("\nAUTHOR: ").End, "AUTHOR");
document.Fields.Create(document.AppendText("\nTITLE: ").End, "TITLE");
document.Fields.Create(document.AppendText("\nCOMMENTS: ").End, "COMMENTS");
document.Fields.Create(document.AppendText("\nCREATEDATE: ").End, "CREATEDATE");
document.Fields.Create(document.AppendText("\nCategory: ").End, "DOCPROPERTY Category");
document.Fields.Update();
document.EndUpdate();

Create Custom Document Properties

To create a custom document property, a new item should be added to the corresponding collection, accessible through the Document.CustomProperties property. This can be done in two ways:

  • Using the DocumentCustomProperties.Item property. It sets a value to the property with the name specified in square brackets. If the property with such name doesn’t exist, it will be created automatically.

The following code illustrates how to create custom document properties and place them in the document’s storage. The DOCVARIABLE field is used to display property values in the document. The DocumentPropertyDisplayHelper is a custom class that helps to obtain a property value.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

    document.BeginUpdate();
    document.AppendText("A new value of MyBookmarkProperty is obtained from here: NEWVALUE!\n");
    document.Bookmarks.Create(document.FindAll("NEWVALUE!", SearchOptions.CaseSensitive)[0], "bmOne");
    document.AppendText("\nMyNumericProperty: ");
    document.Fields.Create(document.Range.End, "DOCVARIABLE CustomProperty MyNumericProperty");
    document.AppendText("\nMyStringProperty: ");
    document.Fields.Create(document.Range.End, "DOCVARIABLE CustomProperty MyStringProperty");
    document.AppendText("\nMyBooleanProperty: ");
    document.Fields.Create(document.Range.End, "DOCVARIABLE CustomProperty MyBooleanProperty");
    document.AppendText("\nMyBookmarkProperty: ");
    document.Fields.Create(document.Range.End, "DOCVARIABLE CustomProperty MyBookmarkProperty");
    document.EndUpdate();

    document.CustomProperties["MyNumericProperty"] = 123.45;
    document.CustomProperties["MyStringProperty"] = "The Final Answer";
    document.CustomProperties["MyBookmarkProperty"] = document.Bookmarks[0];
    document.CustomProperties["MyBooleanProperty"] = true;

    document.CalculateDocumentVariable += DocumentPropertyDisplayHelper.OnCalculateDocumentVariable;
    document.Fields.Update();
class DocumentPropertyDisplayHelper
{
    public static void OnCalculateDocumentVariable(object sender, DevExpress.XtraRichEdit.CalculateDocumentVariableEventArgs e)
    {
        if (e.Arguments.Count == 0 || e.VariableName != "CustomProperty")
            return;
        string name = e.Arguments[0].Value;
        var customProperty = ((DevExpress.Xpf.RichEdit.RichEditControl)sender).Document.CustomProperties[name];
        if (customProperty != null)
            e.Value = customProperty.ToString();
        e.Handled = true;
    }
}
private void SetCustomProperties()
   {
      Document document = richEditControl.Document;
      document.CustomProperties.Add("MyNumericProperty", 123.45);
      document.CustomProperties.Add("MyStringProperty", "The Final Answer");
      document.CustomProperties.Add("MyBookmarkProperty", document.Bookmarks[0]);
      document.CustomProperties.Add("MyBooleanProperty", true);
   }