FieldArgumentBuilder

Inheritance: java.lang.Object

public class FieldArgumentBuilder

Builds a complex field argument consisting of fields, nodes, and plain text.

To learn more, visit the Working with Fields documentation article.

Examples:

Shows how to construct fields using a field builder, and then insert them into the document.


 Document doc = new Document();

 // Below are three examples of field construction done using a field builder.
 // 1 -  Single field:
 // Use a field builder to add a SYMBOL field which displays the \u0192 (Florin) symbol.
 FieldBuilder builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(402);
 builder.addSwitch("\\f", "Arial");
 builder.addSwitch("\\s", 25);
 builder.addSwitch("\\u");
 Field field = builder.buildAndInsert(doc.getFirstSection().getBody().getFirstParagraph());

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

 // 2 -  Nested field:
 // Use a field builder to create a formula field used as an inner field by another field builder.
 FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
 innerFormulaBuilder.addArgument(100);
 innerFormulaBuilder.addArgument("+");
 innerFormulaBuilder.addArgument(74);

 // Create another builder for another SYMBOL field, and insert the formula field
 // that we have created above into the SYMBOL field as its argument.
 builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(innerFormulaBuilder);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 // The outer SYMBOL field will use the formula field result, 174, as its argument,
 // which will make the field display the  (Registered Sign) symbol since its character number is 174.
 Assert.assertEquals(" SYMBOL  = 100 + 74  ", field.getFieldCode());

 // 3 -  Multiple nested fields and arguments:
 // Now, we will use a builder to create an IF field, which displays one of two custom string values,
 // depending on the true/false value of its expression. To get a true/false value
 // that determines which string the IF field displays, the IF field will test two numeric expressions for equality.
 // We will provide the two expressions in the form of formula fields, which we will nest inside the IF field.
 FieldBuilder leftExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 leftExpression.addArgument(2);
 leftExpression.addArgument("+");
 leftExpression.addArgument(3);

 FieldBuilder rightExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 rightExpression.addArgument(2.5);
 rightExpression.addArgument("*");
 rightExpression.addArgument(5.2);

 // Next, we will build two field arguments, which will serve as the true/false output strings for the IF field.
 // These arguments will reuse the output values of our numeric 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 create one more field builder for the IF field and combine all of the expressions.
 builder = new FieldBuilder(FieldType.FIELD_IF);
 builder.addArgument(leftExpression);
 builder.addArgument("=");
 builder.addArgument(rightExpression);
 builder.addArgument(trueOutput);
 builder.addArgument(falseOutput);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 Assert.assertEquals(" IF  = 2 + 3  =  = 2.5 * 5.2  " +
         "\"True, both expressions amount to  = 2 + 3 \" " +
         "\"False,  = 2 + 3  does not equal  = 2.5 * 5.2 \" ", field.getFieldCode());

 doc.updateFields();
 doc.save(getArtifactsDir() + "Field.SYMBOL.docx");
 

Constructors

ConstructorDescription
FieldArgumentBuilder()Initializes an instance of the FieldArgumentBuilder class.

Methods

MethodDescription
addField(FieldBuilder fieldBuilder)Adds a field represented by a FieldBuilder to the argument.
addNode(Inline node)Adds a node to the argument.
addText(String text)Adds a plain text to the argument.
buildBlock(DocumentBuilder documentBuilder)

FieldArgumentBuilder()

public FieldArgumentBuilder()

Initializes an instance of the FieldArgumentBuilder class.

Examples:

Shows how to construct fields using a field builder, and then insert them into the document.


 Document doc = new Document();

 // Below are three examples of field construction done using a field builder.
 // 1 -  Single field:
 // Use a field builder to add a SYMBOL field which displays the \u0192 (Florin) symbol.
 FieldBuilder builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(402);
 builder.addSwitch("\\f", "Arial");
 builder.addSwitch("\\s", 25);
 builder.addSwitch("\\u");
 Field field = builder.buildAndInsert(doc.getFirstSection().getBody().getFirstParagraph());

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

 // 2 -  Nested field:
 // Use a field builder to create a formula field used as an inner field by another field builder.
 FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
 innerFormulaBuilder.addArgument(100);
 innerFormulaBuilder.addArgument("+");
 innerFormulaBuilder.addArgument(74);

 // Create another builder for another SYMBOL field, and insert the formula field
 // that we have created above into the SYMBOL field as its argument.
 builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(innerFormulaBuilder);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 // The outer SYMBOL field will use the formula field result, 174, as its argument,
 // which will make the field display the  (Registered Sign) symbol since its character number is 174.
 Assert.assertEquals(" SYMBOL  = 100 + 74  ", field.getFieldCode());

 // 3 -  Multiple nested fields and arguments:
 // Now, we will use a builder to create an IF field, which displays one of two custom string values,
 // depending on the true/false value of its expression. To get a true/false value
 // that determines which string the IF field displays, the IF field will test two numeric expressions for equality.
 // We will provide the two expressions in the form of formula fields, which we will nest inside the IF field.
 FieldBuilder leftExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 leftExpression.addArgument(2);
 leftExpression.addArgument("+");
 leftExpression.addArgument(3);

 FieldBuilder rightExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 rightExpression.addArgument(2.5);
 rightExpression.addArgument("*");
 rightExpression.addArgument(5.2);

 // Next, we will build two field arguments, which will serve as the true/false output strings for the IF field.
 // These arguments will reuse the output values of our numeric 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 create one more field builder for the IF field and combine all of the expressions.
 builder = new FieldBuilder(FieldType.FIELD_IF);
 builder.addArgument(leftExpression);
 builder.addArgument("=");
 builder.addArgument(rightExpression);
 builder.addArgument(trueOutput);
 builder.addArgument(falseOutput);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 Assert.assertEquals(" IF  = 2 + 3  =  = 2.5 * 5.2  " +
         "\"True, both expressions amount to  = 2 + 3 \" " +
         "\"False,  = 2 + 3  does not equal  = 2.5 * 5.2 \" ", field.getFieldCode());

 doc.updateFields();
 doc.save(getArtifactsDir() + "Field.SYMBOL.docx");
 

addField(FieldBuilder fieldBuilder)

public FieldArgumentBuilder addField(FieldBuilder fieldBuilder)

Adds a field represented by a FieldBuilder to the argument.

Examples:

Shows how to construct fields using a field builder, and then insert them into the document.


 Document doc = new Document();

 // Below are three examples of field construction done using a field builder.
 // 1 -  Single field:
 // Use a field builder to add a SYMBOL field which displays the \u0192 (Florin) symbol.
 FieldBuilder builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(402);
 builder.addSwitch("\\f", "Arial");
 builder.addSwitch("\\s", 25);
 builder.addSwitch("\\u");
 Field field = builder.buildAndInsert(doc.getFirstSection().getBody().getFirstParagraph());

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

 // 2 -  Nested field:
 // Use a field builder to create a formula field used as an inner field by another field builder.
 FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
 innerFormulaBuilder.addArgument(100);
 innerFormulaBuilder.addArgument("+");
 innerFormulaBuilder.addArgument(74);

 // Create another builder for another SYMBOL field, and insert the formula field
 // that we have created above into the SYMBOL field as its argument.
 builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(innerFormulaBuilder);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 // The outer SYMBOL field will use the formula field result, 174, as its argument,
 // which will make the field display the  (Registered Sign) symbol since its character number is 174.
 Assert.assertEquals(" SYMBOL  = 100 + 74  ", field.getFieldCode());

 // 3 -  Multiple nested fields and arguments:
 // Now, we will use a builder to create an IF field, which displays one of two custom string values,
 // depending on the true/false value of its expression. To get a true/false value
 // that determines which string the IF field displays, the IF field will test two numeric expressions for equality.
 // We will provide the two expressions in the form of formula fields, which we will nest inside the IF field.
 FieldBuilder leftExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 leftExpression.addArgument(2);
 leftExpression.addArgument("+");
 leftExpression.addArgument(3);

 FieldBuilder rightExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 rightExpression.addArgument(2.5);
 rightExpression.addArgument("*");
 rightExpression.addArgument(5.2);

 // Next, we will build two field arguments, which will serve as the true/false output strings for the IF field.
 // These arguments will reuse the output values of our numeric 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 create one more field builder for the IF field and combine all of the expressions.
 builder = new FieldBuilder(FieldType.FIELD_IF);
 builder.addArgument(leftExpression);
 builder.addArgument("=");
 builder.addArgument(rightExpression);
 builder.addArgument(trueOutput);
 builder.addArgument(falseOutput);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 Assert.assertEquals(" IF  = 2 + 3  =  = 2.5 * 5.2  " +
         "\"True, both expressions amount to  = 2 + 3 \" " +
         "\"False,  = 2 + 3  does not equal  = 2.5 * 5.2 \" ", field.getFieldCode());

 doc.updateFields();
 doc.save(getArtifactsDir() + "Field.SYMBOL.docx");
 

Parameters:

ParameterTypeDescription
fieldBuilderFieldBuilder

Returns: FieldArgumentBuilder

addNode(Inline node)

public FieldArgumentBuilder addNode(Inline node)

Adds a node to the argument.

Remarks:

Only text level nodes are supported at the moment.

Examples:

Shows how to construct fields using a field builder, and then insert them into the document.


 Document doc = new Document();

 // Below are three examples of field construction done using a field builder.
 // 1 -  Single field:
 // Use a field builder to add a SYMBOL field which displays the \u0192 (Florin) symbol.
 FieldBuilder builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(402);
 builder.addSwitch("\\f", "Arial");
 builder.addSwitch("\\s", 25);
 builder.addSwitch("\\u");
 Field field = builder.buildAndInsert(doc.getFirstSection().getBody().getFirstParagraph());

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

 // 2 -  Nested field:
 // Use a field builder to create a formula field used as an inner field by another field builder.
 FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
 innerFormulaBuilder.addArgument(100);
 innerFormulaBuilder.addArgument("+");
 innerFormulaBuilder.addArgument(74);

 // Create another builder for another SYMBOL field, and insert the formula field
 // that we have created above into the SYMBOL field as its argument.
 builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(innerFormulaBuilder);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 // The outer SYMBOL field will use the formula field result, 174, as its argument,
 // which will make the field display the  (Registered Sign) symbol since its character number is 174.
 Assert.assertEquals(" SYMBOL  = 100 + 74  ", field.getFieldCode());

 // 3 -  Multiple nested fields and arguments:
 // Now, we will use a builder to create an IF field, which displays one of two custom string values,
 // depending on the true/false value of its expression. To get a true/false value
 // that determines which string the IF field displays, the IF field will test two numeric expressions for equality.
 // We will provide the two expressions in the form of formula fields, which we will nest inside the IF field.
 FieldBuilder leftExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 leftExpression.addArgument(2);
 leftExpression.addArgument("+");
 leftExpression.addArgument(3);

 FieldBuilder rightExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 rightExpression.addArgument(2.5);
 rightExpression.addArgument("*");
 rightExpression.addArgument(5.2);

 // Next, we will build two field arguments, which will serve as the true/false output strings for the IF field.
 // These arguments will reuse the output values of our numeric 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 create one more field builder for the IF field and combine all of the expressions.
 builder = new FieldBuilder(FieldType.FIELD_IF);
 builder.addArgument(leftExpression);
 builder.addArgument("=");
 builder.addArgument(rightExpression);
 builder.addArgument(trueOutput);
 builder.addArgument(falseOutput);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 Assert.assertEquals(" IF  = 2 + 3  =  = 2.5 * 5.2  " +
         "\"True, both expressions amount to  = 2 + 3 \" " +
         "\"False,  = 2 + 3  does not equal  = 2.5 * 5.2 \" ", field.getFieldCode());

 doc.updateFields();
 doc.save(getArtifactsDir() + "Field.SYMBOL.docx");
 

Parameters:

ParameterTypeDescription
nodeInline

Returns: FieldArgumentBuilder

addText(String text)

public FieldArgumentBuilder addText(String text)

Adds a plain text to the argument.

Examples:

Shows how to construct fields using a field builder, and then insert them into the document.


 Document doc = new Document();

 // Below are three examples of field construction done using a field builder.
 // 1 -  Single field:
 // Use a field builder to add a SYMBOL field which displays the \u0192 (Florin) symbol.
 FieldBuilder builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(402);
 builder.addSwitch("\\f", "Arial");
 builder.addSwitch("\\s", 25);
 builder.addSwitch("\\u");
 Field field = builder.buildAndInsert(doc.getFirstSection().getBody().getFirstParagraph());

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

 // 2 -  Nested field:
 // Use a field builder to create a formula field used as an inner field by another field builder.
 FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
 innerFormulaBuilder.addArgument(100);
 innerFormulaBuilder.addArgument("+");
 innerFormulaBuilder.addArgument(74);

 // Create another builder for another SYMBOL field, and insert the formula field
 // that we have created above into the SYMBOL field as its argument.
 builder = new FieldBuilder(FieldType.FIELD_SYMBOL);
 builder.addArgument(innerFormulaBuilder);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 // The outer SYMBOL field will use the formula field result, 174, as its argument,
 // which will make the field display the  (Registered Sign) symbol since its character number is 174.
 Assert.assertEquals(" SYMBOL  = 100 + 74  ", field.getFieldCode());

 // 3 -  Multiple nested fields and arguments:
 // Now, we will use a builder to create an IF field, which displays one of two custom string values,
 // depending on the true/false value of its expression. To get a true/false value
 // that determines which string the IF field displays, the IF field will test two numeric expressions for equality.
 // We will provide the two expressions in the form of formula fields, which we will nest inside the IF field.
 FieldBuilder leftExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 leftExpression.addArgument(2);
 leftExpression.addArgument("+");
 leftExpression.addArgument(3);

 FieldBuilder rightExpression = new FieldBuilder(FieldType.FIELD_FORMULA);
 rightExpression.addArgument(2.5);
 rightExpression.addArgument("*");
 rightExpression.addArgument(5.2);

 // Next, we will build two field arguments, which will serve as the true/false output strings for the IF field.
 // These arguments will reuse the output values of our numeric 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 create one more field builder for the IF field and combine all of the expressions.
 builder = new FieldBuilder(FieldType.FIELD_IF);
 builder.addArgument(leftExpression);
 builder.addArgument("=");
 builder.addArgument(rightExpression);
 builder.addArgument(trueOutput);
 builder.addArgument(falseOutput);
 field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

 Assert.assertEquals(" IF  = 2 + 3  =  = 2.5 * 5.2  " +
         "\"True, both expressions amount to  = 2 + 3 \" " +
         "\"False,  = 2 + 3  does not equal  = 2.5 * 5.2 \" ", field.getFieldCode());

 doc.updateFields();
 doc.save(getArtifactsDir() + "Field.SYMBOL.docx");
 

Parameters:

ParameterTypeDescription
textjava.lang.String

Returns: FieldArgumentBuilder

buildBlock(DocumentBuilder documentBuilder)

public void buildBlock(DocumentBuilder documentBuilder)

Parameters:

ParameterTypeDescription
documentBuilderDocumentBuilder