Aspose::Words::HeaderFooterCollection class
Contents
[
Hide
]HeaderFooterCollection class
Provides typed access to HeaderFooter nodes of a Section. To learn more, visit the Working with Headers and Footers documentation article.
class HeaderFooterCollection : public Aspose::Words::NodeCollection
Methods
| Method | Description |
|---|---|
| Add(const System::SharedPtr<Aspose::Words::Node>&) | Adds a node to the end of the collection. |
| Clear() | Removes all nodes from this collection and from the document. |
| Contains(const System::SharedPtr<Aspose::Words::Node>&) | Determines whether a node is in the collection. |
| get_Count() | Gets the number of nodes in the collection. |
| GetEnumerator() override | Provides a simple “foreach” style iteration over the collection of nodes. |
| GetType() const override | |
| idx_get(int32_t) | Retrieves a HeaderFooter at the given index. |
| idx_get(Aspose::Words::HeaderFooterType) | Retrieves a HeaderFooter of the specified type. |
| IndexOf(const System::SharedPtr<Aspose::Words::Node>&) | Returns the zero-based index of the specified node. |
| Insert(int32_t, const System::SharedPtr<Aspose::Words::Node>&) | Inserts a node into the collection at the specified index. |
| Is(const System::TypeInfo&) const override | |
| LinkToPrevious(bool) | Links or unlinks all headers and footers to the corresponding headers and footers in the previous section. |
| LinkToPrevious(Aspose::Words::HeaderFooterType, bool) | Links or unlinks the specified header or footer to the corresponding header or footer in the previous section. |
| Remove(const System::SharedPtr<Aspose::Words::Node>&) | Removes the node from the collection and from the document. |
| RemoveAt(int32_t) | Removes the node at the specified index from the collection and from the document. |
| ToArray() | Copies all HeaderFooters from the collection to a new array of HeaderFooters. |
| static Type() |
Remarks
There can be maximum of one HeaderFooter
of each HeaderFooterType per Section.
HeaderFooter objects can occur in any order in the collection.
Examples
Shows how to create a header and a footer.
auto doc = System::MakeObject<Aspose::Words::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 = System::MakeObject<Aspose::Words::HeaderFooter>(doc, Aspose::Words::HeaderFooterType::HeaderPrimary);
doc->get_FirstSection()->get_HeadersFooters()->Add(header);
System::SharedPtr<Aspose::Words::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 = System::MakeObject<Aspose::Words::HeaderFooter>(doc, Aspose::Words::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(get_ArtifactsDir() + u"HeaderFooter.Create.docx");
Shows how to delete all footers from a document.
auto doc = System::MakeObject<Aspose::Words::Document>(get_MyDir() + u"Header and footer types.docx");
// Iterate through each section and remove footers of every kind.
for (auto&& section : System::IterateOver(doc->LINQ_OfType<System::SharedPtr<Aspose::Words::Section> >()))
{
// There are three kinds of footer and header types.
// 1 - The "First" header/footer, which only appears on the first page of a section.
System::SharedPtr<Aspose::Words::HeaderFooter> footer = section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::FooterFirst);
System::SharedPtr<Aspose::Words::HeaderFooter> condExpression = footer;
if (condExpression != nullptr)
{
condExpression->Remove();
}
// 2 - The "Primary" header/footer, which appears on odd pages.
footer = section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::FooterPrimary);
System::SharedPtr<Aspose::Words::HeaderFooter> condExpression2 = footer;
if (condExpression2 != nullptr)
{
condExpression2->Remove();
}
// 3 - The "Even" header/footer, which appears on even pages.
footer = section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType::FooterEven);
System::SharedPtr<Aspose::Words::HeaderFooter> condExpression3 = footer;
if (condExpression3 != nullptr)
{
condExpression3->Remove();
}
ASSERT_EQ(0, section->get_HeadersFooters()->LINQ_Count(static_cast<System::Func<System::SharedPtr<Aspose::Words::Node>, bool>>(static_cast<std::function<bool(System::SharedPtr<Aspose::Words::Node> hf)>>([](System::SharedPtr<Aspose::Words::Node> hf) -> bool
{
return !(System::ExplicitCast<Aspose::Words::HeaderFooter>(hf))->get_IsHeader();
}))));
}
doc->Save(get_ArtifactsDir() + u"HeaderFooter.RemoveFooters.docx");
See Also
- Class NodeCollection
- Namespace Aspose::Words
- Library Aspose.Words for C++