Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Sometimes users need to apply custom formatting to fields. In this article, we will look at a couple of examples of how this can be done.
To learn more options, see the full list of properties for each field type in the corresponding class in the Fields namespace.
Aspose.Words provides API for custom formatting of field’s result. You can implement IFieldResultFormatter interface to control how the field result is formatted. You can apply numeric format switch, i.e. # “#.##”, date/time format switch, i.e. @ “dd.MM.yyyy”, and number format switch, i.e. * Ordinal.
The following code example shows how to apply custom formatting for the field result:
IF conditionIf you want to evaluate IF condition after mail merge, you can use the EvaluateCondition method that immediately returns the result of the expression evaluation.
The following code example shows how to use this method:
By default Aspose.Words updates TIME field with current culture short time format. If you want to format the TIME field according to your requirement, you can achieve this by implementing IFieldUpdateCultureProvider interface.
The following code examples shows how to apply custom formatting to the TIME field:
Q: How can I apply a numeric format (e.g., two decimal places) to a field result?
A: Implement the IFieldResultFormatter interface and, in the FormatResult method, use a numeric format string such as "#.##" to format the value. Assign your formatter to Document.FieldOptions.FieldResultFormatter before calling Document.UpdateFields().
Q: What is the easiest way to format a DATE field with a custom pattern like dd.MM.yyyy?
A: You can either include the date format switch directly in the field code (\@ "dd.MM.yyyy"), or implement IFieldResultFormatter and return the formatted string for FieldDate types.
Q: How do I change the culture used by the TIME field so it displays a specific time format?
A: Implement the IFieldUpdateCultureProvider interface and return a CultureInfo object that defines the desired time format. Set this provider on Document.FieldOptions.FieldUpdateCultureProvider before updating fields.
Q: Will my custom field formatter work with fields generated by a mail‑merge operation?
A: Yes. After performing Document.MailMerge.Execute, assign the custom IFieldResultFormatter and then call Document.UpdateFields(); the formatter will be invoked for each field result.
Q: My custom formatter is not being called for a particular field—what could be wrong?
A: Ensure that the formatter is assigned to Document.FieldOptions.FieldResultFormatter before you call UpdateFields. Also verify that the field type is supported by the formatter and that no other code resets the formatter after you set it.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.