IRichEditDocumentServer.CalculateDocumentVariable Event
Fires when the DOCVARIABLE field is updated.
Namespace: DevExpress.XtraRichEdit
Assembly: DevExpress.RichEdit.v24.1.Core.dll
NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation
Declaration
Event Data
The CalculateDocumentVariable event's data class is CalculateDocumentVariableEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Arguments | Provides access to a collection of arguments within the DOCVARIABLE field. |
FieldLocked | Gets or sets a locked attribute to the field for which the event occurs. |
Handled | Gets or sets whether the default action is required. |
KeepLastParagraph | Gets or sets whether the last paragraph of the inserted document is kept in the resulting document. |
PreserveInsertedContentFormatting | Gets or sets whether to insert an additional hidden paragraph so that the inserted content is not formatted with the DOCVARIABLE field’s paragraph formatting. |
Value | Gets or sets the value of the DOCVARIABLE field that fired the event. |
VariableName | Gets the name of the document variable to which the DOCVARIABLE field refers. |
Remarks
The CalculateDocumentVariable event is fired when the DOCVARIABLE field value is calculated.
The event is fired when a field is updated (using Field.Update or FieldCollection.Update methods), or during the mail merge on the RichEditDocumentServer.MailMerge method call. Handle this event to analyze DOCVARIABLE field switches and arguments, and provide custom content to insert into this field.
Assign a special DocVariableValue.Current value to the CalculateDocumentVariableEventArgs.Value property and set e.Handled to true to retain the existing DOCVARIABLE value.
You can specify the FieldOptions.UpdateLockedFields property to raise the event for locked DOCVARIABLE fields.
You can lock/unlock the field within the CalculateDocumentVariable handler using the CalculateDocumentVariableEventArgs.FieldLocked property. Set e.Handled to true to apply changes.
Note
If you use a nested field in your project, such as {DOCVARIABLE {MERGEFIELD MyDataField_1}}, check the fields’ CalculateDocumentVariableEventArgs.Arguments value. When the CalculateDocumentVariable event is fired for the first time, e.Arguments contains the <<MyDataField_1>> (the placeholder for the field) since the merge field is not yet calculated. Do not handle the event if the argument is invalid (use comparison and return statements if required) .
If you use RichEditDocumentServer.MailMerge method to evaluate nested DOCVARIABLE fields, set the RichEditMailMergeOptions.ViewMergedData option to true. Otherwise you will get field placeholders instead of values within the CalculateDocumentVariable handler.
Example
This code snippet demonstrates how to handle the Document.CalculateDocumentVariable event to insert dynamic content into the document. In this example, it is used to get weather conditions. A variable name specified in the DOCVARIABLE indicates a choice between location and weather, while the location itself is specified by the field argument.
private static void Document_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) {
if (e.Arguments.Count > 0) {
string location = e.Arguments[0].Value.ToString();
if ((location.Trim() == String.Empty) || (location.Contains("<"))) {
e.Value = " ";
e.Handled = true;
return;
}
switch (e.VariableName) {
case "Weather":
Conditions conditions = new Conditions();
conditions = Weather.GetCurrentConditions(location);
e.Value = String.Format("Weather for {0}: \nConditions: {1}\nTemperature (C) :{2}\nHumidity: {3}\nWind: {4}\n",
location, conditions.Condition, conditions.TempC, conditions.Humidity, conditions.Wind);
break;
case "LOCATION":
if (location == "DO NOT CHANGE!") e.Value = DocVariableValue.Current;
break;
default:
e.Value = "LOCKED FIELD UPDATED";
break;
}
}
else {
e.Value = "LOCKED FIELD UPDATED";
}
e.Handled = true;
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CalculateDocumentVariable event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.