com.aspose.words

Class FieldArgumentBuilder

  • java.lang.Object
    • com.aspose.words.FieldArgumentBuilder
public class FieldArgumentBuilder 
extends java.lang.Object

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

Example:

Inserts a field into a document using field builder constructor
Document doc = new Document();

// Add text into the paragraph
Paragraph para = doc.getFirstSection().getBody().getParagraphs().get(0);
Run run = new Run(doc);
run.setText(" Hello World!");

para.appendChild(run);

FieldArgumentBuilder argumentBuilder = new FieldArgumentBuilder();
argumentBuilder.addField(new FieldBuilder(FieldType.FIELD_MERGE_FIELD));
argumentBuilder.addText("BestField");

FieldBuilder fieldBuilder = new FieldBuilder(FieldType.FIELD_IF);
fieldBuilder.addArgument(argumentBuilder).addArgument("=").addArgument("BestField").addArgument(10).addArgument(20.0).addSwitch("12", "13").buildAndInsert(run);

doc.updateFields();

Example:

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.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 ");

// Use a field builder to create a formula field that will be used by another field builder
FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
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.FIELD_SYMBOL);
builder.addArgument(innerFormulaBuilder);
field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

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

// 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.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 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.FIELD_IF);
builder.addArgument(leftExpression);
builder.addArgument("=");
builder.addArgument(rightExpression);
builder.addArgument(trueOutput);
builder.addArgument(falseOutput);

builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));

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

Constructor Summary
FieldArgumentBuilder()
Initializes an instance of the FieldArgumentBuilder class.
 
Method Summary
FieldArgumentBuilderaddField(FieldBuilder fieldBuilder)
Adds a field represented by a FieldBuilder to the argument.
FieldArgumentBuilderaddNode(Inline node)
Adds a node to the argument.
FieldArgumentBuilderaddText(java.lang.String text)
Adds a plain text to the argument.
 

    • Constructor Detail

      • FieldArgumentBuilder

        public FieldArgumentBuilder()
        Initializes an instance of the FieldArgumentBuilder class.
    • Method Detail

      • addField

        public FieldArgumentBuilder addField(FieldBuilder fieldBuilder)
        Adds a field represented by a FieldBuilder to the argument.

        Example:

        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.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 ");
        
        // Use a field builder to create a formula field that will be used by another field builder
        FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
        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.FIELD_SYMBOL);
        builder.addArgument(innerFormulaBuilder);
        field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));
        
        Assert.assertEquals(field.getFieldCode(), " SYMBOL \u0013 = 100 + 74 \u0014\u0015 ");
        
        // 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.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 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.FIELD_IF);
        builder.addArgument(leftExpression);
        builder.addArgument("=");
        builder.addArgument(rightExpression);
        builder.addArgument(trueOutput);
        builder.addArgument(falseOutput);
        
        builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));
        
        doc.updateFields();
        doc.save(getArtifactsDir() + "Field.FieldBuilder.docx");

        Example:

        Inserts a field into a document using field builder constructor
        Document doc = new Document();
        
        // Add text into the paragraph
        Paragraph para = doc.getFirstSection().getBody().getParagraphs().get(0);
        Run run = new Run(doc);
        run.setText(" Hello World!");
        
        para.appendChild(run);
        
        FieldArgumentBuilder argumentBuilder = new FieldArgumentBuilder();
        argumentBuilder.addField(new FieldBuilder(FieldType.FIELD_MERGE_FIELD));
        argumentBuilder.addText("BestField");
        
        FieldBuilder fieldBuilder = new FieldBuilder(FieldType.FIELD_IF);
        fieldBuilder.addArgument(argumentBuilder).addArgument("=").addArgument("BestField").addArgument(10).addArgument(20.0).addSwitch("12", "13").buildAndInsert(run);
        
        doc.updateFields();
      • addNode

        public FieldArgumentBuilder addNode(Inline node)
        Adds a node to the argument. Only text level nodes are supported at the moment.

        Example:

        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.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 ");
        
        // Use a field builder to create a formula field that will be used by another field builder
        FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
        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.FIELD_SYMBOL);
        builder.addArgument(innerFormulaBuilder);
        field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));
        
        Assert.assertEquals(field.getFieldCode(), " SYMBOL \u0013 = 100 + 74 \u0014\u0015 ");
        
        // 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.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 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.FIELD_IF);
        builder.addArgument(leftExpression);
        builder.addArgument("=");
        builder.addArgument(rightExpression);
        builder.addArgument(trueOutput);
        builder.addArgument(falseOutput);
        
        builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));
        
        doc.updateFields();
        doc.save(getArtifactsDir() + "Field.FieldBuilder.docx");
      • addText

        public FieldArgumentBuilder addText(java.lang.String text)
        Adds a plain text to the argument.

        Example:

        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.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 ");
        
        // Use a field builder to create a formula field that will be used by another field builder
        FieldBuilder innerFormulaBuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
        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.FIELD_SYMBOL);
        builder.addArgument(innerFormulaBuilder);
        field = builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));
        
        Assert.assertEquals(field.getFieldCode(), " SYMBOL \u0013 = 100 + 74 \u0014\u0015 ");
        
        // 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.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 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.FIELD_IF);
        builder.addArgument(leftExpression);
        builder.addArgument("=");
        builder.addArgument(rightExpression);
        builder.addArgument(trueOutput);
        builder.addArgument(falseOutput);
        
        builder.buildAndInsert(doc.getFirstSection().getBody().appendParagraph(""));
        
        doc.updateFields();
        doc.save(getArtifactsDir() + "Field.FieldBuilder.docx");

        Example:

        Inserts a field into a document using field builder constructor
        Document doc = new Document();
        
        // Add text into the paragraph
        Paragraph para = doc.getFirstSection().getBody().getParagraphs().get(0);
        Run run = new Run(doc);
        run.setText(" Hello World!");
        
        para.appendChild(run);
        
        FieldArgumentBuilder argumentBuilder = new FieldArgumentBuilder();
        argumentBuilder.addField(new FieldBuilder(FieldType.FIELD_MERGE_FIELD));
        argumentBuilder.addText("BestField");
        
        FieldBuilder fieldBuilder = new FieldBuilder(FieldType.FIELD_IF);
        fieldBuilder.addArgument(argumentBuilder).addArgument("=").addArgument("BestField").addArgument(10).addArgument(20.0).addSwitch("12", "13").buildAndInsert(run);
        
        doc.updateFields();