Aspose::Words::Paragraph Class Reference

Detailed Description

Represents a paragraph of text.

Paragraph is a block-level node and can be a child of classes derived from Story or InlineStory.

Paragraph can contain any number of inline-level nodes and bookmarks.

The complete list of child nodes that can occur inside a paragraph consists of BookmarkStart, BookmarkEnd, FieldStart, FieldSeparator, FieldEnd, FormField, Comment, Footnote, Run, SpecialChar, Shape, GroupShape, SmartTag.

A valid paragraph in Microsoft Word always ends with a paragraph break character and a minimal valid paragraph consists just of a paragraph break. The Paragraph class automatically appends the appropriate paragraph break character at the end and this character is not part of the child nodes of the Paragraph, therefore a Paragraph can be empty.

Do not include the end of paragraph ControlChar.ParagraphBreak or end of cell ControlChar.Cell characters inside the text of the paragraph as it might make the paragraph invalid when the document is opened in Microsoft Word.

Examples

Shows how to construct an Aspose.Words document by hand.

auto doc = MakeObject<Document>();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc->RemoveAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
auto section = MakeObject<Section>(doc);
doc->AppendChild(section);
// Set some page setup properties for the section.
section->get_PageSetup()->set_SectionStart(SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(PaperSize::Letter);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
auto para = MakeObject<Paragraph>(doc);
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
body->AppendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
auto run = MakeObject<Run>(doc);
run->set_Text(u"Hello World!");
run->get_Font()->set_Color(System::Drawing::Color::get_Red());
para->AppendChild(run);
ASSERT_EQ(u"Hello World!", doc->GetText().Trim());
doc->Save(ArtifactsDir + u"Section.CreateManually.docx");

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

+ Inheritance diagram for Aspose::Words::Paragraph:

Public Member Functions

 Paragraph (const SharedPtr< DocumentBase > &doc)
 Initializes a new instance of the Paragraph class. More...
 
bool Accept (SharedPtr< DocumentVisitor > visitor) override
 Accepts a visitor. More...
 
SharedPtr< FieldAppendField (FieldType fieldType, bool updateField)
 Appends a field to this paragraph. More...
 
SharedPtr< FieldAppendField (const String &fieldCode)
 Appends a field to this paragraph. More...
 
SharedPtr< FieldAppendField (const String &fieldCode, const String &fieldValue)
 Appends a field to this paragraph. More...
 
bool get_BreakIsStyleSeparator ()
 True if this paragraph break is a Style Separator. A style separator allows one paragraph to consist of parts that have different paragraph styles. More...
 
SharedPtr< FrameFormatget_FrameFormat ()
 Provides access to the paragraph formatting properties. More...
 
bool get_IsDeleteRevision ()
 Returns true if this object was deleted in Microsoft Word while change tracking was enabled. More...
 
bool get_IsEndOfCell ()
 True if this paragraph is the last paragraph in a Cell; false otherwise. More...
 
bool get_IsEndOfDocument ()
 True if this paragraph is the last paragraph in the last section of the document. More...
 
bool get_IsEndOfHeaderFooter ()
 True if this paragraph is the last paragraph in the HeaderFooter (main text story) of a Section; false otherwise. More...
 
bool get_IsEndOfSection ()
 True if this paragraph is the last paragraph in the Body (main text story) of a Section; false otherwise. More...
 
bool get_IsFormatRevision ()
 Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled. More...
 
bool get_IsInCell ()
 True if this paragraph is an immediate child of Cell; false otherwise. More...
 
bool get_IsInsertRevision ()
 Returns true if this object was inserted in Microsoft Word while change tracking was enabled. More...
 
bool get_IsListItem ()
 True when the paragraph is an item in a bulleted or numbered list in original revision. More...
 
bool get_IsMoveFromRevision ()
 Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled. More...
 
bool get_IsMoveToRevision ()
 Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled. More...
 
SharedPtr< ListFormatget_ListFormat ()
 Provides access to the list formatting properties of the paragraph. More...
 
SharedPtr< ListLabelget_ListLabel ()
 Gets a ListLabel object that provides access to list numbering value and formatting for this paragraph. More...
 
NodeType get_NodeType () const override
 Returns NodeType.Paragraph. More...
 
SharedPtr< Fontget_ParagraphBreakFont ()
 Provides access to the font formatting of the paragraph break character. More...
 
SharedPtr< ParagraphFormatget_ParagraphFormat ()
 Provides access to the paragraph formatting properties. More...
 
SharedPtr< Sectionget_ParentSection ()
 Retrieves the parent Section of the paragraph. More...
 
SharedPtr< Storyget_ParentStory ()
 Retrieves the parent section-level story that can be Body or HeaderFooter. More...
 
SharedPtr< RunCollectionget_Runs ()
 Provides access to the typed collection of pieces of text inside the paragraph. More...
 
ArrayPtr< SharedPtr< TabStop > > GetEffectiveTabStops ()
 Returns array of all tab stops applied to this paragraph, including applied indirectly by styles or lists. More...
 
String GetText () override
 Gets the text of this paragraph including the end of paragraph character. More...
 
virtual const TypeInfoGetType () const override
 
SharedPtr< FieldInsertField (FieldType fieldType, bool updateField, const SharedPtr< Node > &refNode, bool isAfter)
 Inserts a field into this paragraph. More...
 
SharedPtr< FieldInsertField (const String &fieldCode, const SharedPtr< Node > &refNode, bool isAfter)
 Inserts a field into this paragraph. More...
 
SharedPtr< FieldInsertField (const String &fieldCode, const String &fieldValue, const SharedPtr< Node > &refNode, bool isAfter)
 Inserts a field into this paragraph. More...
 
virtual bool Is (const TypeInfo &target) const override
 
int32_t JoinRunsWithSameFormatting ()
 Joins runs with the same formatting in the paragraph. More...
 
- Public Member Functions inherited from CompositeNode
SharedPtr< NodeAppendChild (const SharedPtr< Node > &newChild)
 Adds the specified node to the end of the list of child nodes for this node. More...
 
SharedPtr< NodeCollectionget_ChildNodes ()
 Gets all immediate child nodes of this node. More...
 
int32_t get_Count ()
 Gets the number of immediate children of this node. More...
 
SharedPtr< Nodeget_FirstChild () const
 Gets the first child of the node. More...
 
bool get_HasChildNodes ()
 Returns true if this node has any child nodes. More...
 
bool get_IsComposite () override
 Returns true as this node can have child nodes. More...
 
SharedPtr< Nodeget_LastChild () const
 Gets the last child of the node. More...
 
SharedPtr< NodeGetChild (NodeType nodeType, int32_t index, bool isDeep)
 Returns an Nth child node that matches the specified type. More...
 
SharedPtr< NodeCollectionGetChildNodes (NodeType nodeType, bool isDeep)
 Returns a live collection of child nodes that match the specified type. More...
 
SharedPtr< IEnumerator< SharedPtr< Node > > > GetEnumerator () override
 Provides support for the for each style iteration over the child nodes of this node. More...
 
String GetText () override
 Gets the text of this node and of all its children. More...
 
virtual const TypeInfoGetType () const override
 
int32_t IndexOf (const SharedPtr< Node > &child)
 Returns the index of the specified child node in the child node array. More...
 
SharedPtr< NodeInsertAfter (const SharedPtr< Node > &newChild, const SharedPtr< Node > &refChild)
 Inserts the specified node immediately after the specified reference node. More...
 
SharedPtr< NodeInsertBefore (const SharedPtr< Node > &newChild, const SharedPtr< Node > &refChild)
 Inserts the specified node immediately before the specified reference node. More...
 
virtual bool Is (const TypeInfo &target) const override
 
SharedPtr< NodePrependChild (const SharedPtr< Node > &newChild)
 Adds the specified node to the beginning of the list of child nodes for this node. More...
 
void RemoveAllChildren ()
 Removes all the child nodes of the current node. More...
 
SharedPtr< NodeRemoveChild (const SharedPtr< Node > &oldChild)
 Removes the specified child node. More...
 
void RemoveSmartTags ()
 Removes all SmartTag descendant nodes of the current node. More...
 
SharedPtr< NodeListSelectNodes (const String &xpath)
 Selects a list of nodes matching the XPath expression. More...
 
SharedPtr< NodeSelectSingleNode (const String &xpath)
 Selects the first Node that matches the XPath expression. More...
 
void SetTemplateWeakPtr (uint32_t argument) override
 
- Public Member Functions inherited from Node
virtual bool Accept (SharedPtr< DocumentVisitor > visitor)=0
 Accepts a visitor. More...
 
SharedPtr< NodeClone (bool isCloneChildren)
 Creates a duplicate of the node. More...
 
int32_t get_CustomNodeId () const
 Specifies custom node identifier. More...
 
virtual SharedPtr< DocumentBaseget_Document () const
 Gets the document to which this node belongs. More...
 
virtual bool get_IsComposite ()
 Returns true if this node can contain other nodes. More...
 
SharedPtr< Nodeget_NextSibling ()
 Gets the node immediately following this node. More...
 
virtual NodeType get_NodeType () const =0
 Gets the type of this node. More...
 
SharedPtr< CompositeNodeget_ParentNode ()
 Gets the immediate parent of this node. More...
 
SharedPtr< Nodeget_PreviousSibling ()
 Gets the node immediately preceding this node. More...
 
SharedPtr< Rangeget_Range ()
 Returns a Range object that represents the portion of a document that is contained in this node. More...
 
SharedPtr< CompositeNodeGetAncestor (NodeType ancestorType)
 Gets the first ancestor of the specified NodeType. More...
 
template<typename T >
GetAncestorOf ()
 
virtual String GetText ()
 Gets the text of this node and of all its children. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
SharedPtr< NodeNextPreOrder (const SharedPtr< Node > &rootNode)
 Gets next node according to the pre-order tree traversal algorithm. More...
 
SharedPtr< NodePreviousPreOrder (const SharedPtr< Node > &rootNode)
 Gets the previous node according to the pre-order tree traversal algorithm. More...
 
void Remove ()
 Removes itself from the parent. More...
 
void set_CustomNodeId (int32_t value)
 Setter for get_CustomNodeId. More...
 
String ToString (SaveFormat saveFormat)
 Exports the content of the node into a string in the specified format. More...
 
String ToString (const SharedPtr< SaveOptions > &saveOptions)
 Exports the content of the node into a string using the specified save options. More...
 

Static Public Member Functions

static const TypeInfoType ()
 
- Static Public Member Functions inherited from CompositeNode
static const TypeInfoType ()
 
- Static Public Member Functions inherited from Node
static String NodeTypeToString (NodeType nodeType)
 A utility method that converts a node type enum value into a user friendly string. More...
 
static const TypeInfoType ()
 

Constructor & Destructor Documentation

◆ Paragraph()

Aspose::Words::Paragraph::Paragraph ( const System::SharedPtr< Aspose::Words::DocumentBase > &  doc)

Initializes a new instance of the Paragraph class.

When Paragraph is created, it belongs to the specified document, but is not yet part of the document and ParentNode is null.

To append Paragraph to the document use InsertAfter or InsertBefore on the story where you want the paragraph inserted.

Parameters
docThe owner document.
Examples

Shows how to construct an Aspose.Words document by hand.

auto doc = MakeObject<Document>();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc->RemoveAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
auto section = MakeObject<Section>(doc);
doc->AppendChild(section);
// Set some page setup properties for the section.
section->get_PageSetup()->set_SectionStart(SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(PaperSize::Letter);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
auto para = MakeObject<Paragraph>(doc);
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
body->AppendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
auto run = MakeObject<Run>(doc);
run->set_Text(u"Hello World!");
run->get_Font()->set_Color(System::Drawing::Color::get_Red());
para->AppendChild(run);
ASSERT_EQ(u"Hello World!", doc->GetText().Trim());
doc->Save(ArtifactsDir + u"Section.CreateManually.docx");

Member Function Documentation

◆ Accept()

bool Aspose::Words::Paragraph::Accept ( System::SharedPtr< Aspose::Words::DocumentVisitor visitor)
overridevirtual

Accepts a visitor.

Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.

For more info see the Visitor design pattern.

Parameters
visitorThe visitor that will visit the nodes.
Returns
True if all nodes were visited; false if DocumentVisitor stopped the operation before visiting all nodes.
Examples

Shows how to use a DocumentVisitor implementation to remove all hidden content from a document.

void RemoveHiddenContentFromDocument()
{
auto doc = MakeObject<Document>(MyDir + u"Hidden content.docx");
auto hiddenContentRemover = MakeObject<ExFont::RemoveHiddenContentVisitor>();
// Below are three types of fields which can accept a document visitor,
// which will allow it to visit the accepting node, and then traverse its child nodes in a depth-first manner.
// 1 - Paragraph node:
auto para = System::DynamicCast<Paragraph>(doc->GetChild(NodeType::Paragraph, 4, true));
para->Accept(hiddenContentRemover);
// 2 - Table node:
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
table->Accept(hiddenContentRemover);
// 3 - Document node:
doc->Accept(hiddenContentRemover);
doc->Save(ArtifactsDir + u"Font.RemoveHiddenContentFromDocument.docx");
}
class RemoveHiddenContentVisitor : public DocumentVisitor
{
public:
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
if (fieldStart->get_Font()->get_Hidden())
{
fieldStart->Remove();
}
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
if (fieldEnd->get_Font()->get_Hidden())
{
fieldEnd->Remove();
}
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
if (fieldSeparator->get_Font()->get_Hidden())
{
fieldSeparator->Remove();
}
}
VisitorAction VisitRun(SharedPtr<Run> run) override
{
if (run->get_Font()->get_Hidden())
{
run->Remove();
}
}
VisitorAction VisitParagraphStart(SharedPtr<Paragraph> paragraph) override
{
if (paragraph->get_ParagraphBreakFont()->get_Hidden())
{
paragraph->Remove();
}
}
VisitorAction VisitFormField(SharedPtr<FormField> formField) override
{
if (formField->get_Font()->get_Hidden())
{
formField->Remove();
}
}
VisitorAction VisitGroupShapeStart(SharedPtr<GroupShape> groupShape) override
{
if (groupShape->get_Font()->get_Hidden())
{
groupShape->Remove();
}
}
VisitorAction VisitShapeStart(SharedPtr<Shape> shape) override
{
if (shape->get_Font()->get_Hidden())
{
shape->Remove();
}
}
VisitorAction VisitCommentStart(SharedPtr<Comment> comment) override
{
if (comment->get_Font()->get_Hidden())
{
comment->Remove();
}
}
VisitorAction VisitFootnoteStart(SharedPtr<Footnote> footnote) override
{
if (footnote->get_Font()->get_Hidden())
{
footnote->Remove();
}
}
VisitorAction VisitSpecialChar(SharedPtr<SpecialChar> specialChar) override
{
if (specialChar->get_Font()->get_Hidden())
{
specialChar->Remove();
}
}
VisitorAction VisitTableEnd(SharedPtr<Table> table) override
{
// The content inside table cells may have the hidden content flag, but the tables themselves cannot.
// If this table had nothing but hidden content, this visitor would have removed all of it,
// and there would be no child nodes left.
// Thus, we can also treat the table itself as hidden content and remove it.
// Tables which are empty but do not have hidden content will have cells with empty paragraphs inside,
// which this visitor will not remove.
if (!table->get_HasChildNodes())
{
table->Remove();
}
}
VisitorAction VisitCellEnd(SharedPtr<Cell> cell) override
{
if (!cell->get_HasChildNodes() && cell->get_ParentNode() != nullptr)
{
cell->Remove();
}
}
VisitorAction VisitRowEnd(SharedPtr<Row> row) override
{
if (!row->get_HasChildNodes() && row->get_ParentNode() != nullptr)
{
row->Remove();
}
}
};

Implements Aspose::Words::Node.

◆ AppendField() [1/3]

System::SharedPtr< Aspose::Words::Fields::Field > Aspose::Words::Paragraph::AppendField ( Aspose::Words::Fields::FieldType  fieldType,
bool  updateField 
)

Appends a field to this paragraph.

Parameters
fieldTypeThe type of the field to append.
updateFieldSpecifies whether to update the field immediately.
Returns
A Field object that represents the appended field.
Examples

Shows various ways of appending fields to a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Below are three ways of appending a field to the end of a paragraph.
// 1 - Append a DATE field using a field type, and then update it:
paragraph->AppendField(FieldType::FieldDate, true);
// 2 - Append a TIME field using a field code:
paragraph->AppendField(u" TIME \\@ \"HH:mm:ss\" ");
// 3 - Append a QUOTE field using a field code, and get it to display a placeholder value:
paragraph->AppendField(u" QUOTE \"Real value\"", u"Placeholder value");
ASSERT_EQ(u"Placeholder value", doc->get_Range()->get_Fields()->idx_get(2)->get_Result());
// This field will display its placeholder value until we update it.
doc->UpdateFields();
ASSERT_EQ(u"Real value", doc->get_Range()->get_Fields()->idx_get(2)->get_Result());
doc->Save(ArtifactsDir + u"Paragraph.AppendField.docx");

◆ AppendField() [2/3]

System::SharedPtr< Aspose::Words::Fields::Field > Aspose::Words::Paragraph::AppendField ( const System::String fieldCode)

Appends a field to this paragraph.

Parameters
fieldCodeThe field code to append (without curly braces).
Returns
A Field object that represents the appended field.
Examples

Shows various ways of appending fields to a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Below are three ways of appending a field to the end of a paragraph.
// 1 - Append a DATE field using a field type, and then update it:
paragraph->AppendField(FieldType::FieldDate, true);
// 2 - Append a TIME field using a field code:
paragraph->AppendField(u" TIME \\@ \"HH:mm:ss\" ");
// 3 - Append a QUOTE field using a field code, and get it to display a placeholder value:
paragraph->AppendField(u" QUOTE \"Real value\"", u"Placeholder value");
ASSERT_EQ(u"Placeholder value", doc->get_Range()->get_Fields()->idx_get(2)->get_Result());
// This field will display its placeholder value until we update it.
doc->UpdateFields();
ASSERT_EQ(u"Real value", doc->get_Range()->get_Fields()->idx_get(2)->get_Result());
doc->Save(ArtifactsDir + u"Paragraph.AppendField.docx");

◆ AppendField() [3/3]

System::SharedPtr< Aspose::Words::Fields::Field > Aspose::Words::Paragraph::AppendField ( const System::String fieldCode,
const System::String fieldValue 
)

Appends a field to this paragraph.

Parameters
fieldCodeThe field code to append (without curly braces).
fieldValueThe field value to append. Pass null for fields that do not have a value.
Returns
A Field object that represents the appended field.
Examples

Shows various ways of appending fields to a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Below are three ways of appending a field to the end of a paragraph.
// 1 - Append a DATE field using a field type, and then update it:
paragraph->AppendField(FieldType::FieldDate, true);
// 2 - Append a TIME field using a field code:
paragraph->AppendField(u" TIME \\@ \"HH:mm:ss\" ");
// 3 - Append a QUOTE field using a field code, and get it to display a placeholder value:
paragraph->AppendField(u" QUOTE \"Real value\"", u"Placeholder value");
ASSERT_EQ(u"Placeholder value", doc->get_Range()->get_Fields()->idx_get(2)->get_Result());
// This field will display its placeholder value until we update it.
doc->UpdateFields();
ASSERT_EQ(u"Real value", doc->get_Range()->get_Fields()->idx_get(2)->get_Result());
doc->Save(ArtifactsDir + u"Paragraph.AppendField.docx");

◆ get_BreakIsStyleSeparator()

bool Aspose::Words::Paragraph::get_BreakIsStyleSeparator ( )

True if this paragraph break is a Style Separator. A style separator allows one paragraph to consist of parts that have different paragraph styles.

Examples

Shows how to write text to the same line as a TOC heading and have it not show up in the TOC.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertTableOfContents(u"\\o \\h \\z \\u");
builder->InsertBreak(BreakType::PageBreak);
// Insert a paragraph with a style that the TOC will pick up as an entry.
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Heading1);
// Both these strings are in the same paragraph and will therefore show up on the same TOC entry.
builder->Write(u"Heading 1. ");
builder->Write(u"Will appear in the TOC. ");
// If we insert a style separator, we can write more text in the same paragraph
// and use a different style without showing up in the TOC.
// If we use a heading type style after the separator, we can draw multiple TOC entries from one document text line.
builder->InsertStyleSeparator();
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Quote);
builder->Write(u"Won't appear in the TOC. ");
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_BreakIsStyleSeparator());
doc->UpdateFields();
doc->Save(ArtifactsDir + u"Paragraph.BreakIsStyleSeparator.docx");

◆ get_FrameFormat()

System::SharedPtr< Aspose::Words::FrameFormat > Aspose::Words::Paragraph::get_FrameFormat ( )

Provides access to the paragraph formatting properties.

Examples

Shows how to get information about formatting properties of paragraphs that are frames.

auto doc = MakeObject<Document>(MyDir + u"Paragraph frame.docx");
SharedPtr<Paragraph> paragraphFrame = doc->get_FirstSection()->get_Body()->get_Paragraphs()->LINQ_OfType<SharedPtr<Paragraph>>()->LINQ_First(
[](SharedPtr<Paragraph> p) { return p->get_FrameFormat()->get_IsFrame(); });
ASPOSE_ASSERT_EQ(233.3, paragraphFrame->get_FrameFormat()->get_Width());
ASPOSE_ASSERT_EQ(138.8, paragraphFrame->get_FrameFormat()->get_Height());
ASSERT_EQ(HeightRule::AtLeast, paragraphFrame->get_FrameFormat()->get_HeightRule());
ASSERT_EQ(HorizontalAlignment::Default, paragraphFrame->get_FrameFormat()->get_HorizontalAlignment());
ASSERT_EQ(VerticalAlignment::Default, paragraphFrame->get_FrameFormat()->get_VerticalAlignment());
ASPOSE_ASSERT_EQ(34.05, paragraphFrame->get_FrameFormat()->get_HorizontalPosition());
ASSERT_EQ(RelativeHorizontalPosition::Page, paragraphFrame->get_FrameFormat()->get_RelativeHorizontalPosition());
ASPOSE_ASSERT_EQ(9.0, paragraphFrame->get_FrameFormat()->get_HorizontalDistanceFromText());
ASPOSE_ASSERT_EQ(20.5, paragraphFrame->get_FrameFormat()->get_VerticalPosition());
ASSERT_EQ(RelativeVerticalPosition::Paragraph, paragraphFrame->get_FrameFormat()->get_RelativeVerticalPosition());
ASPOSE_ASSERT_EQ(0.0, paragraphFrame->get_FrameFormat()->get_VerticalDistanceFromText());

◆ get_IsDeleteRevision()

bool Aspose::Words::Paragraph::get_IsDeleteRevision ( )

Returns true if this object was deleted in Microsoft Word while change tracking was enabled.

Examples

Shows how to work with revision paragraphs.

auto doc = MakeObject<Document>();
SharedPtr<Body> body = doc->get_FirstSection()->get_Body();
SharedPtr<Paragraph> para = body->get_FirstParagraph();
para->AppendChild(MakeObject<Run>(doc, u"Paragraph 1. "));
body->AppendParagraph(u"Paragraph 2. ");
body->AppendParagraph(u"Paragraph 3. ");
// The above paragraphs are not revisions.
// Paragraphs that we add after starting revision tracking will register as "Insert" revisions.
doc->StartTrackRevisions(u"John Doe", System::DateTime::get_Now());
para = body->AppendParagraph(u"Paragraph 4. ");
ASSERT_TRUE(para->get_IsInsertRevision());
// Paragraphs that we remove after starting revision tracking will register as "Delete" revisions.
SharedPtr<ParagraphCollection> paragraphs = body->get_Paragraphs();
ASSERT_EQ(4, paragraphs->get_Count());
para = paragraphs->idx_get(2);
para->Remove();
// Such paragraphs will remain until we either accept or reject the delete revision.
// Accepting the revision will remove the paragraph for good,
// and rejecting the revision will leave it in the document as if we never deleted it.
ASSERT_EQ(4, paragraphs->get_Count());
ASSERT_TRUE(para->get_IsDeleteRevision());
// Accept the revision, and then verify that the paragraph is gone.
doc->AcceptAllRevisions();
ASSERT_EQ(3, paragraphs->get_Count());
ASSERT_EQ(0, para->get_Count());
ASSERT_EQ(String(u"Paragraph 1. \r") + u"Paragraph 2. \r" + u"Paragraph 4.", doc->GetText().Trim());

◆ get_IsEndOfCell()

bool Aspose::Words::Paragraph::get_IsEndOfCell ( )

True if this paragraph is the last paragraph in a Cell; false otherwise.

Examples

Shows how to set a table to stay together on the same page.

auto doc = MakeObject<Document>(MyDir + u"Table spanning two pages.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Enabling KeepWithNext for every paragraph in the table except for the
// last ones in the last row will prevent the table from splitting across multiple pages.
for (const auto& cell : System::IterateOver(table->GetChildNodes(NodeType::Cell, true)->LINQ_OfType<SharedPtr<Cell>>()))
{
for (const auto& para : System::IterateOver(cell->get_Paragraphs()->LINQ_OfType<SharedPtr<Paragraph>>()))
{
ASSERT_TRUE(para->get_IsInCell());
if (!(cell->get_ParentRow()->get_IsLastRow() && para->get_IsEndOfCell()))
{
para->get_ParagraphFormat()->set_KeepWithNext(true);
}
}
}
doc->Save(ArtifactsDir + u"Table.KeepTableTogether.docx");

◆ get_IsEndOfDocument()

bool Aspose::Words::Paragraph::get_IsEndOfDocument ( )

True if this paragraph is the last paragraph in the last section of the document.

Examples

Shows how to insert a paragraph into the document.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Font> font = builder->get_Font();
font->set_Size(16);
font->set_Bold(true);
font->set_Name(u"Arial");
font->set_Underline(Underline::Dash);
SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat();
paragraphFormat->set_FirstLineIndent(8);
paragraphFormat->set_Alignment(ParagraphAlignment::Justify);
paragraphFormat->set_AddSpaceBetweenFarEastAndAlpha(true);
paragraphFormat->set_AddSpaceBetweenFarEastAndDigit(true);
paragraphFormat->set_KeepTogether(true);
// The "Writeln" method ends the paragraph after appending text
// and then starts a new line, adding a new paragraph.
builder->Writeln(u"Hello world!");
ASSERT_TRUE(builder->get_CurrentParagraph()->get_IsEndOfDocument());

◆ get_IsEndOfHeaderFooter()

bool Aspose::Words::Paragraph::get_IsEndOfHeaderFooter ( )

True if this paragraph is the last paragraph in the HeaderFooter (main text story) of a Section; false otherwise.

Examples

Shows how to create a header and a footer.

auto doc = MakeObject<Document>();
// Create a header and append a paragraph to it. The text in that paragraph
// will appear at the top of every page of this section, above the main body text.
auto header = MakeObject<HeaderFooter>(doc, HeaderFooterType::HeaderPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(header);
SharedPtr<Paragraph> para = header->AppendParagraph(u"My header.");
ASSERT_TRUE(header->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
// Create a footer and append a paragraph to it. The text in that paragraph
// will appear at the bottom of every page of this section, below the main body text.
auto footer = MakeObject<HeaderFooter>(doc, HeaderFooterType::FooterPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(footer);
para = footer->AppendParagraph(u"My footer.");
ASSERT_FALSE(footer->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
ASPOSE_ASSERT_EQ(footer, para->get_ParentStory());
ASPOSE_ASSERT_EQ(footer->get_ParentSection(), para->get_ParentSection());
ASPOSE_ASSERT_EQ(footer->get_ParentSection(), header->get_ParentSection());
doc->Save(ArtifactsDir + u"HeaderFooter.Create.docx");

◆ get_IsEndOfSection()

bool Aspose::Words::Paragraph::get_IsEndOfSection ( )

True if this paragraph is the last paragraph in the Body (main text story) of a Section; false otherwise.

Examples

Shows how to insert the contents of one document to a bookmark in another document.

void InsertAtBookmark()
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->StartBookmark(u"InsertionPoint");
builder->Write(u"We will insert a document here: ");
builder->EndBookmark(u"InsertionPoint");
auto docToInsert = MakeObject<Document>();
builder = MakeObject<DocumentBuilder>(docToInsert);
builder->Write(u"Hello world!");
docToInsert->Save(ArtifactsDir + u"NodeImporter.InsertAtMergeField.docx");
SharedPtr<Bookmark> bookmark = doc->get_Range()->get_Bookmarks()->idx_get(u"InsertionPoint");
InsertDocument(bookmark->get_BookmarkStart()->get_ParentNode(), docToInsert);
ASSERT_EQ(String(u"We will insert a document here: ") + u"\rHello world!", doc->GetText().Trim());
}
static void InsertDocument(SharedPtr<Node> insertionDestination, SharedPtr<Document> docToInsert)
{
if (insertionDestination->get_NodeType() == NodeType::Paragraph || insertionDestination->get_NodeType() == NodeType::Table)
{
SharedPtr<CompositeNode> destinationParent = insertionDestination->get_ParentNode();
auto importer = MakeObject<NodeImporter>(docToInsert, insertionDestination->get_Document(), ImportFormatMode::KeepSourceFormatting);
// Loop through all block-level nodes in the section's body,
// then clone and insert every node that is not the last empty paragraph of a section.
for (const auto& srcSection : System::IterateOver(docToInsert->get_Sections()->LINQ_OfType<SharedPtr<Section>>()))
{
for (const auto& srcNode : System::IterateOver(srcSection->get_Body()))
{
if (srcNode->get_NodeType() == NodeType::Paragraph)
{
auto para = System::DynamicCast<Paragraph>(srcNode);
if (para->get_IsEndOfSection() && !para->get_HasChildNodes())
{
continue;
}
}
SharedPtr<Node> newNode = importer->ImportNode(srcNode, true);
destinationParent->InsertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
}
else
{
throw System::ArgumentException(u"The destination node should be either a paragraph or table.");
}
}

◆ get_IsFormatRevision()

bool Aspose::Words::Paragraph::get_IsFormatRevision ( )

Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled.

Examples

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

auto doc = MakeObject<Document>(MyDir + u"Format revision.docx");
// This paragraph is a "Format" revision, which occurs when we change the formatting of existing text
// while tracking revisions in Microsoft Word via "Review" -> "Track changes".
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_IsFormatRevision());

◆ get_IsInCell()

bool Aspose::Words::Paragraph::get_IsInCell ( )

True if this paragraph is an immediate child of Cell; false otherwise.

Examples

Shows how to set a table to stay together on the same page.

auto doc = MakeObject<Document>(MyDir + u"Table spanning two pages.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Enabling KeepWithNext for every paragraph in the table except for the
// last ones in the last row will prevent the table from splitting across multiple pages.
for (const auto& cell : System::IterateOver(table->GetChildNodes(NodeType::Cell, true)->LINQ_OfType<SharedPtr<Cell>>()))
{
for (const auto& para : System::IterateOver(cell->get_Paragraphs()->LINQ_OfType<SharedPtr<Paragraph>>()))
{
ASSERT_TRUE(para->get_IsInCell());
if (!(cell->get_ParentRow()->get_IsLastRow() && para->get_IsEndOfCell()))
{
para->get_ParagraphFormat()->set_KeepWithNext(true);
}
}
}
doc->Save(ArtifactsDir + u"Table.KeepTableTogether.docx");

◆ get_IsInsertRevision()

bool Aspose::Words::Paragraph::get_IsInsertRevision ( )

Returns true if this object was inserted in Microsoft Word while change tracking was enabled.

Examples

Shows how to work with revision paragraphs.

auto doc = MakeObject<Document>();
SharedPtr<Body> body = doc->get_FirstSection()->get_Body();
SharedPtr<Paragraph> para = body->get_FirstParagraph();
para->AppendChild(MakeObject<Run>(doc, u"Paragraph 1. "));
body->AppendParagraph(u"Paragraph 2. ");
body->AppendParagraph(u"Paragraph 3. ");
// The above paragraphs are not revisions.
// Paragraphs that we add after starting revision tracking will register as "Insert" revisions.
doc->StartTrackRevisions(u"John Doe", System::DateTime::get_Now());
para = body->AppendParagraph(u"Paragraph 4. ");
ASSERT_TRUE(para->get_IsInsertRevision());
// Paragraphs that we remove after starting revision tracking will register as "Delete" revisions.
SharedPtr<ParagraphCollection> paragraphs = body->get_Paragraphs();
ASSERT_EQ(4, paragraphs->get_Count());
para = paragraphs->idx_get(2);
para->Remove();
// Such paragraphs will remain until we either accept or reject the delete revision.
// Accepting the revision will remove the paragraph for good,
// and rejecting the revision will leave it in the document as if we never deleted it.
ASSERT_EQ(4, paragraphs->get_Count());
ASSERT_TRUE(para->get_IsDeleteRevision());
// Accept the revision, and then verify that the paragraph is gone.
doc->AcceptAllRevisions();
ASSERT_EQ(3, paragraphs->get_Count());
ASSERT_EQ(0, para->get_Count());
ASSERT_EQ(String(u"Paragraph 1. \r") + u"Paragraph 2. \r" + u"Paragraph 4.", doc->GetText().Trim());

◆ get_IsListItem()

bool Aspose::Words::Paragraph::get_IsListItem ( )

True when the paragraph is an item in a bulleted or numbered list in original revision.

Examples

Shows how to nest a list inside another list.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
// We can create nested lists by increasing the indent level.
// We can begin and end a list by using a document builder's "ListFormat" property.
// Each paragraph that we add between a list's start and the end will become an item in the list.
// Create an outline list for the headings.
SharedPtr<List> outlineList = doc->get_Lists()->Add(ListTemplate::OutlineNumbers);
builder->get_ListFormat()->set_List(outlineList);
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Heading1);
builder->Writeln(u"This is my Chapter 1");
// Create a numbered list.
SharedPtr<List> numberedList = doc->get_Lists()->Add(ListTemplate::NumberDefault);
builder->get_ListFormat()->set_List(numberedList);
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Normal);
builder->Writeln(u"Numbered list item 1.");
// Every paragraph that comprises a list will have this flag.
ASSERT_TRUE(builder->get_CurrentParagraph()->get_IsListItem());
ASSERT_TRUE(builder->get_ParagraphFormat()->get_IsListItem());
// Create a bulleted list.
SharedPtr<List> bulletedList = doc->get_Lists()->Add(ListTemplate::BulletDefault);
builder->get_ListFormat()->set_List(bulletedList);
builder->get_ParagraphFormat()->set_LeftIndent(72);
builder->Writeln(u"Bulleted list item 1.");
builder->Writeln(u"Bulleted list item 2.");
builder->get_ParagraphFormat()->ClearFormatting();
// Revert to the numbered list.
builder->get_ListFormat()->set_List(numberedList);
builder->Writeln(u"Numbered list item 2.");
builder->Writeln(u"Numbered list item 3.");
// Revert to the outline list.
builder->get_ListFormat()->set_List(outlineList);
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Heading1);
builder->Writeln(u"This is my Chapter 2");
builder->get_ParagraphFormat()->ClearFormatting();
builder->get_Document()->Save(ArtifactsDir + u"Lists.NestedLists.docx");

◆ get_IsMoveFromRevision()

bool Aspose::Words::Paragraph::get_IsMoveFromRevision ( )

Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.

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());

◆ get_IsMoveToRevision()

bool Aspose::Words::Paragraph::get_IsMoveToRevision ( )

Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.

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());

◆ get_ListFormat()

System::SharedPtr< Aspose::Words::Lists::ListFormat > Aspose::Words::Paragraph::get_ListFormat ( )

Provides access to the list formatting properties of the paragraph.

Examples

Shows how to output all paragraphs in a document that are list items.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->get_ListFormat()->ApplyNumberDefault();
builder->Writeln(u"Numbered list item 1");
builder->Writeln(u"Numbered list item 2");
builder->Writeln(u"Numbered list item 3");
builder->get_ListFormat()->RemoveNumbers();
builder->get_ListFormat()->ApplyBulletDefault();
builder->Writeln(u"Bulleted list item 1");
builder->Writeln(u"Bulleted list item 2");
builder->Writeln(u"Bulleted list item 3");
builder->get_ListFormat()->RemoveNumbers();
SharedPtr<NodeCollection> paras = doc->GetChildNodes(NodeType::Paragraph, true);
for (auto para : System::IterateOver(
paras->LINQ_OfType<SharedPtr<Paragraph>>()->LINQ_Where([](SharedPtr<Paragraph> p) { return p->get_ListFormat()->get_IsListItem(); })))
{
std::cout << String::Format(u"This paragraph belongs to list ID# {0}, number style \"{1}\"", para->get_ListFormat()->get_List()->get_ListId(),
para->get_ListFormat()->get_ListLevel()->get_NumberStyle())
<< std::endl;
std::cout << "\t\"" << para->GetText().Trim() << "\"" << std::endl;
}

◆ get_ListLabel()

System::SharedPtr< Aspose::Words::Lists::ListLabel > Aspose::Words::Paragraph::get_ListLabel ( )

Gets a ListLabel object that provides access to list numbering value and formatting for this paragraph.

Examples

Shows how to extract the list labels of all paragraphs that are list items.

auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");
doc->UpdateListLabels();
SharedPtr<NodeCollection> paras = doc->GetChildNodes(NodeType::Paragraph, true);
// Find if we have the paragraph list. In our document, our list uses plain Arabic numbers,
// which start at three and ends at six.
for (auto paragraph : System::IterateOver(
paras->LINQ_OfType<SharedPtr<Paragraph>>()->LINQ_Where([](SharedPtr<Paragraph> p) { return p->get_ListFormat()->get_IsListItem(); })))
{
std::cout << "List item paragraph #" << paras->IndexOf(paragraph) << std::endl;
// This is the text we get when getting when we output this node to text format.
// This text output will omit list labels. Trim any paragraph formatting characters.
String paragraphText = paragraph->ToString(SaveFormat::Text).Trim();
std::cout << "\tExported Text: " << paragraphText << std::endl;
SharedPtr<ListLabel> label = paragraph->get_ListLabel();
// This gets the position of the paragraph in the current level of the list. If we have a list with multiple levels,
// this will tell us what position it is on that level.
std::cout << "\tNumerical Id: " << label->get_LabelValue() << std::endl;
// Combine them together to include the list label with the text in the output.
std::cout << "\tList label combined with text: " << label->get_LabelString() << " " << paragraphText << std::endl;
}

◆ get_NodeType()

Aspose::Words::NodeType Aspose::Words::Paragraph::get_NodeType ( ) const
overridevirtual

Returns NodeType.Paragraph.

Examples

Shows how to traverse a composite node's tree of child nodes.

void RecurseChildren()
{
auto doc = MakeObject<Document>(MyDir + u"Paragraphs.docx");
// Any node that can contain child nodes, such as the document itself, is composite.
ASSERT_TRUE(doc->get_IsComposite());
// Invoke the recursive function that will go through and print all the child nodes of a composite node.
TraverseAllNodes(doc, 0);
}
void TraverseAllNodes(SharedPtr<CompositeNode> parentNode, int depth)
{
for (SharedPtr<Node> childNode = parentNode->get_FirstChild(); childNode != nullptr; childNode = childNode->get_NextSibling())
{
std::cout << (String(u'\t', depth)) << Node::NodeTypeToString(childNode->get_NodeType());
// Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node.
if (childNode->get_IsComposite())
{
std::cout << std::endl;
TraverseAllNodes(System::DynamicCast<CompositeNode>(childNode), depth + 1);
}
else if (System::ObjectExt::Is<Inline>(childNode))
{
std::cout << " - \"" << childNode->GetText().Trim() << "\"" << std::endl;
}
else
{
std::cout << std::endl;
}
}
}

Implements Aspose::Words::Node.

◆ get_ParagraphBreakFont()

System::SharedPtr< Aspose::Words::Font > Aspose::Words::Paragraph::get_ParagraphBreakFont ( )

Provides access to the font formatting of the paragraph break character.

Examples

Shows how to use a DocumentVisitor implementation to remove all hidden content from a document.

void RemoveHiddenContentFromDocument()
{
auto doc = MakeObject<Document>(MyDir + u"Hidden content.docx");
auto hiddenContentRemover = MakeObject<ExFont::RemoveHiddenContentVisitor>();
// Below are three types of fields which can accept a document visitor,
// which will allow it to visit the accepting node, and then traverse its child nodes in a depth-first manner.
// 1 - Paragraph node:
auto para = System::DynamicCast<Paragraph>(doc->GetChild(NodeType::Paragraph, 4, true));
para->Accept(hiddenContentRemover);
// 2 - Table node:
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
table->Accept(hiddenContentRemover);
// 3 - Document node:
doc->Accept(hiddenContentRemover);
doc->Save(ArtifactsDir + u"Font.RemoveHiddenContentFromDocument.docx");
}
class RemoveHiddenContentVisitor : public DocumentVisitor
{
public:
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
if (fieldStart->get_Font()->get_Hidden())
{
fieldStart->Remove();
}
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
if (fieldEnd->get_Font()->get_Hidden())
{
fieldEnd->Remove();
}
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
if (fieldSeparator->get_Font()->get_Hidden())
{
fieldSeparator->Remove();
}
}
VisitorAction VisitRun(SharedPtr<Run> run) override
{
if (run->get_Font()->get_Hidden())
{
run->Remove();
}
}
VisitorAction VisitParagraphStart(SharedPtr<Paragraph> paragraph) override
{
if (paragraph->get_ParagraphBreakFont()->get_Hidden())
{
paragraph->Remove();
}
}
VisitorAction VisitFormField(SharedPtr<FormField> formField) override
{
if (formField->get_Font()->get_Hidden())
{
formField->Remove();
}
}
VisitorAction VisitGroupShapeStart(SharedPtr<GroupShape> groupShape) override
{
if (groupShape->get_Font()->get_Hidden())
{
groupShape->Remove();
}
}
VisitorAction VisitShapeStart(SharedPtr<Shape> shape) override
{
if (shape->get_Font()->get_Hidden())
{
shape->Remove();
}
}
VisitorAction VisitCommentStart(SharedPtr<Comment> comment) override
{
if (comment->get_Font()->get_Hidden())
{
comment->Remove();
}
}
VisitorAction VisitFootnoteStart(SharedPtr<Footnote> footnote) override
{
if (footnote->get_Font()->get_Hidden())
{
footnote->Remove();
}
}
VisitorAction VisitSpecialChar(SharedPtr<SpecialChar> specialChar) override
{
if (specialChar->get_Font()->get_Hidden())
{
specialChar->Remove();
}
}
VisitorAction VisitTableEnd(SharedPtr<Table> table) override
{
// The content inside table cells may have the hidden content flag, but the tables themselves cannot.
// If this table had nothing but hidden content, this visitor would have removed all of it,
// and there would be no child nodes left.
// Thus, we can also treat the table itself as hidden content and remove it.
// Tables which are empty but do not have hidden content will have cells with empty paragraphs inside,
// which this visitor will not remove.
if (!table->get_HasChildNodes())
{
table->Remove();
}
}
VisitorAction VisitCellEnd(SharedPtr<Cell> cell) override
{
if (!cell->get_HasChildNodes() && cell->get_ParentNode() != nullptr)
{
cell->Remove();
}
}
VisitorAction VisitRowEnd(SharedPtr<Row> row) override
{
if (!row->get_HasChildNodes() && row->get_ParentNode() != nullptr)
{
row->Remove();
}
}
};

◆ get_ParagraphFormat()

System::SharedPtr< Aspose::Words::ParagraphFormat > Aspose::Words::Paragraph::get_ParagraphFormat ( )

Provides access to the paragraph formatting properties.

Examples

Shows how to construct an Aspose.Words document by hand.

auto doc = MakeObject<Document>();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc->RemoveAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
auto section = MakeObject<Section>(doc);
doc->AppendChild(section);
// Set some page setup properties for the section.
section->get_PageSetup()->set_SectionStart(SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(PaperSize::Letter);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
auto para = MakeObject<Paragraph>(doc);
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
body->AppendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
auto run = MakeObject<Run>(doc);
run->set_Text(u"Hello World!");
run->get_Font()->set_Color(System::Drawing::Color::get_Red());
para->AppendChild(run);
ASSERT_EQ(u"Hello World!", doc->GetText().Trim());
doc->Save(ArtifactsDir + u"Section.CreateManually.docx");

◆ get_ParentSection()

System::SharedPtr< Aspose::Words::Section > Aspose::Words::Paragraph::get_ParentSection ( )

Retrieves the parent Section of the paragraph.

Examples

Shows how to create a header and a footer.

auto doc = MakeObject<Document>();
// Create a header and append a paragraph to it. The text in that paragraph
// will appear at the top of every page of this section, above the main body text.
auto header = MakeObject<HeaderFooter>(doc, HeaderFooterType::HeaderPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(header);
SharedPtr<Paragraph> para = header->AppendParagraph(u"My header.");
ASSERT_TRUE(header->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
// Create a footer and append a paragraph to it. The text in that paragraph
// will appear at the bottom of every page of this section, below the main body text.
auto footer = MakeObject<HeaderFooter>(doc, HeaderFooterType::FooterPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(footer);
para = footer->AppendParagraph(u"My footer.");
ASSERT_FALSE(footer->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
ASPOSE_ASSERT_EQ(footer, para->get_ParentStory());
ASPOSE_ASSERT_EQ(footer->get_ParentSection(), para->get_ParentSection());
ASPOSE_ASSERT_EQ(footer->get_ParentSection(), header->get_ParentSection());
doc->Save(ArtifactsDir + u"HeaderFooter.Create.docx");

◆ get_ParentStory()

System::SharedPtr< Aspose::Words::Story > Aspose::Words::Paragraph::get_ParentStory ( )

Retrieves the parent section-level story that can be Body or HeaderFooter.

Examples

Shows how to create a header and a footer.

auto doc = MakeObject<Document>();
// Create a header and append a paragraph to it. The text in that paragraph
// will appear at the top of every page of this section, above the main body text.
auto header = MakeObject<HeaderFooter>(doc, HeaderFooterType::HeaderPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(header);
SharedPtr<Paragraph> para = header->AppendParagraph(u"My header.");
ASSERT_TRUE(header->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
// Create a footer and append a paragraph to it. The text in that paragraph
// will appear at the bottom of every page of this section, below the main body text.
auto footer = MakeObject<HeaderFooter>(doc, HeaderFooterType::FooterPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(footer);
para = footer->AppendParagraph(u"My footer.");
ASSERT_FALSE(footer->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
ASPOSE_ASSERT_EQ(footer, para->get_ParentStory());
ASPOSE_ASSERT_EQ(footer->get_ParentSection(), para->get_ParentSection());
ASPOSE_ASSERT_EQ(footer->get_ParentSection(), header->get_ParentSection());
doc->Save(ArtifactsDir + u"HeaderFooter.Create.docx");

◆ get_Runs()

System::SharedPtr< Aspose::Words::RunCollection > Aspose::Words::Paragraph::get_Runs ( )

Provides access to the typed collection of pieces of text inside the paragraph.

Examples

Shows how to determine the revision type of an inline node.

auto doc = MakeObject<Document>(MyDir + u"Revision runs.docx");
// When we edit the document while the "Track Changes" option, found in via Review -> Tracking,
// is turned on in Microsoft Word, the changes we apply count as revisions.
// When editing a document using Aspose.Words, we can begin tracking revisions by
// invoking the document's "StartTrackRevisions" method and stop tracking by using the "StopTrackRevisions" method.
// We can either accept revisions to assimilate them into the document
// or reject them to change the proposed change effectively.
ASSERT_EQ(6, doc->get_Revisions()->get_Count());
// The parent node of a revision is the run that the revision concerns. A Run is an Inline node.
auto run = System::DynamicCast<Run>(doc->get_Revisions()->idx_get(0)->get_ParentNode());
SharedPtr<Paragraph> firstParagraph = run->get_ParentParagraph();
SharedPtr<RunCollection> runs = firstParagraph->get_Runs();
ASSERT_EQ(6, runs->ToArray()->get_Length());
// Below are five types of revisions that can flag an Inline node.
// 1 - An "insert" revision:
// This revision occurs when we insert text while tracking changes.
ASSERT_TRUE(runs->idx_get(2)->get_IsInsertRevision());
// 2 - A "format" revision:
// This revision occurs when we change the formatting of text while tracking changes.
ASSERT_TRUE(runs->idx_get(2)->get_IsFormatRevision());
// 3 - A "move from" revision:
// When we highlight text in Microsoft Word, and then drag it to a different place in the document
// while tracking changes, two revisions appear.
// The "move from" revision is a copy of the text originally before we moved it.
ASSERT_TRUE(runs->idx_get(4)->get_IsMoveFromRevision());
// 4 - A "move to" revision:
// The "move to" revision is the text that we moved in its new position in the document.
// "Move from" and "move to" revisions appear in pairs for every move revision we carry out.
// Accepting a move revision deletes the "move from" revision and its text,
// and keeps the text from the "move to" revision.
// Rejecting a move revision conversely keeps the "move from" revision and deletes the "move to" revision.
ASSERT_TRUE(runs->idx_get(1)->get_IsMoveToRevision());
// 5 - A "delete" revision:
// This revision occurs when we delete text while tracking changes. When we delete text like this,
// it will stay in the document as a revision until we either accept the revision,
// which will delete the text for good, or reject the revision, which will keep the text we deleted where it was.
ASSERT_TRUE(runs->idx_get(5)->get_IsDeleteRevision());

◆ GetEffectiveTabStops()

System::ArrayPtr< System::SharedPtr< Aspose::Words::TabStop > > Aspose::Words::Paragraph::GetEffectiveTabStops ( )

Returns array of all tab stops applied to this paragraph, including applied indirectly by styles or lists.

Examples

Shows how to set custom tab stops for a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// If we are in a paragraph with no tab stops in this collection,
// the cursor will jump 36 points each time we press the Tab key in Microsoft Word.
ASSERT_EQ(0, doc->get_FirstSection()->get_Body()->get_FirstParagraph()->GetEffectiveTabStops()->get_Length());
// We can add custom tab stops in Microsoft Word if we enable the ruler via the "View" tab.
// Each unit on this ruler is two default tab stops, which is 72 points.
// We can add custom tab stops programmatically like this.
SharedPtr<TabStopCollection> tabStops = doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_ParagraphFormat()->get_TabStops();
tabStops->Add(72, TabAlignment::Left, TabLeader::Dots);
tabStops->Add(360, TabAlignment::Right, TabLeader::Line);
// We can see these tab stops in Microsoft Word by enabling the ruler via "View" -> "Show" -> "Ruler".
ASSERT_EQ(3, para->GetEffectiveTabStops()->get_Length());
// Any tab characters we add will make use of the tab stops on the ruler and may,
// depending on the tab leader's value, leave a line between the tab departure and arrival destinations.
para->AppendChild(MakeObject<Run>(doc, u"\tTab 1\tTab 2\tTab 3"));
doc->Save(ArtifactsDir + u"Paragraph.TabStops.docx");

◆ GetText()

System::String Aspose::Words::Paragraph::GetText ( )
overridevirtual

Gets the text of this paragraph including the end of paragraph character.

The text of all child nodes is concatenated and the end of paragraph character is appended as follows:

The returned string includes all control and special characters as described in ControlChar.

Examples

Shows how to add, update and delete child nodes in a CompositeNode's collection of children.

auto doc = MakeObject<Document>();
// An empty document, by default, has one paragraph.
ASSERT_EQ(1, doc->get_FirstSection()->get_Body()->get_Paragraphs()->get_Count());
// Composite nodes such as our paragraph can contain other composite and inline nodes as children.
SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
auto paragraphText = MakeObject<Run>(doc, u"Initial text. ");
paragraph->AppendChild(paragraphText);
// Create three more run nodes.
auto run1 = MakeObject<Run>(doc, u"Run 1. ");
auto run2 = MakeObject<Run>(doc, u"Run 2. ");
auto run3 = MakeObject<Run>(doc, u"Run 3. ");
// The document body will not display these runs until we insert them into a composite node
// that itself is a part of the document's node tree, as we did with the first run.
// We can determine where the text contents of nodes that we insert
// appears in the document by specifying an insertion location relative to another node in the paragraph.
ASSERT_EQ(u"Initial text.", paragraph->GetText().Trim());
// Insert the second run into the paragraph in front of the initial run.
paragraph->InsertBefore(run2, paragraphText);
ASSERT_EQ(u"Run 2. Initial text.", paragraph->GetText().Trim());
// Insert the third run after the initial run.
paragraph->InsertAfter(run3, paragraphText);
ASSERT_EQ(u"Run 2. Initial text. Run 3.", paragraph->GetText().Trim());
// Insert the first run to the start of the paragraph's child nodes collection.
paragraph->PrependChild(run1);
ASSERT_EQ(u"Run 1. Run 2. Initial text. Run 3.", paragraph->GetText().Trim());
ASSERT_EQ(4, paragraph->GetChildNodes(NodeType::Any, true)->get_Count());
// We can modify the contents of the run by editing and deleting existing child nodes.
(System::DynamicCast<Run>(paragraph->GetChildNodes(NodeType::Run, true)->idx_get(1)))->set_Text(u"Updated run 2. ");
paragraph->GetChildNodes(NodeType::Run, true)->Remove(paragraphText);
ASSERT_EQ(u"Run 1. Updated run 2. Run 3.", paragraph->GetText().Trim());
ASSERT_EQ(3, paragraph->GetChildNodes(NodeType::Any, true)->get_Count());

Reimplemented from Aspose::Words::CompositeNode.

◆ GetType()

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

Reimplemented from Aspose::Words::CompositeNode.

◆ InsertField() [1/3]

System::SharedPtr< Aspose::Words::Fields::Field > Aspose::Words::Paragraph::InsertField ( Aspose::Words::Fields::FieldType  fieldType,
bool  updateField,
const System::SharedPtr< Aspose::Words::Node > &  refNode,
bool  isAfter 
)

Inserts a field into this paragraph.

Parameters
fieldTypeThe type of the field to insert.
updateFieldSpecifies whether to update the field immediately.
refNodeReference node inside this paragraph (if refNode is null, then appends to the end of the paragraph).
isAfterWhether to insert the field after or before reference node.
Returns
A Field object that represents the inserted field.
Examples

Shows various ways of adding fields to a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Below are three ways of inserting a field into a paragraph.
// 1 - Insert an AUTHOR field into a paragraph after one of the paragraph's child nodes:
auto run = MakeObject<Run>(doc);
run->set_Text(u"This run was written by ");
para->AppendChild(run);
doc->get_BuiltInDocumentProperties()->idx_get(u"Author")->set_Value(System::ObjectExt::Box<String>(u"John Doe"));
para->InsertField(FieldType::FieldAuthor, true, run, true);
// 2 - Insert a QUOTE field after one of the paragraph's child nodes:
run = MakeObject<Run>(doc);
run->set_Text(u".");
para->AppendChild(run);
SharedPtr<Field> field = para->InsertField(u" QUOTE \" Real value\" ", run, true);
// 3 - Insert a QUOTE field before one of the paragraph's child nodes,
// and get it to display a placeholder value:
para->InsertField(u" QUOTE \" Real value.\"", u" Placeholder value.", field->get_Start(), false);
ASSERT_EQ(u" Placeholder value.", doc->get_Range()->get_Fields()->idx_get(1)->get_Result());
// This field will display its placeholder value until we update it.
doc->UpdateFields();
ASSERT_EQ(u" Real value.", doc->get_Range()->get_Fields()->idx_get(1)->get_Result());
doc->Save(ArtifactsDir + u"Paragraph.InsertField.docx");

◆ InsertField() [2/3]

System::SharedPtr< Aspose::Words::Fields::Field > Aspose::Words::Paragraph::InsertField ( const System::String fieldCode,
const System::SharedPtr< Aspose::Words::Node > &  refNode,
bool  isAfter 
)

Inserts a field into this paragraph.

Parameters
fieldCodeThe field code to insert (without curly braces).
refNodeReference node inside this paragraph (if refNode is null, then appends to the end of the paragraph).
isAfterWhether to insert the field after or before reference node.
Returns
A Field object that represents the inserted field.
Examples

Shows various ways of adding fields to a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Below are three ways of inserting a field into a paragraph.
// 1 - Insert an AUTHOR field into a paragraph after one of the paragraph's child nodes:
auto run = MakeObject<Run>(doc);
run->set_Text(u"This run was written by ");
para->AppendChild(run);
doc->get_BuiltInDocumentProperties()->idx_get(u"Author")->set_Value(System::ObjectExt::Box<String>(u"John Doe"));
para->InsertField(FieldType::FieldAuthor, true, run, true);
// 2 - Insert a QUOTE field after one of the paragraph's child nodes:
run = MakeObject<Run>(doc);
run->set_Text(u".");
para->AppendChild(run);
SharedPtr<Field> field = para->InsertField(u" QUOTE \" Real value\" ", run, true);
// 3 - Insert a QUOTE field before one of the paragraph's child nodes,
// and get it to display a placeholder value:
para->InsertField(u" QUOTE \" Real value.\"", u" Placeholder value.", field->get_Start(), false);
ASSERT_EQ(u" Placeholder value.", doc->get_Range()->get_Fields()->idx_get(1)->get_Result());
// This field will display its placeholder value until we update it.
doc->UpdateFields();
ASSERT_EQ(u" Real value.", doc->get_Range()->get_Fields()->idx_get(1)->get_Result());
doc->Save(ArtifactsDir + u"Paragraph.InsertField.docx");

◆ InsertField() [3/3]

System::SharedPtr< Aspose::Words::Fields::Field > Aspose::Words::Paragraph::InsertField ( const System::String fieldCode,
const System::String fieldValue,
const System::SharedPtr< Aspose::Words::Node > &  refNode,
bool  isAfter 
)

Inserts a field into this paragraph.

Parameters
fieldCodeThe field code to insert (without curly braces).
fieldValueThe field value to insert. Pass null for fields that do not have a value.
refNodeReference node inside this paragraph (if refNode is null, then appends to the end of the paragraph).
isAfterWhether to insert the field after or before reference node.
Returns
A Field object that represents the inserted field.
Examples

Shows various ways of adding fields to a paragraph.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Below are three ways of inserting a field into a paragraph.
// 1 - Insert an AUTHOR field into a paragraph after one of the paragraph's child nodes:
auto run = MakeObject<Run>(doc);
run->set_Text(u"This run was written by ");
para->AppendChild(run);
doc->get_BuiltInDocumentProperties()->idx_get(u"Author")->set_Value(System::ObjectExt::Box<String>(u"John Doe"));
para->InsertField(FieldType::FieldAuthor, true, run, true);
// 2 - Insert a QUOTE field after one of the paragraph's child nodes:
run = MakeObject<Run>(doc);
run->set_Text(u".");
para->AppendChild(run);
SharedPtr<Field> field = para->InsertField(u" QUOTE \" Real value\" ", run, true);
// 3 - Insert a QUOTE field before one of the paragraph's child nodes,
// and get it to display a placeholder value:
para->InsertField(u" QUOTE \" Real value.\"", u" Placeholder value.", field->get_Start(), false);
ASSERT_EQ(u" Placeholder value.", doc->get_Range()->get_Fields()->idx_get(1)->get_Result());
// This field will display its placeholder value until we update it.
doc->UpdateFields();
ASSERT_EQ(u" Real value.", doc->get_Range()->get_Fields()->idx_get(1)->get_Result());
doc->Save(ArtifactsDir + u"Paragraph.InsertField.docx");

◆ Is()

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

Reimplemented from Aspose::Words::CompositeNode.

◆ JoinRunsWithSameFormatting()

int32_t Aspose::Words::Paragraph::JoinRunsWithSameFormatting ( )

Joins runs with the same formatting in the paragraph.

Returns
Number of joins performed. When N adjacent runs are being joined they count as N - 1 joins.
Examples

Shows how to simplify paragraphs by merging superfluous runs.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Insert four runs of text into the paragraph.
builder->Write(u"Run 1. ");
builder->Write(u"Run 2. ");
builder->Write(u"Run 3. ");
builder->Write(u"Run 4. ");
// If we open this document in Microsoft Word, the paragraph will look like one seamless text body.
// However, it will consist of four separate runs with the same formatting. Fragmented paragraphs like this
// may occur when we manually edit parts of one paragraph many times in Microsoft Word.
SharedPtr<Paragraph> para = builder->get_CurrentParagraph();
ASSERT_EQ(4, para->get_Runs()->get_Count());
// Change the style of the last run to set it apart from the first three.
para->get_Runs()->idx_get(3)->get_Font()->set_StyleIdentifier(StyleIdentifier::Emphasis);
// We can run the "JoinRunsWithSameFormatting" method to optimize the document's contents
// by merging similar runs into one, reducing their overall count.
// This method also returns the number of runs that this method merged.
// These two merges occurred to combine Runs #1, #2, and #3,
// while leaving out Run #4 because it has an incompatible style.
ASSERT_EQ(2, para->JoinRunsWithSameFormatting());
// The number of runs left will equal the original count
// minus the number of run merges that the "JoinRunsWithSameFormatting" method carried out.
ASSERT_EQ(2, para->get_Runs()->get_Count());
ASSERT_EQ(u"Run 1. Run 2. Run 3. ", para->get_Runs()->idx_get(0)->get_Text());
ASSERT_EQ(u"Run 4. ", para->get_Runs()->idx_get(1)->get_Text());

◆ Type()

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