FieldBuilder Class
Builds a field from field code tokens (arguments and switches).
Inheritance Hierarchy
SystemObject
  Aspose.Words.FieldsFieldBuilder

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.11
Syntax
public class FieldBuilder

The FieldBuilder type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleFieldBuilder
Initializes an instance of the FieldBuilder class.
Methods
  NameDescription
Public methodCode exampleAddArgument(Double)
Adds a field's argument.
Public methodCode exampleAddArgument(Int32)
Adds a field's argument.
Public methodCode exampleAddArgument(String)
Adds a field's argument.
Public methodCode exampleAddArgument(FieldArgumentBuilder)
Adds a field's argument represented by FieldArgumentBuilder to the field's code.
Public methodCode exampleAddArgument(FieldBuilder)
Adds a child field represented by another FieldBuilder to the field's code.
Public methodCode exampleAddSwitch(String)
Adds a field's switch.
Public methodCode exampleAddSwitch(String, Double)
Adds a field's switch.
Public methodCode exampleAddSwitch(String, Int32)
Adds a field's switch.
Public methodCode exampleAddSwitch(String, String)
Adds a field's switch.
Public methodCode exampleBuildAndInsert(Inline)
Builds and inserts a field into the document before the specified inline node.
Public methodCode exampleBuildAndInsert(Paragraph)
Builds and inserts a field into the document to the end of the specified paragraph.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString (Inherited from Object.)
Examples
Shows how to insert fields using a field builder.
Document doc = new Document();

// Use a field builder to add a SYMBOL field which displays the "F with hook" symbol
FieldBuilder builder = new FieldBuilder(FieldType.FieldSymbol);
builder.AddArgument(402);
builder.AddSwitch("\\f", "Arial");
builder.AddSwitch("\\s", 25);
builder.AddSwitch("\\u");
Field field = builder.BuildAndInsert(doc.FirstSection.Body.FirstParagraph);

Assert.AreEqual(" SYMBOL 402 \\f Arial \\s 25 \\u ", field.GetFieldCode());

// Use a field builder to create a formula field that will be used by another field builder
FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FieldFormula);
innerFormulaBuilder.AddArgument(100);
innerFormulaBuilder.AddArgument("+");
innerFormulaBuilder.AddArgument(74);

// Add a field builder as an argument to another field builder
// The result of our formula field will be used as an ANSI value representing the "enclosed R" symbol,
// to be displayed by this SYMBOL field
builder = new FieldBuilder(FieldType.FieldSymbol);
builder.AddArgument(innerFormulaBuilder);
field = builder.BuildAndInsert(doc.FirstSection.Body.AppendParagraph(""));

Assert.AreEqual(" SYMBOL \u0013 = 100 + 74 \u0014\u0015 ", field.GetFieldCode());

// Now we will use our builder to construct a more complex field with nested fields
// For our IF field, we will first create two formula fields to serve as expressions
// Their results will be tested for equality to decide what value an IF field displays
FieldBuilder leftExpression = new FieldBuilder(FieldType.FieldFormula);
leftExpression.AddArgument(2);
leftExpression.AddArgument("+");
leftExpression.AddArgument(3);

FieldBuilder rightExpression = new FieldBuilder(FieldType.FieldFormula);
rightExpression.AddArgument(2.5);
rightExpression.AddArgument("*");
rightExpression.AddArgument(5.2);

// Next, we will create two field arguments using field argument builders
// These will serve as the two possible outputs of our IF field and they will also use our two expressions
FieldArgumentBuilder trueOutput = new FieldArgumentBuilder();
trueOutput.AddText("True, both expressions amount to ");
trueOutput.AddField(leftExpression);

FieldArgumentBuilder falseOutput = new FieldArgumentBuilder();
falseOutput.AddNode(new Run(doc, "False, "));
falseOutput.AddField(leftExpression);
falseOutput.AddNode(new Run(doc, " does not equal "));
falseOutput.AddField(rightExpression);

// Finally, we will use a field builder to create an IF field which takes two field builders as expressions,
// and two field argument builders as the two potential outputs
builder = new FieldBuilder(FieldType.FieldIf);
builder.AddArgument(leftExpression);
builder.AddArgument("=");
builder.AddArgument(rightExpression);
builder.AddArgument(trueOutput);
builder.AddArgument(falseOutput);

builder.BuildAndInsert(doc.FirstSection.Body.AppendParagraph(""));

doc.UpdateFields();
doc.Save(ArtifactsDir + "Field.FieldBuilder.docx");
See Also