Aspose::Words::Section Class Referencefinal

Detailed Description

Represents a single section in a document.

Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section.

A minimal valid section needs to have Body with one Paragraph.

Each section has its own set of properties that specify page size, orientation, margins etc.

You can create a copy of a section using Clone(). The copy can be inserted into the same or different document.

To add, insert or remove a whole section including section break and section properties use methods of the Sections object.

To copy and insert just content of the section excluding the section break and section properties use AppendContent and PrependContent methods.

Examples

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

auto doc = MakeObject<Document>();
// A newly created blank document still comes one section, one body and one paragraph
// Calling this method will remove all those nodes to completely empty the document
doc->RemoveAllChildren();
// This document now has no composite nodes that content can be added to
// If we wish to edit it, we will need to repopulate its node collection,
// which we will start to do with by creating a new Section node
auto section = MakeObject<Section>(doc);
// Append the section to the document
doc->AppendChild(section);
// Lets set some properties for the section
section->get_PageSetup()->set_SectionStart(Aspose::Words::SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(Aspose::Words::PaperSize::Letter);
// The section that we created is empty, lets populate it. The section needs at least the Body node
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// The body needs to have at least one paragraph
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information
auto para = MakeObject<Paragraph>(doc);
body->AppendChild(para);
// We can set some formatting for the paragraph
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(Aspose::Words::ParagraphAlignment::Center);
// So far we have one empty paragraph in the document
// The document is valid and can be saved, but lets add some text before saving
// Create a new run of text and add it to our 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(String(u"Hello World!") + ControlChar::SectionBreakChar, doc->GetText());
doc->Save(ArtifactsDir + u"Section.CreateFromScratch.docx");

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

+ Inheritance diagram for Aspose::Words::Section:

Public Member Functions

 Section (SharedPtr< DocumentBase > doc)
 Initializes a new instance of the Section class. More...
 
bool Accept (SharedPtr< DocumentVisitor > visitor) override
 Accepts a visitor. More...
 
void AppendContent (SharedPtr< Section > sourceSection)
 Inserts a copy of content of the source section at the end of this section. More...
 
void ClearContent ()
 Clears the section. More...
 
void ClearHeadersFooters ()
 Clears the headers and footers of this section. More...
 
SharedPtr< SectionClone ()
 Creates a duplicate of this section. More...
 
void DeleteHeaderFooterShapes ()
 Deletes all shapes (drawing objects) from the headers and footers of this section. More...
 
void EnsureMinimum ()
 Ensures that the section has Body with one Paragraph. More...
 
SharedPtr< Bodyget_Body ()
 Returns the Body child node of the section. More...
 
SharedPtr< HeaderFooterCollectionget_HeadersFooters ()
 Provides access to the headers and footers nodes of the section. More...
 
NodeType get_NodeType () const override
 Returns NodeType.Section. More...
 
SharedPtr< PageSetupget_PageSetup ()
 Returns an object that represents page setup and section properties. More...
 
bool get_ProtectedForForms ()
 True if the section is protected for forms. When a section is protected for forms, users can select and modify text only in form fields in Microsoft Word. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void PrependContent (SharedPtr< Section > sourceSection)
 Inserts a copy of content of the source section at the beginning of this section. More...
 
void set_ProtectedForForms (bool value)
 Setter for get_ProtectedForForms. More...
 
- 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
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...
 
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
 

Constructor & Destructor Documentation

◆ Section()

Aspose::Words::Section::Section ( System::SharedPtr< Aspose::Words::DocumentBase doc)

Initializes a new instance of the Section class.

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

To include Section into a document use Document.InsertAfter, Document.InsertBefore or Sections.Add and Section.Insert methods.

Parameters
docThe owner document.
Examples

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

auto doc = MakeObject<Document>();
// A newly created blank document still comes one section, one body and one paragraph
// Calling this method will remove all those nodes to completely empty the document
doc->RemoveAllChildren();
// This document now has no composite nodes that content can be added to
// If we wish to edit it, we will need to repopulate its node collection,
// which we will start to do with by creating a new Section node
auto section = MakeObject<Section>(doc);
// Append the section to the document
doc->AppendChild(section);
// Lets set some properties for the section
section->get_PageSetup()->set_SectionStart(Aspose::Words::SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(Aspose::Words::PaperSize::Letter);
// The section that we created is empty, lets populate it. The section needs at least the Body node
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// The body needs to have at least one paragraph
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information
auto para = MakeObject<Paragraph>(doc);
body->AppendChild(para);
// We can set some formatting for the paragraph
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(Aspose::Words::ParagraphAlignment::Center);
// So far we have one empty paragraph in the document
// The document is valid and can be saved, but lets add some text before saving
// Create a new run of text and add it to our 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(String(u"Hello World!") + ControlChar::SectionBreakChar, doc->GetText());
doc->Save(ArtifactsDir + u"Section.CreateFromScratch.docx");

Member Function Documentation

◆ Accept()

bool Aspose::Words::Section::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.

Implements Aspose::Words::Node.

◆ AppendContent()

void Aspose::Words::Section::AppendContent ( System::SharedPtr< Aspose::Words::Section sourceSection)

Inserts a copy of content of the source section at the end of this section.

Only content of Body of the source section is copied, page setup, headers and footers are not copied.

The nodes are automatically imported if the source section belongs to a different document.

No new section is created in the destination document.

Parameters
sourceSectionThe section to copy content from.
Examples

Shows how to append content of an existing section. The number of sections in the document remains the same.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->Write(u"Section 2");
builder->Write(u"Section 3");
// This is the section that we will append and prepend to
SharedPtr<Section> section = doc->get_Sections()->idx_get(2);
// This copies content of the 1st section and inserts it at the beginning of the specified section
SharedPtr<Section> sectionToPrepend = doc->get_Sections()->idx_get(0);
section->PrependContent(sectionToPrepend);
// This copies content of the 2nd section and inserts it at the end of the specified section
SharedPtr<Section> sectionToAppend = doc->get_Sections()->idx_get(1);
section->AppendContent(sectionToAppend);
ASSERT_EQ(String(u"Section 1") + ControlChar::SectionBreak() + u"Section 2" + ControlChar::SectionBreak() + u"Section 1" + ControlChar::ParagraphBreak() + u"Section 3" + ControlChar::ParagraphBreak() + u"Section 2" + ControlChar::SectionBreak(), doc->GetText());

◆ ClearContent()

void Aspose::Words::Section::ClearContent ( )

Clears the section.

The text of Body is cleared, only one empty paragraph is left that represents the section break.

The text of all headers and footers is cleared, but HeaderFooter objects themselves are not removed.

Examples

Shows how to clear the content of a section.

auto doc = MakeObject<Document>(MyDir + u"Document.docx");
ASSERT_EQ(u"Hello World!", doc->GetText().Trim());
doc->get_FirstSection()->ClearContent();
ASSERT_EQ(String::Empty, doc->GetText().Trim());

◆ ClearHeadersFooters()

void Aspose::Words::Section::ClearHeadersFooters ( )

Clears the headers and footers of this section.

The text of all headers and footers is cleared, but HeaderFooter objects themselves are not removed.

This makes headers and footers of this section linked to headers and footers of the previous section.

Examples

Clears content of all headers and footers in a section.

auto doc = MakeObject<Document>(MyDir + u"Header and footer types.docx");
SharedPtr<Section> section = doc->get_Sections()->idx_get(0);
ASSERT_EQ(6, section->get_HeadersFooters()->get_Count());
ASSERT_EQ(u"First header", section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::HeaderFirst)->GetText().Trim());
section->ClearHeadersFooters();
ASSERT_EQ(6, section->get_HeadersFooters()->get_Count());
ASSERT_EQ(String::Empty, section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::HeaderFirst)->GetText());

◆ Clone()

System::SharedPtr<Aspose::Words::Section> Aspose::Words::Section::Clone ( )

Creates a duplicate of this section.

Examples

Shows how to add/remove sections in a document.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->Write(u"Section 2");
// This shows what is in the document originally. The document has two sections
ASSERT_EQ(u"Section 1\x000c" u"Section 2", doc->GetText().Trim());
// Delete the first section from the document
doc->get_Sections()->RemoveAt(0);
// Duplicate the last section and append the copy to the end of the document
int lastSectionIdx = doc->get_Sections()->get_Count() - 1;
SharedPtr<Section> newSection = doc->get_Sections()->idx_get(lastSectionIdx)->Clone();
doc->get_Sections()->Add(newSection);
// Check what the document contains after we changed it
ASSERT_EQ(u"Section 2\x000c" u"Section 2", doc->GetText().Trim());

◆ DeleteHeaderFooterShapes()

void Aspose::Words::Section::DeleteHeaderFooterShapes ( )

Deletes all shapes (drawing objects) from the headers and footers of this section.

Examples

Removes all images and shapes from all headers footers in a section.

auto doc = MakeObject<Document>();
SharedPtr<Section> section = doc->get_Sections()->idx_get(0);
auto firstHeader = MakeObject<HeaderFooter>(doc, Aspose::Words::HeaderFooterType::HeaderFirst);
section->get_HeadersFooters()->Add(firstHeader);
firstHeader->AppendParagraph(u"This paragraph contains a shape: ");
auto shape = MakeObject<Shape>(doc, Aspose::Words::Drawing::ShapeType::Arrow);
firstHeader->get_FirstParagraph()->AppendChild(shape);
ASSERT_EQ(1, firstHeader->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());
section->DeleteHeaderFooterShapes();
ASSERT_EQ(0, firstHeader->GetChildNodes(Aspose::Words::NodeType::Shape, true)->get_Count());

◆ EnsureMinimum()

void Aspose::Words::Section::EnsureMinimum ( )

Ensures that the section has Body with one Paragraph.

Examples

Shows how to prepare a new section node for editing.

auto doc = MakeObject<Document>();
// A blank document comes with a section, which has a body, which in turn has a paragraph,
// so we can edit the document by adding children to the paragraph like shapes or runs of text
ASSERT_EQ(2, doc->get_Sections()->idx_get(0)->GetChildNodes(Aspose::Words::NodeType::Any, true)->get_Count());
// If we add a new section like this, it will not have a body or a paragraph that we can edit
doc->get_Sections()->Add(MakeObject<Section>(doc));
ASSERT_EQ(0, doc->get_Sections()->idx_get(1)->GetChildNodes(Aspose::Words::NodeType::Any, true)->get_Count());
// Makes sure that the section contains a body with at least one paragraph
doc->get_LastSection()->EnsureMinimum();
// Now we can add content to this section
ASSERT_EQ(2, doc->get_Sections()->idx_get(1)->GetChildNodes(Aspose::Words::NodeType::Any, true)->get_Count());

◆ get_Body()

System::SharedPtr<Aspose::Words::Body> Aspose::Words::Section::get_Body ( )

Returns the Body child node of the section.

Body contains main text of the section.

Returns null if the section does not have a Body node among its children.

Examples

Clears main text from all sections from the document leaving the sections themselves.

// Open a document
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->Write(u"Section 2");
// This shows what is in the document originally
// The document has two sections
ASSERT_EQ(String::Format(u"Section 1{0}Section 2{1}",ControlChar::SectionBreak(),ControlChar::SectionBreak()), doc->GetText());
// Loop through all sections in the document
for (auto section : System::IterateOver(doc->get_Sections()->LINQ_OfType<SharedPtr<Section> >()))
{
// Each section has a Body node that contains main story (main text) of the section
SharedPtr<Body> body = section->get_Body();
// This clears all nodes from the body
body->RemoveAllChildren();
// Technically speaking, for the main story of a section to be valid, it needs to have
// at least one empty paragraph. That's what the EnsureMinimum method does
body->EnsureMinimum();
}
// Check how the content of the document looks now
ASSERT_EQ(String::Format(u"{0}{1}",ControlChar::SectionBreak(),ControlChar::SectionBreak()), doc->GetText());

◆ get_HeadersFooters()

System::SharedPtr<Aspose::Words::HeaderFooterCollection> Aspose::Words::Section::get_HeadersFooters ( )

Provides access to the headers and footers nodes of the section.

Examples

Deletes all footers from all sections, but leaves headers intact.

auto doc = MakeObject<Document>(MyDir + u"Header and footer types.docx");
for (auto section : System::IterateOver(doc->LINQ_OfType<SharedPtr<Section> >()))
{
// Up to three different footers are possible in a section (for first, even and odd pages)
// We check and delete all of them
SharedPtr<HeaderFooter> footer = section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::FooterFirst);
if (footer != nullptr)
{
footer->Remove();
}
// Primary footer is the footer used for odd pages
footer = section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::FooterPrimary);
if (footer != nullptr)
{
footer->Remove();
}
footer = section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::FooterEven);
if (footer != nullptr)
{
footer->Remove();
}
// All footers have been removed from the section's HeaderFooter collection,
// so every remaining node is a header and has the "IsHeader" flag set to true
ASSERT_EQ(0, section->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return !(System::DynamicCast<HeaderFooter>(hf))->get_IsHeader(); }));
}
doc->Save(ArtifactsDir + u"HeaderFooter.RemoveFooters.docx");

Shows how to replace text in the document footer.

// Open the template document, containing obsolete copyright information in the footer
auto doc = MakeObject<Document>(MyDir + u"Footer.docx");
SharedPtr<HeaderFooterCollection> headersFooters = doc->get_FirstSection()->get_HeadersFooters();
SharedPtr<HeaderFooter> footer = headersFooters->idx_get(Aspose::Words::HeaderFooterType::FooterPrimary);
auto options = MakeObject<FindReplaceOptions>();
options->set_MatchCase(false);
options->set_FindWholeWordsOnly(false);
int currentYear = System::DateTime::get_Now().get_Year();
footer->get_Range()->Replace(u"(C) 2006 Aspose Pty Ltd.", String::Format(u"Copyright (C) {0} by Aspose Pty Ltd.",currentYear), options);
doc->Save(ArtifactsDir + u"HeaderFooter.ReplaceText.docx");

◆ get_NodeType()

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

Returns NodeType.Section.

Implements Aspose::Words::Node.

◆ get_PageSetup()

System::SharedPtr<Aspose::Words::PageSetup> Aspose::Words::Section::get_PageSetup ( )

Returns an object that represents page setup and section properties.

Examples

Shows how to create a page border that looks like a wide blue band at the top of the first page only.

auto doc = MakeObject<Document>();
SharedPtr<PageSetup> pageSetup = doc->get_Sections()->idx_get(0)->get_PageSetup();
pageSetup->set_BorderAlwaysInFront(false);
pageSetup->set_BorderDistanceFrom(Aspose::Words::PageBorderDistanceFrom::PageEdge);
pageSetup->set_BorderAppliesTo(Aspose::Words::PageBorderAppliesTo::FirstPage);
SharedPtr<Border> border = pageSetup->get_Borders()->idx_get(Aspose::Words::BorderType::Top);
border->set_LineStyle(Aspose::Words::LineStyle::Single);
border->set_LineWidth(30);
border->set_Color(System::Drawing::Color::get_Blue());
border->set_DistanceFromText(0);
doc->Save(ArtifactsDir + u"PageSetup.PageBorderProperties.docx");

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

auto doc = MakeObject<Document>();
// A newly created blank document still comes one section, one body and one paragraph
// Calling this method will remove all those nodes to completely empty the document
doc->RemoveAllChildren();
// This document now has no composite nodes that content can be added to
// If we wish to edit it, we will need to repopulate its node collection,
// which we will start to do with by creating a new Section node
auto section = MakeObject<Section>(doc);
// Append the section to the document
doc->AppendChild(section);
// Lets set some properties for the section
section->get_PageSetup()->set_SectionStart(Aspose::Words::SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(Aspose::Words::PaperSize::Letter);
// The section that we created is empty, lets populate it. The section needs at least the Body node
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// The body needs to have at least one paragraph
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information
auto para = MakeObject<Paragraph>(doc);
body->AppendChild(para);
// We can set some formatting for the paragraph
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(Aspose::Words::ParagraphAlignment::Center);
// So far we have one empty paragraph in the document
// The document is valid and can be saved, but lets add some text before saving
// Create a new run of text and add it to our 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(String(u"Hello World!") + ControlChar::SectionBreakChar, doc->GetText());
doc->Save(ArtifactsDir + u"Section.CreateFromScratch.docx");

◆ get_ProtectedForForms()

bool Aspose::Words::Section::get_ProtectedForForms ( )

True if the section is protected for forms. When a section is protected for forms, users can select and modify text only in form fields in Microsoft Word.

Examples

Shows how to protect a section so only editing in form fields is possible.

auto doc = MakeObject<Document>();
// Insert two sections with some text
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Section 1. Unprotected.");
builder->Writeln(u"Section 2. Protected.");
// Section protection only works when document protection is turned and only editing in form fields is allowed
// By default, all sections are protected, but we can selectively turn protection off
doc->get_Sections()->idx_get(0)->set_ProtectedForForms(false);
doc->Save(ArtifactsDir + u"Section.Protect.docx");

◆ GetType()

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

Reimplemented from Aspose::Words::CompositeNode.

◆ Is()

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

Reimplemented from Aspose::Words::CompositeNode.

◆ PrependContent()

void Aspose::Words::Section::PrependContent ( System::SharedPtr< Aspose::Words::Section sourceSection)

Inserts a copy of content of the source section at the beginning of this section.

Only content of Body of the source section is copied, page setup, headers and footers are not copied.

The nodes are automatically imported if the source section belongs to a different document.

No new section is created in the destination document.

Parameters
sourceSectionThe section to copy content from.
Examples

Shows how to append content of an existing section. The number of sections in the document remains the same.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->Write(u"Section 2");
builder->Write(u"Section 3");
// This is the section that we will append and prepend to
SharedPtr<Section> section = doc->get_Sections()->idx_get(2);
// This copies content of the 1st section and inserts it at the beginning of the specified section
SharedPtr<Section> sectionToPrepend = doc->get_Sections()->idx_get(0);
section->PrependContent(sectionToPrepend);
// This copies content of the 2nd section and inserts it at the end of the specified section
SharedPtr<Section> sectionToAppend = doc->get_Sections()->idx_get(1);
section->AppendContent(sectionToAppend);
ASSERT_EQ(String(u"Section 1") + ControlChar::SectionBreak() + u"Section 2" + ControlChar::SectionBreak() + u"Section 1" + ControlChar::ParagraphBreak() + u"Section 3" + ControlChar::ParagraphBreak() + u"Section 2" + ControlChar::SectionBreak(), doc->GetText());

◆ set_ProtectedForForms()

void Aspose::Words::Section::set_ProtectedForForms ( bool  value)

◆ Type()

static const System::TypeInfo& Aspose::Words::Section::Type ( )
static
String
@ Shape
A drawing object, such as an OfficeArt shape, image or an OLE object. A Shape node can contain Paragr...
static constexpr char16_t SectionBreakChar
End of section character: (char)12 or "\f".
Definition: ControlChar.h:1255
@ Top
Specifies the top border of a paragraph or a table cell.
static System::String & SectionBreak()
End of section character: "\x000c" or "\f". Note it has the same value as PageBreak.
static ASPOSECPP_SHARED_API DateTime get_Now()
@ Arrow
std::enable_if_t<!Details::IsIterable< Enumerable >::value, Details::EnumeratorAdapter< Enumerable, T > > IterateOver(System::SmartPtr< Enumerable > enumerable)
@ HeaderFirst
Header for the first page of the section.
@ Letter
8.5 x 11 inches.
static System::String & ParagraphBreak()
End of paragraph character: "\x000d" or "\r". Same as Cr
@ NewPage
The section starts from a new page.
@ FirstPage
Page border is shown on the first page of the section only.
@ FooterEven
Footer for even numbered pages.
@ AllowOnlyFormFields
User can only enter data in the form fields in the document.
static ASPOSECPP_SHARED_API Color get_Blue()
@ PageEdge
Border position is measured from the page edge.
ASPOSECPP_SHARED_API int get_Year() const
static ASPOSECPP_SHARED_API Color get_Red()
@ FooterPrimary
Primary footer, also used for odd numbered pages.
@ FooterFirst
Footer for the first page of the section.
@ SectionBreakContinuous
Specifies start of new section on the same page as the previous section.
@ Single
@ Any
Indicates all node types. Allows to select all children.
@ SectionBreakNewPage
Specifies start of new section on a new page.
@ Center
Text is centered horizontally.