search/mag_sel search/close
Aspose::Words::Fields::FieldArgumentBuilder Class Reference

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

Examples

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

auto doc = MakeObject<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 ƒ (Florin) symbol.
auto builder = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(402);
builder->AddSwitch(u"\\f", u"Arial");
builder->AddSwitch(u"\\s", 25);
builder->AddSwitch(u"\\u");
SharedPtr<Field> field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->get_FirstParagraph());
ASSERT_EQ(u" SYMBOL 402 \\f Arial \\s 25 \\u ", field->GetFieldCode());
// 2 - Nested field:
// Use a field builder to create a formula field used as an inner field by another field builder.
auto innerFormulaBuilder = MakeObject<FieldBuilder>(FieldType::FieldFormula);
innerFormulaBuilder->AddArgument(100);
innerFormulaBuilder->AddArgument(u"+");
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 = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(innerFormulaBuilder);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
// 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_EQ(u" SYMBOL \u0013 = 100 + 74 \u0014\u0015 ", 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.
auto leftExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
leftExpression->AddArgument(2);
leftExpression->AddArgument(u"+");
leftExpression->AddArgument(3);
auto rightExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
rightExpression->AddArgument(2.5);
rightExpression->AddArgument(u"*");
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.
auto trueOutput = MakeObject<FieldArgumentBuilder>();
trueOutput->AddText(u"True, both expressions amount to ");
trueOutput->AddField(leftExpression);
auto falseOutput = MakeObject<FieldArgumentBuilder>();
falseOutput->AddNode(MakeObject<Run>(doc, u"False, "));
falseOutput->AddField(leftExpression);
falseOutput->AddNode(MakeObject<Run>(doc, u" 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 = MakeObject<FieldBuilder>(FieldType::FieldIf);
builder->AddArgument(leftExpression);
builder->AddArgument(u"=");
builder->AddArgument(rightExpression);
builder->AddArgument(trueOutput);
builder->AddArgument(falseOutput);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
ASSERT_EQ(String(u" IF \u0013 = 2 + 3 \u0014\u0015 = \u0013 = 2.5 * 5.2 \u0014\u0015 ") +
u"\"True, both expressions amount to \u0013 = 2 + 3 \u0014\u0015\" " +
u"\"False, \u0013 = 2 + 3 \u0014\u0015 does not equal \u0013 = 2.5 * 5.2 \u0014\u0015\" ",
field->GetFieldCode());
doc->UpdateFields();
doc->Save(ArtifactsDir + u"Field.SYMBOL.docx");

#include <Aspose.Words.Cpp/Fields/FieldArgumentBuilder.h>

+ Inheritance diagram for Aspose::Words::Fields::FieldArgumentBuilder:

Public Member Functions

 FieldArgumentBuilder ()
 Initializes an instance of the FieldArgumentBuilder class. More...
 
SharedPtr< FieldArgumentBuilderAddField (SharedPtr< FieldBuilder > fieldBuilder)
 Adds a field represented by a FieldBuilder to the argument. More...
 
SharedPtr< FieldArgumentBuilderAddNode (SharedPtr< Inline > node)
 Adds a node to the argument. More...
 
SharedPtr< FieldArgumentBuilderAddText (String text)
 Adds a plain text to the argument. More...
 
void BuildBlock (SharedPtr< DocumentBuilder > documentBuilder) override
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 

Static Public Member Functions

static const TypeInfoType ()
 

Constructor & Destructor Documentation

◆ FieldArgumentBuilder()

Aspose::Words::Fields::FieldArgumentBuilder::FieldArgumentBuilder ( )

Initializes an instance of the FieldArgumentBuilder class.

Member Function Documentation

◆ AddField()

System::SharedPtr<Aspose::Words::Fields::FieldArgumentBuilder> Aspose::Words::Fields::FieldArgumentBuilder::AddField ( System::SharedPtr< Aspose::Words::Fields::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.

auto doc = MakeObject<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 ƒ (Florin) symbol.
auto builder = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(402);
builder->AddSwitch(u"\\f", u"Arial");
builder->AddSwitch(u"\\s", 25);
builder->AddSwitch(u"\\u");
SharedPtr<Field> field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->get_FirstParagraph());
ASSERT_EQ(u" SYMBOL 402 \\f Arial \\s 25 \\u ", field->GetFieldCode());
// 2 - Nested field:
// Use a field builder to create a formula field used as an inner field by another field builder.
auto innerFormulaBuilder = MakeObject<FieldBuilder>(FieldType::FieldFormula);
innerFormulaBuilder->AddArgument(100);
innerFormulaBuilder->AddArgument(u"+");
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 = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(innerFormulaBuilder);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
// 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_EQ(u" SYMBOL \u0013 = 100 + 74 \u0014\u0015 ", 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.
auto leftExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
leftExpression->AddArgument(2);
leftExpression->AddArgument(u"+");
leftExpression->AddArgument(3);
auto rightExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
rightExpression->AddArgument(2.5);
rightExpression->AddArgument(u"*");
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.
auto trueOutput = MakeObject<FieldArgumentBuilder>();
trueOutput->AddText(u"True, both expressions amount to ");
trueOutput->AddField(leftExpression);
auto falseOutput = MakeObject<FieldArgumentBuilder>();
falseOutput->AddNode(MakeObject<Run>(doc, u"False, "));
falseOutput->AddField(leftExpression);
falseOutput->AddNode(MakeObject<Run>(doc, u" 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 = MakeObject<FieldBuilder>(FieldType::FieldIf);
builder->AddArgument(leftExpression);
builder->AddArgument(u"=");
builder->AddArgument(rightExpression);
builder->AddArgument(trueOutput);
builder->AddArgument(falseOutput);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
ASSERT_EQ(String(u" IF \u0013 = 2 + 3 \u0014\u0015 = \u0013 = 2.5 * 5.2 \u0014\u0015 ") +
u"\"True, both expressions amount to \u0013 = 2 + 3 \u0014\u0015\" " +
u"\"False, \u0013 = 2 + 3 \u0014\u0015 does not equal \u0013 = 2.5 * 5.2 \u0014\u0015\" ",
field->GetFieldCode());
doc->UpdateFields();
doc->Save(ArtifactsDir + u"Field.SYMBOL.docx");

◆ AddNode()

System::SharedPtr<Aspose::Words::Fields::FieldArgumentBuilder> Aspose::Words::Fields::FieldArgumentBuilder::AddNode ( System::SharedPtr< Aspose::Words::Inline node)

Adds a node to the argument.

Examples

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

auto doc = MakeObject<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 ƒ (Florin) symbol.
auto builder = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(402);
builder->AddSwitch(u"\\f", u"Arial");
builder->AddSwitch(u"\\s", 25);
builder->AddSwitch(u"\\u");
SharedPtr<Field> field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->get_FirstParagraph());
ASSERT_EQ(u" SYMBOL 402 \\f Arial \\s 25 \\u ", field->GetFieldCode());
// 2 - Nested field:
// Use a field builder to create a formula field used as an inner field by another field builder.
auto innerFormulaBuilder = MakeObject<FieldBuilder>(FieldType::FieldFormula);
innerFormulaBuilder->AddArgument(100);
innerFormulaBuilder->AddArgument(u"+");
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 = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(innerFormulaBuilder);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
// 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_EQ(u" SYMBOL \u0013 = 100 + 74 \u0014\u0015 ", 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.
auto leftExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
leftExpression->AddArgument(2);
leftExpression->AddArgument(u"+");
leftExpression->AddArgument(3);
auto rightExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
rightExpression->AddArgument(2.5);
rightExpression->AddArgument(u"*");
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.
auto trueOutput = MakeObject<FieldArgumentBuilder>();
trueOutput->AddText(u"True, both expressions amount to ");
trueOutput->AddField(leftExpression);
auto falseOutput = MakeObject<FieldArgumentBuilder>();
falseOutput->AddNode(MakeObject<Run>(doc, u"False, "));
falseOutput->AddField(leftExpression);
falseOutput->AddNode(MakeObject<Run>(doc, u" 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 = MakeObject<FieldBuilder>(FieldType::FieldIf);
builder->AddArgument(leftExpression);
builder->AddArgument(u"=");
builder->AddArgument(rightExpression);
builder->AddArgument(trueOutput);
builder->AddArgument(falseOutput);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
ASSERT_EQ(String(u" IF \u0013 = 2 + 3 \u0014\u0015 = \u0013 = 2.5 * 5.2 \u0014\u0015 ") +
u"\"True, both expressions amount to \u0013 = 2 + 3 \u0014\u0015\" " +
u"\"False, \u0013 = 2 + 3 \u0014\u0015 does not equal \u0013 = 2.5 * 5.2 \u0014\u0015\" ",
field->GetFieldCode());
doc->UpdateFields();
doc->Save(ArtifactsDir + u"Field.SYMBOL.docx");

◆ AddText()

System::SharedPtr<Aspose::Words::Fields::FieldArgumentBuilder> Aspose::Words::Fields::FieldArgumentBuilder::AddText ( System::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.

auto doc = MakeObject<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 ƒ (Florin) symbol.
auto builder = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(402);
builder->AddSwitch(u"\\f", u"Arial");
builder->AddSwitch(u"\\s", 25);
builder->AddSwitch(u"\\u");
SharedPtr<Field> field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->get_FirstParagraph());
ASSERT_EQ(u" SYMBOL 402 \\f Arial \\s 25 \\u ", field->GetFieldCode());
// 2 - Nested field:
// Use a field builder to create a formula field used as an inner field by another field builder.
auto innerFormulaBuilder = MakeObject<FieldBuilder>(FieldType::FieldFormula);
innerFormulaBuilder->AddArgument(100);
innerFormulaBuilder->AddArgument(u"+");
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 = MakeObject<FieldBuilder>(FieldType::FieldSymbol);
builder->AddArgument(innerFormulaBuilder);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
// 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_EQ(u" SYMBOL \u0013 = 100 + 74 \u0014\u0015 ", 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.
auto leftExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
leftExpression->AddArgument(2);
leftExpression->AddArgument(u"+");
leftExpression->AddArgument(3);
auto rightExpression = MakeObject<FieldBuilder>(FieldType::FieldFormula);
rightExpression->AddArgument(2.5);
rightExpression->AddArgument(u"*");
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.
auto trueOutput = MakeObject<FieldArgumentBuilder>();
trueOutput->AddText(u"True, both expressions amount to ");
trueOutput->AddField(leftExpression);
auto falseOutput = MakeObject<FieldArgumentBuilder>();
falseOutput->AddNode(MakeObject<Run>(doc, u"False, "));
falseOutput->AddField(leftExpression);
falseOutput->AddNode(MakeObject<Run>(doc, u" 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 = MakeObject<FieldBuilder>(FieldType::FieldIf);
builder->AddArgument(leftExpression);
builder->AddArgument(u"=");
builder->AddArgument(rightExpression);
builder->AddArgument(trueOutput);
builder->AddArgument(falseOutput);
field = builder->BuildAndInsert(doc->get_FirstSection()->get_Body()->AppendParagraph(String::Empty));
ASSERT_EQ(String(u" IF \u0013 = 2 + 3 \u0014\u0015 = \u0013 = 2.5 * 5.2 \u0014\u0015 ") +
u"\"True, both expressions amount to \u0013 = 2 + 3 \u0014\u0015\" " +
u"\"False, \u0013 = 2 + 3 \u0014\u0015 does not equal \u0013 = 2.5 * 5.2 \u0014\u0015\" ",
field->GetFieldCode());
doc->UpdateFields();
doc->Save(ArtifactsDir + u"Field.SYMBOL.docx");

◆ BuildBlock()

void Aspose::Words::Fields::FieldArgumentBuilder::BuildBlock ( System::SharedPtr< Aspose::Words::DocumentBuilder documentBuilder)
override

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Fields::FieldArgumentBuilder::GetType ( ) const
overridevirtual

◆ Is()

virtual bool Aspose::Words::Fields::FieldArgumentBuilder::Is ( const System::TypeInfo target) const
overridevirtual

◆ Type()

static const System::TypeInfo& Aspose::Words::Fields::FieldArgumentBuilder::Type ( )
static