Building Fields with FieldBuilder
Building Fields with FieldBuilder
This guide shows how to construct a Word field from scratch in Aspose.Words FOSS for .NET using FieldBuilder and FieldArgumentBuilder, instead of working with a field that already exists in a document. For reading, updating, or removing existing fields, see the Working with Fields guide.
Creating a Field with FieldBuilder
FieldBuilder builds a field from field code tokens — arguments and switches. Construct one with new FieldBuilder(fieldType), passing the FieldType of the field you want to build. Add arguments with the overloaded AddArgument(argument) method, which accepts a string, int, double, a nested FieldBuilder, or a FieldArgumentBuilder. Add switches with the overloaded AddSwitch(switchName), AddSwitch(switchName, switchArgument) (string), or AddSwitch(switchName, switchArgument) (int/double) methods.
Inserting the Built Field
Once a FieldBuilder has its arguments and switches, call BuildAndInsert(refNode) to insert it into the document — one overload accepts an Inline node and inserts the field before it, and another accepts a Paragraph and inserts the field at the end of that paragraph.
Composing Complex Arguments with FieldArgumentBuilder
FieldArgumentBuilder builds a complex field argument made up of fields, nodes, and plain text, for cases where a single argument needs more than a plain string. Construct one with new FieldArgumentBuilder(), then call AddText(text) to add plain text, AddNode(node) to add an Inline node, or AddField(fieldBuilder) to nest another field (built with its own FieldBuilder) inside the argument. Pass the completed FieldArgumentBuilder to FieldBuilder.AddArgument(argument) to use it as one of the outer field’s arguments.
Tips and Best Practices
- Use
FieldBuilderwhen constructing a field type that isn’t backed by one of the dedicated field classes (such asFieldDateorFieldSymbol), or when generating field codes programmatically from data rather than hand-authoring them. - Nest a child field inside an outer field’s argument with
FieldBuilder.AddArgument(FieldBuilder)for a simple case, or build a mixed argument of text, nodes, and nested fields withFieldArgumentBuilderwhen the argument is more than a single field. - Pick the
BuildAndInsert(refNode)overload that matches where you want the field to land — before a specificInlinenode, or at the end of aParagraph. - After
BuildAndInsert, treat the result like any other field in the tree — for example, callUpdate()on it if the field type requires an explicit recalculation.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
BuildAndInsert targets the wrong location | The wrong overload was used for the intended insertion point | Pass an Inline node to insert before it, or a Paragraph to insert at its end |
| A field argument won’t accept plain text and a node together | AddArgument was called with a single string/int/double instead of a composed argument | Build the mixed argument with FieldArgumentBuilder (AddText, AddNode, AddField) and pass that to AddArgument |
| Switch value isn’t recognized in the built field code | AddSwitch(switchName) was used when the switch actually needs an argument | Use the AddSwitch(switchName, switchArgument) overload with the switch’s string, int, or double value |
FAQ
When should I use FieldBuilder instead of inserting a pre-formed field code?
Use FieldBuilder when you need explicit control over a field’s arguments and switches as you construct them programmatically, rather than inserting a pre-formed field code string.
How do I add more than one argument to a field?
Call AddArgument once per argument — it’s overloaded for string, int, double, a nested FieldBuilder, and FieldArgumentBuilder.
Can I build a field whose argument contains a mix of text and another field?
Yes — use FieldArgumentBuilder, calling AddText, AddNode, and AddField as needed, then pass it to FieldBuilder.AddArgument.
How do I actually put the built field into the document?
Call BuildAndInsert(refNode), passing either an Inline node (inserts before it) or a Paragraph (inserts at its end).
API Reference Summary
| Class/Method | Description |
|---|---|
FieldBuilder | Builds a field from field code tokens (arguments and switches) |
FieldBuilder(fieldType) | Constructor taking the FieldType to build |
FieldBuilder.AddArgument(argument) | Adds an argument (string, int, double, FieldBuilder, or FieldArgumentBuilder) |
FieldBuilder.AddSwitch(switchName) / AddSwitch(switchName, switchArgument) | Adds a field switch, with or without a value |
FieldBuilder.BuildAndInsert(refNode) | Inserts the built field before an Inline node or at the end of a Paragraph |
FieldArgumentBuilder | Builds a complex field argument from text, nodes, and nested fields |
FieldArgumentBuilder.AddText(text) | Adds plain text to the argument |
FieldArgumentBuilder.AddNode(node) | Adds an Inline node to the argument |
FieldArgumentBuilder.AddField(fieldBuilder) | Nests a field built by FieldBuilder into the argument |