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

A collection of Field objects that represents the fields in the specified range.

An instance of this collection iterates fields which start fall within the specified range.

The FieldCollection collection does not own the fields it contains, rather, is just a selection of fields.

The FieldCollection collection is "live", i.e. changes to the children of the node object that it was created from are immediately reflected in the fields returned by the FieldCollection properties and methods.

Examples

Shows how to work with a collection of fields.

void FieldCollection_()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Iterate over the field collection, and print contents and type
// of every field using a custom visitor implementation.
auto fieldVisitor = MakeObject<ExField::FieldVisitor>();
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Field>>> fieldEnumerator = fields->GetEnumerator();
while (fieldEnumerator->MoveNext())
{
if (fieldEnumerator->get_Current() != nullptr)
{
fieldEnumerator->get_Current()->get_Start()->Accept(fieldVisitor);
if (fieldEnumerator->get_Current()->get_Separator() != nullptr)
{
fieldEnumerator->get_Current()->get_Separator()->Accept(fieldVisitor);
}
fieldEnumerator->get_Current()->get_End()->Accept(fieldVisitor);
}
else
{
std::cout << "There are no fields in the document." << std::endl;
}
}
}
std::cout << fieldVisitor->GetText() << std::endl;
}
class FieldVisitor : public DocumentVisitor
{
public:
FieldVisitor()
{
mBuilder = MakeObject<System::Text::StringBuilder>();
}
String GetText()
{
return mBuilder->ToString();
}
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
mBuilder->AppendLine(String(u"Found field: ") + System::ObjectExt::ToString(fieldStart->get_FieldType()));
mBuilder->AppendLine(String(u"\tField code: ") + fieldStart->GetField()->GetFieldCode());
mBuilder->AppendLine(String(u"\tDisplayed as: ") + fieldStart->GetField()->get_Result());
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
mBuilder->AppendLine(String(u"\tFound separator: ") + fieldSeparator->GetText());
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
mBuilder->AppendLine(String(u"End of field: ") + System::ObjectExt::ToString(fieldEnd->get_FieldType()));
}
private:
SharedPtr<System::Text::StringBuilder> mBuilder;
};

Shows how to remove fields from a field collection.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields->idx_get(0)->Remove();
ASSERT_EQ(5, fields->get_Count());
// 2 - Get the collection to remove a field that we pass to its removal method:
SharedPtr<Field> lastField = fields->idx_get(3);
fields->Remove(lastField);
ASSERT_EQ(4, fields->get_Count());
// 3 - Remove a field from a collection at an index:
fields->RemoveAt(2);
ASSERT_EQ(3, fields->get_Count());
// 4 - Remove all the fields from the collection at once:
fields->Clear();
ASSERT_EQ(0, fields->get_Count());

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

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

Public Member Functions

void Clear ()
 Removes all fields of this collection from the document and from this collection itself. More...
 
int32_t get_Count ()
 Returns the number of the fields in the collection. More...
 
SharedPtr< IEnumerator< SharedPtr< Field > > > GetEnumerator () override
 Returns an enumerator object. More...
 
virtual const TypeInfoGetType () const override
 
SharedPtr< Fieldidx_get (int32_t index)
 Returns a field at the specified index. More...
 
virtual bool Is (const TypeInfo &target) const override
 
void Remove (SharedPtr< Field > field)
 Removes the specified field from this collection and from the document. More...
 
void RemoveAt (int32_t index)
 Removes a field at the specified index from this collection and from the document. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ Clear()

void Aspose::Words::Fields::FieldCollection::Clear ( )

Removes all fields of this collection from the document and from this collection itself.

Examples

Shows how to remove fields from a field collection.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields->idx_get(0)->Remove();
ASSERT_EQ(5, fields->get_Count());
// 2 - Get the collection to remove a field that we pass to its removal method:
SharedPtr<Field> lastField = fields->idx_get(3);
fields->Remove(lastField);
ASSERT_EQ(4, fields->get_Count());
// 3 - Remove a field from a collection at an index:
fields->RemoveAt(2);
ASSERT_EQ(3, fields->get_Count());
// 4 - Remove all the fields from the collection at once:
fields->Clear();
ASSERT_EQ(0, fields->get_Count());

◆ get_Count()

int32_t Aspose::Words::Fields::FieldCollection::get_Count ( )

Returns the number of the fields in the collection.

Examples

Shows how to work with a collection of fields.

void FieldCollection_()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Iterate over the field collection, and print contents and type
// of every field using a custom visitor implementation.
auto fieldVisitor = MakeObject<ExField::FieldVisitor>();
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Field>>> fieldEnumerator = fields->GetEnumerator();
while (fieldEnumerator->MoveNext())
{
if (fieldEnumerator->get_Current() != nullptr)
{
fieldEnumerator->get_Current()->get_Start()->Accept(fieldVisitor);
if (fieldEnumerator->get_Current()->get_Separator() != nullptr)
{
fieldEnumerator->get_Current()->get_Separator()->Accept(fieldVisitor);
}
fieldEnumerator->get_Current()->get_End()->Accept(fieldVisitor);
}
else
{
std::cout << "There are no fields in the document." << std::endl;
}
}
}
std::cout << fieldVisitor->GetText() << std::endl;
}
class FieldVisitor : public DocumentVisitor
{
public:
FieldVisitor()
{
mBuilder = MakeObject<System::Text::StringBuilder>();
}
String GetText()
{
return mBuilder->ToString();
}
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
mBuilder->AppendLine(String(u"Found field: ") + System::ObjectExt::ToString(fieldStart->get_FieldType()));
mBuilder->AppendLine(String(u"\tField code: ") + fieldStart->GetField()->GetFieldCode());
mBuilder->AppendLine(String(u"\tDisplayed as: ") + fieldStart->GetField()->get_Result());
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
mBuilder->AppendLine(String(u"\tFound separator: ") + fieldSeparator->GetText());
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
mBuilder->AppendLine(String(u"End of field: ") + System::ObjectExt::ToString(fieldEnd->get_FieldType()));
}
private:
SharedPtr<System::Text::StringBuilder> mBuilder;
};

Shows how to remove fields from a field collection.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields->idx_get(0)->Remove();
ASSERT_EQ(5, fields->get_Count());
// 2 - Get the collection to remove a field that we pass to its removal method:
SharedPtr<Field> lastField = fields->idx_get(3);
fields->Remove(lastField);
ASSERT_EQ(4, fields->get_Count());
// 3 - Remove a field from a collection at an index:
fields->RemoveAt(2);
ASSERT_EQ(3, fields->get_Count());
// 4 - Remove all the fields from the collection at once:
fields->Clear();
ASSERT_EQ(0, fields->get_Count());

◆ GetEnumerator()

System::SharedPtr<System::Collections::Generic::IEnumerator<System::SharedPtr<Aspose::Words::Fields::Field> > > Aspose::Words::Fields::FieldCollection::GetEnumerator ( )
override

Returns an enumerator object.

Examples

Shows how to work with a collection of fields.

void FieldCollection_()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Iterate over the field collection, and print contents and type
// of every field using a custom visitor implementation.
auto fieldVisitor = MakeObject<ExField::FieldVisitor>();
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Field>>> fieldEnumerator = fields->GetEnumerator();
while (fieldEnumerator->MoveNext())
{
if (fieldEnumerator->get_Current() != nullptr)
{
fieldEnumerator->get_Current()->get_Start()->Accept(fieldVisitor);
if (fieldEnumerator->get_Current()->get_Separator() != nullptr)
{
fieldEnumerator->get_Current()->get_Separator()->Accept(fieldVisitor);
}
fieldEnumerator->get_Current()->get_End()->Accept(fieldVisitor);
}
else
{
std::cout << "There are no fields in the document." << std::endl;
}
}
}
std::cout << fieldVisitor->GetText() << std::endl;
}
class FieldVisitor : public DocumentVisitor
{
public:
FieldVisitor()
{
mBuilder = MakeObject<System::Text::StringBuilder>();
}
String GetText()
{
return mBuilder->ToString();
}
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
mBuilder->AppendLine(String(u"Found field: ") + System::ObjectExt::ToString(fieldStart->get_FieldType()));
mBuilder->AppendLine(String(u"\tField code: ") + fieldStart->GetField()->GetFieldCode());
mBuilder->AppendLine(String(u"\tDisplayed as: ") + fieldStart->GetField()->get_Result());
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
mBuilder->AppendLine(String(u"\tFound separator: ") + fieldSeparator->GetText());
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
mBuilder->AppendLine(String(u"End of field: ") + System::ObjectExt::ToString(fieldEnd->get_FieldType()));
}
private:
SharedPtr<System::Text::StringBuilder> mBuilder;
};

◆ GetType()

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

Reimplemented from System::Object.

◆ idx_get()

System::SharedPtr<Aspose::Words::Fields::Field> Aspose::Words::Fields::FieldCollection::idx_get ( int32_t  index)

Returns a field at the specified index.

The index is zero-based.

Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.

If index is greater than or equal to the number of items in the list, this returns a null reference.

If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.

Parameters
indexAn index into the collection.
Examples

Shows how to remove fields from a field collection.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields->idx_get(0)->Remove();
ASSERT_EQ(5, fields->get_Count());
// 2 - Get the collection to remove a field that we pass to its removal method:
SharedPtr<Field> lastField = fields->idx_get(3);
fields->Remove(lastField);
ASSERT_EQ(4, fields->get_Count());
// 3 - Remove a field from a collection at an index:
fields->RemoveAt(2);
ASSERT_EQ(3, fields->get_Count());
// 4 - Remove all the fields from the collection at once:
fields->Clear();
ASSERT_EQ(0, fields->get_Count());

◆ Is()

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

Reimplemented from System::Object.

◆ Remove()

void Aspose::Words::Fields::FieldCollection::Remove ( System::SharedPtr< Aspose::Words::Fields::Field field)

Removes the specified field from this collection and from the document.

Parameters
fieldA field to remove.
Examples

Shows how to remove fields from a field collection.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields->idx_get(0)->Remove();
ASSERT_EQ(5, fields->get_Count());
// 2 - Get the collection to remove a field that we pass to its removal method:
SharedPtr<Field> lastField = fields->idx_get(3);
fields->Remove(lastField);
ASSERT_EQ(4, fields->get_Count());
// 3 - Remove a field from a collection at an index:
fields->RemoveAt(2);
ASSERT_EQ(3, fields->get_Count());
// 4 - Remove all the fields from the collection at once:
fields->Clear();
ASSERT_EQ(0, fields->get_Count());

◆ RemoveAt()

void Aspose::Words::Fields::FieldCollection::RemoveAt ( int32_t  index)

Removes a field at the specified index from this collection and from the document.

Parameters
indexAn index into the collection.
Examples

Shows how to remove fields from a field collection.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder->InsertField(u" TIME ");
builder->InsertField(u" REVNUM ");
builder->InsertField(u" AUTHOR \"John Doe\" ");
builder->InsertField(u" SUBJECT \"My Subject\" ");
builder->InsertField(u" QUOTE \"Hello world!\" ");
doc->UpdateFields();
SharedPtr<FieldCollection> fields = doc->get_Range()->get_Fields();
ASSERT_EQ(6, fields->get_Count());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields->idx_get(0)->Remove();
ASSERT_EQ(5, fields->get_Count());
// 2 - Get the collection to remove a field that we pass to its removal method:
SharedPtr<Field> lastField = fields->idx_get(3);
fields->Remove(lastField);
ASSERT_EQ(4, fields->get_Count());
// 3 - Remove a field from a collection at an index:
fields->RemoveAt(2);
ASSERT_EQ(3, fields->get_Count());
// 4 - Remove all the fields from the collection at once:
fields->Clear();
ASSERT_EQ(0, fields->get_Count());

◆ Type()

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