Detailed Description

Base class for elements that contain block-level nodes Paragraph and Table.

Text of a Word document is said to consist of several stories. The main text is stored in the main text story represented by Body, each header and footer is stored in a separate story represented by HeaderFooter.

Examples

Shows how to clear a body of inline shapes.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Use a DocumentBuilder to insert a shape
// This is an inline shape, which has a parent Paragraph, which is in turn a child of the Body
builder->InsertShape(Aspose::Words::Drawing::ShapeType::Cube, 100.0, 100.0);
ASSERT_EQ(1, doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());
// We can delete all such shapes from the Body, affecting all child Paragraphs
ASSERT_EQ(Aspose::Words::StoryType::MainText, doc->get_FirstSection()->get_Body()->get_StoryType());
doc->get_FirstSection()->get_Body()->DeleteShapes();
ASSERT_EQ(0, doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());

#include <Aspose.Words.Cpp/Model/Sections/Story.h>

+ Inheritance diagram for Aspose::Words::Story:

Public Member Functions

SharedPtr< ParagraphAppendParagraph (String text)
 A shortcut method that creates a Paragraph object with optional text and appends it to the end of this object. More...
 
void DeleteShapes ()
 Deletes all shapes from the text of this story. More...
 
SharedPtr< Paragraphget_FirstParagraph ()
 Gets the first paragraph in the story. More...
 
SharedPtr< Paragraphget_LastParagraph ()
 Gets the last paragraph in the story. More...
 
SharedPtr< ParagraphCollectionget_Paragraphs ()
 Gets a collection of paragraphs that are immediate children of the story. More...
 
StoryType get_StoryType () const
 Gets the type of this story. More...
 
SharedPtr< TableCollectionget_Tables ()
 Gets a collection of tables that are immediate children of the story. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
- Public Member Functions inherited from CompositeNode
SharedPtr< NodeAppendChild (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...
 
SharedPtr< CompositeNodeget_Container () override
 
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< NodeGetCurrentNode () override
 
SharedPtr< IEnumerator< SharedPtr< Node > > > GetEnumerator () override
 Provides support for the for each style iteration over the child nodes of this node. More...
 
SharedPtr< NodeGetNextMatchingNode (SharedPtr< Node > curNode) override
 
String GetText () override
 Gets the text of this node and of all its children. More...
 
int32_t IndexOf (SharedPtr< Node > child)
 Returns the index of the specified child node in the child node array. More...
 
SharedPtr< NodeInsertAfter (SharedPtr< Node > newChild, SharedPtr< Node > refChild)
 Inserts the specified node immediately after the specified reference node. More...
 
SharedPtr< NodeInsertBefore (SharedPtr< Node > newChild, SharedPtr< Node > refChild)
 Inserts the specified node immediately before the specified reference node. More...
 
SharedPtr< NodePrependChild (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 (SharedPtr< Node > oldChild)
 Removes the specified child node. More...
 
void RemoveSmartTags ()
 Removes all SmartTag descendant nodes of the current node. More...
 
SharedPtr< NodeListSelectNodes (String xpath)
 Selects a list of nodes matching the XPath expression. More...
 
SharedPtr< NodeSelectSingleNode (String xpath)
 Selects the first Node that matches the XPath expression. More...
 
- 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...
 
virtual SharedPtr< DocumentBaseget_Document () const
 Gets the document to which this node belongs. 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 ()
 
SharedPtr< NodeNextPreOrder (SharedPtr< Node > rootNode)
 Gets next node according to the pre-order tree traversal algorithm. More...
 
SharedPtr< NodePreviousPreOrder (SharedPtr< Node > rootNode)
 Gets the previous node according to the pre-order tree traversal algorithm. More...
 
void Remove ()
 Removes itself from the parent. More...
 
String ToString (SaveFormat saveFormat)
 Exports the content of the node into a string in the specified format. More...
 
String ToString (SharedPtr< SaveOptions > saveOptions)
 Exports the content of the node into a string using the specified save options. More...
 
- Public Member Functions inherited from Object
ASPOSECPP_SHARED_API Object ()
 
ASPOSECPP_SHARED_API Object (Object const &x)
 
virtual ASPOSECPP_SHARED_API ~Object ()
 
virtual ASPOSECPP_SHARED_API bool Equals (ptr obj)
 
Detail::SmartPtrCounter * GetCounter ()
 
virtual ASPOSECPP_SHARED_API int GetHashCode () const
 
ASPOSECPP_SHARED_API void Lock ()
 
virtual ASPOSECPP_SHARED_API ptr MemberwiseClone () const
 
Objectoperator= (Object const &x)
 
bool ReferenceEquals (String const &str, std::nullptr_t)
 
bool ReferenceEquals (String const &str1, String const &str2)
 
int RemovedSharedRefs (int count)
 
virtual ASPOSECPP_SHARED_API void SetTemplateWeakPtr (unsigned int argument)
 
int SharedCount () const
 
ObjectSharedRefAdded ()
 
int SharedRefRemovedSafe ()
 
virtual ASPOSECPP_SHARED_API String ToString () const
 
ASPOSECPP_SHARED_API void Unlock ()
 
Detail::SmartPtrCounter * WeakRefAdded ()
 
void WeakRefRemoved ()
 
- Public Member Functions inherited from System::Collections::Generic::IEnumerable< System::SharedPtr< Aspose::Words::Node > >
EnumeratorBasedIterator< T, IEnumerator< T > > begin () noexcept
 
EnumeratorBasedIterator< T, IEnumerator< T > > cbegin () noexcept
 
EnumeratorBasedIterator< T, IEnumerator< T > > cend () noexcept
 
EnumeratorBasedIterator< T, IEnumerator< T > > end () noexcept
 
bool LINQ_All (std::function< bool(T)> predicate)
 
bool LINQ_Any ()
 
bool LINQ_Any (std::function< bool(T)> predicate)
 
SharedPtr< IEnumerable< ResultType > > LINQ_Cast ()
 
SharedPtr< IEnumerable< Result > > LINQ_Cast ()
 
bool LINQ_Contains (T value)
 
int LINQ_Count ()
 
int LINQ_Count (const Func< T, bool > &predicate)
 
LINQ_ElementAt (int index)
 
LINQ_First ()
 
LINQ_First (const Func< T, bool > &predicate)
 
LINQ_FirstOrDefault ()
 
LINQ_FirstOrDefault (std::function< bool(T)> predicate)
 
LINQ_Last ()
 
LINQ_LastOrDefault ()
 
SharedPtr< IEnumerable< ResultType > > LINQ_OfType ()
 
SharedPtr< IEnumerable< Result > > LINQ_OfType ()
 
SharedPtr< IEnumerable< Source > > LINQ_OrderBy (const Func< Source, Key > &keySelector)
 
SharedPtr< IEnumerable< T > > LINQ_OrderBy (const Func< T, Key > &keySelector)
 
SharedPtr< IEnumerable< Result > > LINQ_Select (const Func< Source, Result > &selector)
 
SharedPtr< IEnumerable< ResultType > > LINQ_Select (const Func< T, ResultType > &selector)
 
ArrayPtr< T > LINQ_ToArray ()
 
SharedPtr< List< T > > LINQ_ToList ()
 
SharedPtr< IEnumerable< T > > LINQ_Where (std::function< bool(T)> predicate)
 

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 ()
 
- Static Public Member Functions inherited from Object
bool Equals (double const &objA, double const &objB)
 
bool Equals (float const &objA, float const &objB)
 
static std::enable_if<!IsSmartPtr< T1 >::value &&!IsSmartPtr< T2 >::value, bool >::type Equals (T1 const &objA, T2 const &objB)
 
static std::enable_if< IsSmartPtr< T1 >::value &&IsSmartPtr< T2 >::value, bool >::type Equals (T1 const &objA, T2 const &objB)
 
static bool ReferenceEquals (ptr const &objA, ptr const &objB)
 
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals (T const &objA, std::nullptr_t)
 
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals (T const &objA, T const &objB)
 
static const TypeInfoType ()
 

Additional Inherited Members

- Public Types inherited from Object
typedef SmartPtr< Objectptr
 
typedef System::Details::SharedMembersType shared_members_type
 
- Public Types inherited from System::Collections::Generic::IEnumerable< System::SharedPtr< Aspose::Words::Node > >
typedef IEnumerator< T > IEnumeratorType
 
typedef T ValueType
 

Member Function Documentation

◆ AppendParagraph()

System::SharedPtr<Aspose::Words::Paragraph> Aspose::Words::Story::AppendParagraph ( System::String  text)

A shortcut method that creates a Paragraph object with optional text and appends it to the end of this object.

Parameters
textThe text for the paragraph. Can be null or empty string.
Returns
The newly created and appended paragraph.
Examples

Creates a header and footer using the document object model and insert them into a section.

auto doc = MakeObject<Document>();
auto header = MakeObject<HeaderFooter>(doc, Aspose::Words::HeaderFooterType::HeaderPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(header);
// Add a paragraph with text to the footer
SharedPtr<Paragraph> para = header->AppendParagraph(u"My header");
ASSERT_TRUE(header->get_IsHeader());
ASSERT_TRUE(para->get_IsEndOfHeaderFooter());
auto footer = MakeObject<HeaderFooter>(doc, Aspose::Words::HeaderFooterType::FooterPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(footer);
// Add a paragraph with text to the 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.HeaderFooterCreate.docx");

◆ DeleteShapes()

void Aspose::Words::Story::DeleteShapes ( )

Deletes all shapes from the text of this story.

Examples

Shows how to clear a body of inline shapes.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Use a DocumentBuilder to insert a shape
// This is an inline shape, which has a parent Paragraph, which is in turn a child of the Body
builder->InsertShape(Aspose::Words::Drawing::ShapeType::Cube, 100.0, 100.0);
ASSERT_EQ(1, doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());
// We can delete all such shapes from the Body, affecting all child Paragraphs
ASSERT_EQ(Aspose::Words::StoryType::MainText, doc->get_FirstSection()->get_Body()->get_StoryType());
doc->get_FirstSection()->get_Body()->DeleteShapes();
ASSERT_EQ(0, doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());

◆ get_FirstParagraph()

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

Gets the first paragraph in the story.

Examples

Shows how to add a formatted run of text to a document using the object model.

auto doc = MakeObject<Document>();
// Create a new run of text
auto run = MakeObject<Run>(doc, u"Hello");
// Specify character formatting for the run of text
SharedPtr<Aspose::Words::Font> f = run->get_Font();
f->set_Name(u"Courier New");
f->set_Size(36);
f->set_HighlightColor(System::Drawing::Color::get_Yellow());
// Append the run of text to the end of the first paragraph
// in the body of the first section of the document
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(run);

Shows how to create a textbox with some text and different formatting options in a new document.

auto doc = MakeObject<Document>();
// Create a new shape of type TextBox
auto textBox = MakeObject<Shape>(doc, Aspose::Words::Drawing::ShapeType::TextBox);
// Set some settings of the textbox itself
// Set the wrap of the textbox to inline
// Set the horizontal and vertical alignment of the text inside the shape
textBox->set_HorizontalAlignment(Aspose::Words::Drawing::HorizontalAlignment::Center);
textBox->set_VerticalAlignment(Aspose::Words::Drawing::VerticalAlignment::Top);
// Set the textbox height and width
textBox->set_Height(50);
textBox->set_Width(200);
// Set the textbox in front of other shapes with a lower ZOrder
textBox->set_ZOrder(2);
// Let's create a new paragraph for the textbox manually and align it in the center
// Make sure we add the new nodes to the textbox as well
textBox->AppendChild(MakeObject<Paragraph>(doc));
SharedPtr<Paragraph> para = textBox->get_FirstParagraph();
para->get_ParagraphFormat()->set_Alignment(Aspose::Words::ParagraphAlignment::Center);
// Add some text to the paragraph
auto run = MakeObject<Run>(doc);
run->set_Text(u"Hello world!");
para->AppendChild(run);
// Append the textbox to the first paragraph in the body
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(textBox);
doc->Save(ArtifactsDir + u"Shape.CreateTextBox.docx");

◆ get_LastParagraph()

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

Gets the last paragraph in the story.

Examples

Shows how to move a DocumentBuilder's cursor position to a specified node.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Write a paragraph with the DocumentBuilder
builder->Writeln(u"Text 1. ");
// Move the DocumentBuilder to the first paragraph of the document and add another paragraph
builder->MoveTo(doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_Runs()->idx_get(0));
builder->Writeln(u"Text 2. ");
// Since we moved to a node before the first paragraph before we added a second paragraph,
// the second paragraph will appear in front of the first paragraph
ASSERT_EQ(u"Text 2. \rText 1.", doc->GetText().Trim());
// We can move the DocumentBuilder back to the end of the document like this
// and carry on adding text to the end of the document
builder->MoveTo(doc->get_FirstSection()->get_Body()->get_LastParagraph());
builder->Writeln(u"Text 3. ");
ASSERT_EQ(u"Text 2. \rText 1. \rText 3.", doc->GetText().Trim());

◆ get_Paragraphs()

System::SharedPtr<Aspose::Words::ParagraphCollection> Aspose::Words::Story::get_Paragraphs ( )

Gets a collection of paragraphs that are immediate children of the story.

Examples

Shows how to get paragraph that was moved (deleted/inserted) in Microsoft Word while change tracking was enabled.

auto doc = MakeObject<Document>(MyDir + u"Revisions.docx");
// There are two sets of move revisions in this document
// One moves a small part of a paragraph, while the other moves a whole paragraph
// Paragraph.IsMoveFromRevision/IsMoveToRevision will only be true if a whole paragraph is moved, as in the latter case
SharedPtr<ParagraphCollection> paragraphs = doc->get_FirstSection()->get_Body()->get_Paragraphs();
for (int i = 0; i < paragraphs->get_Count(); i++)
{
if (paragraphs->idx_get(i)->get_IsMoveFromRevision())
{
System::Console::WriteLine(u"The paragraph {0} has been moved (deleted).", System::ObjectExt::Box<int>(i));
}
if (paragraphs->idx_get(i)->get_IsMoveToRevision())
{
System::Console::WriteLine(u"The paragraph {0} has been moved (inserted).", System::ObjectExt::Box<int>(i));
}
}

◆ get_StoryType()

Aspose::Words::StoryType Aspose::Words::Story::get_StoryType ( ) const

Gets the type of this story.

Examples

Shows how to clear a body of inline shapes.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Use a DocumentBuilder to insert a shape
// This is an inline shape, which has a parent Paragraph, which is in turn a child of the Body
builder->InsertShape(Aspose::Words::Drawing::ShapeType::Cube, 100.0, 100.0);
ASSERT_EQ(1, doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());
// We can delete all such shapes from the Body, affecting all child Paragraphs
ASSERT_EQ(Aspose::Words::StoryType::MainText, doc->get_FirstSection()->get_Body()->get_StoryType());
doc->get_FirstSection()->get_Body()->DeleteShapes();
ASSERT_EQ(0, doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());

◆ get_Tables()

System::SharedPtr<Aspose::Words::Tables::TableCollection> Aspose::Words::Story::get_Tables ( )

Gets a collection of tables that are immediate children of the story.

Examples

Shows how to use typed properties to access nodes of the document tree.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
// Quick typed access to all Table child nodes contained in the Body
SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();
ASSERT_EQ(5, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(4, tables->idx_get(1)->get_Rows()->get_Count());
for (auto table : System::IterateOver(tables->LINQ_OfType<SharedPtr<Table> >()))
{
// Quick typed access to the first row of the table
if (table->get_FirstRow() != nullptr)
{
table->get_FirstRow()->Remove();
}
// Quick typed access to the last row of the table
if (table->get_LastRow() != nullptr)
{
table->get_LastRow()->Remove();
}
}
// Each table has shrunk by two rows
ASSERT_EQ(3, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(2, tables->idx_get(1)->get_Rows()->get_Count());

◆ GetType()

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

◆ Is()

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

◆ Type()

static const System::TypeInfo& Aspose::Words::Story::Type ( )
static
@ Shape
A drawing object, such as an OfficeArt shape, image or an OLE object. A Shape node can contain Paragr...
@ Cube
static ASPOSECPP_SHARED_API Color get_Yellow()
@ None
No text wrapping around the shape. The shape is placed behind or in front of text.
static ASPOSECPP_SHARED_API void WriteLine()
std::enable_if_t<!Details::IsIterable< Enumerable >::value, Details::EnumeratorAdapter< Enumerable, T > > IterateOver(System::SmartPtr< Enumerable > enumerable)
@ TextBox
The shape is a textbox. Note that shapes of many other types can also have text inside them too....
@ HeaderPrimary
Primary header, also used for odd numbered pages.
@ MainText
Contains the main text of the document, represented by Body.
@ Center
Specifies that the object shall be centered with respect to the horizontal alignment base.
@ FooterPrimary
Primary footer, also used for odd numbered pages.
@ Top
Specifies that the object shall be at the top of the vertical alignment base.
@ Center
Text is centered horizontally.