search/mag_sel search/close
Aspose::Words::ParagraphCollection Class Reference

Provides typed access to a collection of Paragraph nodes.

Examples

Shows how to check whether a paragraph is a move revision.

auto doc = MakeObject<Document>(MyDir + u"Revisions.docx");
// This document contains "Move" revisions, which appear when we highlight text with the cursor,
// and then drag it to move it to another location
// while tracking revisions in Microsoft Word via "Review" -> "Track changes".
ASSERT_EQ(6, doc->get_Revisions()->LINQ_Count([](SharedPtr<Revision> r) { return r->get_RevisionType() == RevisionType::Moving; }));
SharedPtr<ParagraphCollection> paragraphs = doc->get_FirstSection()->get_Body()->get_Paragraphs();
// Move revisions consist of pairs of "Move from", and "Move to" revisions.
// These revisions are potential changes to the document that we can either accept or reject.
// Before we accept/reject a move revision, the document
// must keep track of both the departure and arrival destinations of the text.
// The second and the fourth paragraph define one such revision, and thus both have the same contents.
ASSERT_EQ(paragraphs->idx_get(1)->GetText(), paragraphs->idx_get(3)->GetText());
// The "Move from" revision is the paragraph where we dragged the text from.
// If we accept the revision, this paragraph will disappear,
// and the other will remain and no longer be a revision.
ASSERT_TRUE(paragraphs->idx_get(1)->get_IsMoveFromRevision());
// The "Move to" revision is the paragraph where we dragged the text to.
// If we reject the revision, this paragraph instead will disappear, and the other will remain.
ASSERT_TRUE(paragraphs->idx_get(3)->get_IsMoveToRevision());

#include <Aspose.Words.Cpp/ParagraphCollection.h>

+ Inheritance diagram for Aspose::Words::ParagraphCollection:

Public Member Functions

virtual const TypeInfoGetType () const override
 
SharedPtr< Paragraphidx_get (int32_t index)
 Retrieves a Paragraph at the given index. More...
 
virtual bool Is (const TypeInfo &target) const override
 
ArrayPtr< SharedPtr< Paragraph > > ToArray ()
 Copies all paragraphs from the collection to a new array of paragraphs. More...
 
- Public Member Functions inherited from NodeCollection
void Add (SharedPtr< Node > node)
 Adds a node to the end of the collection. More...
 
void Clear ()
 Removes all nodes from this collection and from the document. More...
 
bool Contains (SharedPtr< Node > node)
 Determines whether a node is in the collection. More...
 
SharedPtr< CompositeNodeget_Container () override
 
int32_t get_Count ()
 Gets the number of nodes in the collection. More...
 
SharedPtr< NodeGetCurrentNode () override
 
SharedPtr< IEnumerator< SharedPtr< Node > > > GetEnumerator () override
 Provides a simple "foreach" style iteration over the collection of nodes. More...
 
SharedPtr< NodeGetNextMatchingNode (SharedPtr< Node > curNode) override
 
SharedPtr< Nodeidx_get (int32_t index)
 Retrieves a node at the given index. More...
 
int32_t IndexOf (SharedPtr< Node > node)
 Returns the zero-based index of the specified node. More...
 
void Insert (int32_t index, SharedPtr< Node > node)
 Inserts a node into the collection at the specified index. More...
 
void Remove (SharedPtr< Node > node)
 Removes the node from the collection and from the document. More...
 
void RemoveAt (int32_t index)
 Removes the node at the specified index from the collection and from the document. More...
 
ArrayPtr< SharedPtr< Node > > ToArray ()
 Copies all nodes from the collection to a new array of nodes. More...
 

Static Public Member Functions

static const TypeInfoType ()
 
- Static Public Member Functions inherited from NodeCollection
static const TypeInfoType ()
 

Member Function Documentation

◆ GetType()

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

Reimplemented from Aspose::Words::NodeCollection.

◆ idx_get()

System::SharedPtr<Aspose::Words::Paragraph> Aspose::Words::ParagraphCollection::idx_get ( int32_t  index)

Retrieves a Paragraph at the given 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 check whether a paragraph is a move revision.

auto doc = MakeObject<Document>(MyDir + u"Revisions.docx");
// This document contains "Move" revisions, which appear when we highlight text with the cursor,
// and then drag it to move it to another location
// while tracking revisions in Microsoft Word via "Review" -> "Track changes".
ASSERT_EQ(6, doc->get_Revisions()->LINQ_Count([](SharedPtr<Revision> r) { return r->get_RevisionType() == RevisionType::Moving; }));
SharedPtr<ParagraphCollection> paragraphs = doc->get_FirstSection()->get_Body()->get_Paragraphs();
// Move revisions consist of pairs of "Move from", and "Move to" revisions.
// These revisions are potential changes to the document that we can either accept or reject.
// Before we accept/reject a move revision, the document
// must keep track of both the departure and arrival destinations of the text.
// The second and the fourth paragraph define one such revision, and thus both have the same contents.
ASSERT_EQ(paragraphs->idx_get(1)->GetText(), paragraphs->idx_get(3)->GetText());
// The "Move from" revision is the paragraph where we dragged the text from.
// If we accept the revision, this paragraph will disappear,
// and the other will remain and no longer be a revision.
ASSERT_TRUE(paragraphs->idx_get(1)->get_IsMoveFromRevision());
// The "Move to" revision is the paragraph where we dragged the text to.
// If we reject the revision, this paragraph instead will disappear, and the other will remain.
ASSERT_TRUE(paragraphs->idx_get(3)->get_IsMoveToRevision());

◆ Is()

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

Reimplemented from Aspose::Words::NodeCollection.

◆ ToArray()

System::ArrayPtr<System::SharedPtr<Aspose::Words::Paragraph> > Aspose::Words::ParagraphCollection::ToArray ( )

Copies all paragraphs from the collection to a new array of paragraphs.

Returns
An array of paragraphs.
Examples

Shows how to create an array from a NodeCollection.

auto doc = MakeObject<Document>(MyDir + u"Paragraphs.docx");
ArrayPtr<SharedPtr<Paragraph>> paras = doc->get_FirstSection()->get_Body()->get_Paragraphs()->ToArray();
ASSERT_EQ(22, paras->get_Length());

Shows how to use "hot remove" to remove a node during enumeration.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"The first paragraph");
builder->Writeln(u"The second paragraph");
builder->Writeln(u"The third paragraph");
builder->Writeln(u"The fourth paragraph");
// Remove a node from the collection in the middle of an enumeration.
for (SharedPtr<Paragraph> para : doc->get_FirstSection()->get_Body()->get_Paragraphs()->ToArray())
{
if (para->get_Range()->get_Text().Contains(u"third"))
{
para->Remove();
}
}
ASSERT_FALSE(doc->GetText().Contains(u"The third paragraph"));

◆ Type()

static const System::TypeInfo& Aspose::Words::ParagraphCollection::Type ( )
static