Aspose::Words::HeaderFooter Class Reference

Detailed Description

Represents a container for the header or footer text of a section.

HeaderFooter can contain Paragraph and Table child nodes.

HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section.

If Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word.

When HeaderFooter contains at least one Paragraph, it is no longer considered linked to previous in Microsoft Word.

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

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

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

+ Inheritance diagram for Aspose::Words::HeaderFooter:

Public Member Functions

 HeaderFooter (SharedPtr< DocumentBase > doc, HeaderFooterType headerFooterType)
 Creates a new header or footer of the specified type. More...
 
bool Accept (SharedPtr< DocumentVisitor > visitor) override
 Accepts a visitor. More...
 
HeaderFooterType get_HeaderFooterType ()
 Gets the type of this header/footer. More...
 
bool get_IsHeader ()
 True if this HeaderFooter object is a header. More...
 
bool get_IsLinkedToPrevious ()
 True if this header or footer is linked to the corresponding header or footer in the previous section. More...
 
NodeType get_NodeType () const override
 Returns NodeType.HeaderFooter. More...
 
SharedPtr< Sectionget_ParentSection ()
 Gets the parent section of this story. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_IsLinkedToPrevious (bool value)
 Setter for get_IsLinkedToPrevious. More...
 
- Public Member Functions inherited from Story
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...
 
- 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 Story
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

◆ HeaderFooter()

Aspose::Words::HeaderFooter::HeaderFooter ( System::SharedPtr< Aspose::Words::DocumentBase doc,
Aspose::Words::HeaderFooterType  headerFooterType 
)

Creates a new header or footer of the specified type.

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

To append HeaderFooter to a Section use Section.InsertAfter, Section.InsertBefore, HeadersFooters.Add or HeadersFooters.Insert.

Parameters
docThe owner document.
headerFooterTypeA HeaderFooterType value that specifies the type of the header or footer.
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");

Member Function Documentation

◆ Accept()

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

◆ get_HeaderFooterType()

Aspose::Words::HeaderFooterType Aspose::Words::HeaderFooter::get_HeaderFooterType ( )

Gets the type of this header/footer.

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

◆ get_IsHeader()

bool Aspose::Words::HeaderFooter::get_IsHeader ( )

True if this HeaderFooter object is a header.

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

◆ get_IsLinkedToPrevious()

bool Aspose::Words::HeaderFooter::get_IsLinkedToPrevious ( )

True if this header or footer is linked to the corresponding header or footer in the previous section.

Default is true.

Note, when your link a header or footer, its contents is cleared.

Examples

Shows how to link header/footers between sections.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Create three sections
builder->Write(u"Section 1");
builder->Write(u"Section 2");
builder->Write(u"Section 3");
// Create a header and footer in the first section and give them text
builder->MoveToSection(0);
builder->Write(u"This is the header, which will be displayed in sections 1 and 2.");
builder->Write(u"This is the footer, which will be displayed in sections 1, 2 and 3.");
// If headers/footers are linked by the next section, they appear in that section also
// The second section will display the header/footers of the first
doc->get_Sections()->idx_get(1)->get_HeadersFooters()->LinkToPrevious(true);
// However, the underlying headers/footers in the respective header/footer collections of the sections still remain different
// Linking just overrides the existing headers/footers from the latter section
ASSERT_EQ(doc->get_Sections()->idx_get(0)->get_HeadersFooters()->idx_get(0)->get_HeaderFooterType(), doc->get_Sections()->idx_get(1)->get_HeadersFooters()->idx_get(0)->get_HeaderFooterType());
ASPOSE_ASSERT_NE(doc->get_Sections()->idx_get(0)->get_HeadersFooters()->idx_get(0)->get_ParentSection(), doc->get_Sections()->idx_get(1)->get_HeadersFooters()->idx_get(0)->get_ParentSection());
ASSERT_NE(doc->get_Sections()->idx_get(0)->get_HeadersFooters()->idx_get(0)->GetText(), doc->get_Sections()->idx_get(1)->get_HeadersFooters()->idx_get(0)->GetText());
// Likewise, unlinking headers/footers makes them not appear
doc->get_Sections()->idx_get(2)->get_HeadersFooters()->LinkToPrevious(false);
// We can also choose only certain header/footer types to get linked, like the footer in this case
// The 3rd section now won't have the same header but will have the same footer as the 2nd and 1st sections
doc->get_Sections()->idx_get(2)->get_HeadersFooters()->LinkToPrevious(Aspose::Words::HeaderFooterType::FooterPrimary, true);
// The first section's header/footers can't link themselves to anything because there is no previous section
ASSERT_EQ(2, doc->get_Sections()->idx_get(0)->get_HeadersFooters()->get_Count());
ASSERT_EQ(2, doc->get_Sections()->idx_get(0)->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return !(System::DynamicCast<HeaderFooter>(hf))->get_IsLinkedToPrevious(); }));
// All of the second section's header/footers are linked to those of the first
ASSERT_EQ(6, doc->get_Sections()->idx_get(1)->get_HeadersFooters()->get_Count());
ASSERT_EQ(6, doc->get_Sections()->idx_get(1)->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return (System::DynamicCast<HeaderFooter>(hf))->get_IsLinkedToPrevious(); }));
// In the third section, only the footer we explicitly linked is linked to that of the second, and consequently the first section
ASSERT_EQ(6, doc->get_Sections()->idx_get(2)->get_HeadersFooters()->get_Count());
ASSERT_EQ(5, doc->get_Sections()->idx_get(2)->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return !(System::DynamicCast<HeaderFooter>(hf))->get_IsLinkedToPrevious(); }));
ASSERT_TRUE(doc->get_Sections()->idx_get(2)->get_HeadersFooters()->idx_get(3)->get_IsLinkedToPrevious());
doc->Save(ArtifactsDir + u"HeaderFooter.HeaderFooterLink.docx");

◆ get_NodeType()

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

Returns NodeType.HeaderFooter.

Examples

Shows how you can enumerate through children of a composite node and detect types of the children nodes.

// Open a document
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->Write(u"Primary header");
builder->Write(u"Primary footer");
// Get the first section in the document
SharedPtr<Section> section = doc->get_FirstSection();
// A Section is a composite node and therefore can contain child nodes
// Section can contain only Body and HeaderFooter nodes
for (auto node : System::IterateOver(section))
{
// Every node has the NodeType property
switch (node->get_NodeType())
{
{
// If the node type is Body, we can cast the node to the Body class
auto body = System::DynamicCast<Aspose::Words::Body>(node);
// Write the content of the main story of the section to the console
System::Console::WriteLine(u"*** Body ***");
System::Console::WriteLine(body->GetText());
break;
}
{
// If the node type is HeaderFooter, we can cast the node to the HeaderFooter class
auto headerFooter = System::DynamicCast<Aspose::Words::HeaderFooter>(node);
// Write the content of the header footer to the console
System::Console::WriteLine(u"*** HeaderFooter ***");
System::Console::WriteLine(System::ObjectExt::Box<HeaderFooterType>(headerFooter->get_HeaderFooterType()));
System::Console::WriteLine(headerFooter->GetText());
break;
}
default:
{
// Other types of nodes never occur inside a Section node
throw System::Exception(u"Unexpected node type in a section.");
}
}
}

Implements Aspose::Words::Node.

◆ get_ParentSection()

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

Gets the parent section of this story.

ParentSection is equivalent to (Section)ParentNode.

Examples

Shows how to link header/footers between sections.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Create three sections
builder->Write(u"Section 1");
builder->Write(u"Section 2");
builder->Write(u"Section 3");
// Create a header and footer in the first section and give them text
builder->MoveToSection(0);
builder->Write(u"This is the header, which will be displayed in sections 1 and 2.");
builder->Write(u"This is the footer, which will be displayed in sections 1, 2 and 3.");
// If headers/footers are linked by the next section, they appear in that section also
// The second section will display the header/footers of the first
doc->get_Sections()->idx_get(1)->get_HeadersFooters()->LinkToPrevious(true);
// However, the underlying headers/footers in the respective header/footer collections of the sections still remain different
// Linking just overrides the existing headers/footers from the latter section
ASSERT_EQ(doc->get_Sections()->idx_get(0)->get_HeadersFooters()->idx_get(0)->get_HeaderFooterType(), doc->get_Sections()->idx_get(1)->get_HeadersFooters()->idx_get(0)->get_HeaderFooterType());
ASPOSE_ASSERT_NE(doc->get_Sections()->idx_get(0)->get_HeadersFooters()->idx_get(0)->get_ParentSection(), doc->get_Sections()->idx_get(1)->get_HeadersFooters()->idx_get(0)->get_ParentSection());
ASSERT_NE(doc->get_Sections()->idx_get(0)->get_HeadersFooters()->idx_get(0)->GetText(), doc->get_Sections()->idx_get(1)->get_HeadersFooters()->idx_get(0)->GetText());
// Likewise, unlinking headers/footers makes them not appear
doc->get_Sections()->idx_get(2)->get_HeadersFooters()->LinkToPrevious(false);
// We can also choose only certain header/footer types to get linked, like the footer in this case
// The 3rd section now won't have the same header but will have the same footer as the 2nd and 1st sections
doc->get_Sections()->idx_get(2)->get_HeadersFooters()->LinkToPrevious(Aspose::Words::HeaderFooterType::FooterPrimary, true);
// The first section's header/footers can't link themselves to anything because there is no previous section
ASSERT_EQ(2, doc->get_Sections()->idx_get(0)->get_HeadersFooters()->get_Count());
ASSERT_EQ(2, doc->get_Sections()->idx_get(0)->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return !(System::DynamicCast<HeaderFooter>(hf))->get_IsLinkedToPrevious(); }));
// All of the second section's header/footers are linked to those of the first
ASSERT_EQ(6, doc->get_Sections()->idx_get(1)->get_HeadersFooters()->get_Count());
ASSERT_EQ(6, doc->get_Sections()->idx_get(1)->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return (System::DynamicCast<HeaderFooter>(hf))->get_IsLinkedToPrevious(); }));
// In the third section, only the footer we explicitly linked is linked to that of the second, and consequently the first section
ASSERT_EQ(6, doc->get_Sections()->idx_get(2)->get_HeadersFooters()->get_Count());
ASSERT_EQ(5, doc->get_Sections()->idx_get(2)->get_HeadersFooters()->LINQ_Count([](SharedPtr<Node> hf) { return !(System::DynamicCast<HeaderFooter>(hf))->get_IsLinkedToPrevious(); }));
ASSERT_TRUE(doc->get_Sections()->idx_get(2)->get_HeadersFooters()->idx_get(3)->get_IsLinkedToPrevious());
doc->Save(ArtifactsDir + u"HeaderFooter.HeaderFooterLink.docx");

◆ GetType()

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

Reimplemented from Aspose::Words::Story.

◆ Is()

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

Reimplemented from Aspose::Words::Story.

◆ set_IsLinkedToPrevious()

void Aspose::Words::HeaderFooter::set_IsLinkedToPrevious ( bool  value)

◆ Type()

static const System::TypeInfo& Aspose::Words::HeaderFooter::Type ( )
static
@ HeaderFooter
A HeaderFooter object that contains text of a particular header or footer inside a section....
static ASPOSECPP_SHARED_API DateTime get_Now()
ExceptionWrapper< Details_Exception > Exception
static ASPOSECPP_SHARED_API void WriteLine()
std::enable_if_t<!Details::IsIterable< Enumerable >::value, Details::EnumeratorAdapter< Enumerable, T > > IterateOver(System::SmartPtr< Enumerable > enumerable)
@ HeaderPrimary
Primary header, also used for odd numbered pages.
@ FooterEven
Footer for even numbered pages.
@ Body
A Body object that contains the main text of a section (main text story). A Body node can have Paragr...
ASPOSECPP_SHARED_API int get_Year() const
@ FooterPrimary
Primary footer, also used for odd numbered pages.
@ FooterFirst
Footer for the first page of the section.
@ SectionBreakNewPage
Specifies start of new section on a new page.