DOCVARIABLE
- 3 minutes to read
DOCVARIABLE
Mixed field
{ DOCVARIABLE “variable name” “argument1” “argument 2”… }
Overview
Inserts a string associated with a document variable.
The following members are used to obtain a DOCVARIABLE field value.
Member | Description |
---|---|
Document.Variables | RichEditDocumentServer first searches this collection for a specified variable. |
Document.CalculateDocumentVariable | If variable is not found in the collection, the RichEditDocumentServer raises this event so you can specify the value in code. |
Note
You can return a text or the entire IRichEditDocumentServer.Document as a document variable.
Example: Handle the CalculateDocumentVariable Event to Insert Dynamic Content
Note
A complete code sample project is available at How to use document variable (DOCVARIABLE) fields.
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;
}
Note
Set the event arguments’ CalculateDocumentVariableEventArgs.Value property to the DocVariableValue.Current value in the CalculateDocumentVariable event handler to save the initial DOCVARIABLE field value.
Use the Document.UpdateDocVariablesBeforePrint or the FieldOptions.UpdateDocVariablesBeforePrint properties to specify whether to update the DOCVARIABLE fields before printing.