Working with Headers and Footers

Aspose.Words allows users to work with headers and footers in a document. A header is text that is placed at the top of a page, and a footer is text at the bottom of a page. Typically, these areas are used to insert information that should be repeated on all or some pages of the document, such as page numbers, creation date, company information, and so on.

Create Headers or Footers using DocumentBuilder

If you want to add a document header or footer programmatically, the easiest way is to use the DocumentBuilder class to do it.

The following code example shows how to add a header and footer for document pages:

When you add a header or footer to a document, you can set some advanced properties. Aspose.Words provides users with the HeaderFooter and HeaderFooterCollection classes, as well as HeaderFooterType enumeration that give you more control over the header and footer customization process.

You can specify three different header types and three different footer types for one document:

  1. Header and/or footer for the first page
  2. Header and/or footer for even pages
  3. Header and/or footer for odd pages

The following code example shows how to add a header for odd document pages:

Specify Whether to Display Different Headers or Footers for the First Page

As said above, you can also set a different header or footer for the first page. To do this, you need to set the DifferentFirstPageHeaderFooter flag to true and then specyfy the HeaderFirst or FooterFirst value.

The following code example shows how to set the header for the first page only:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Specify that we want different headers and footers for first page.
builder->get_PageSetup()->set_DifferentFirstPageHeaderFooter(true);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderFirst);
builder->Write(u"Header for the first page.");
builder->MoveToHeaderFooter(HeaderFooterType::FooterFirst);
builder->Write(u"Footer for the first page.");
builder->MoveToSection(0);
builder->Writeln(u"Page 1");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"Page 2");
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.DifferentFirstPage.docx");

Specify Whether to Display Different Headers or Footers for Odd or Even Pages

Next, you will want to set different headers or footers for odd and even pages in a document. To do this, you need to set the OddAndEvenPagesHeaderFooter flag to true and then specyfy the values HeaderPrimary and HeaderEven, or FooterPrimary and FooterEven.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Specify that we want different headers and footers for even and odd pages.
builder->get_PageSetup()->set_OddAndEvenPagesHeaderFooter(true);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderEven);
builder->Write(u"Header for even pages.");
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->Write(u"Header for odd pages.");
builder->MoveToHeaderFooter(HeaderFooterType::FooterEven);
builder->Write(u"Footer for even pages.");
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary);
builder->Write(u"Footer for odd pages.");
builder->MoveToSection(0);
builder->Writeln(u"Page 1");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"Page 2");
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.OddEvenPages.docx");

Insert an Absolutely Positioned Image into the Header

To place an image in a header or footer, use the HeaderPrimary header type or the FooterPrimary footer type and the InsertImage method.

The following code example shows how to add an image to a header:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->InsertImage(ImagesDir + u"Logo.jpg", RelativeHorizontalPosition::RightMargin, 10,
RelativeVerticalPosition::Page, 10, 50, 50, WrapType::Through);
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.InsertImage.docx");
view raw insert-image.h hosted with ❤ by GitHub

With Aspose.Words you can set the font and paragraph properties, use the HeaderPrimary header type or the FooterPrimary footer type, as well as methods and properties for working with the fonts and paragraphs you use for the document body.

The following code example shows how to set the text in the header to Arial, bold, size 14, and center alignment:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_Font()->set_Name(u"Arial");
builder->get_Font()->set_Bold(true);
builder->get_Font()->set_Size(14);
builder->Write(u"Header for page.");
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.FontProps.docx");
view raw font-props.h hosted with ❤ by GitHub

If necessary, you can add page numbers to the header or footer. To do this, use the HeaderPrimary header type or the FooterPrimary footer type and the InsertField method to add the required field.

The following code example shows how to add page numbers to the footer on the right:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Right);
builder->Write(u"Page ");
builder->InsertField(u"PAGE", u"");
builder->Write(u" of ");
builder->InsertField(u"NUMPAGES", u"");
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.PageNumbers.docx");
view raw page-numbers.h hosted with ❤ by GitHub

Use Headers or Footers Defined in the Previous Section

If you need to copy the header or footer from the previous section, you can do that too.

The following code example shows how to copy the header or footer from the previous section:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
/// <summary>
/// Clones and copies headers/footers form the previous section to the specified section.
/// </summary>
void CopyHeadersFootersFromPreviousSection(SharedPtr<Section> section)
{
auto previousSection = System::ExplicitCast<Section>(section->get_PreviousSibling());
if (previousSection == nullptr)
{
return;
}
section->get_HeadersFooters()->Clear();
for (const auto& headerFooter : System::IterateOver<HeaderFooter>(previousSection->get_HeadersFooters()))
{
section->get_HeadersFooters()->Add((System::ExplicitCast<Node>(headerFooter))->Clone(true));
}
}

Aspose.Words allows you to provide the appearance of a header or footer when using different orientations and page sizes.

The following example shows how to do this:

How to Remove Only Headers or Only Footers

Each section in a document can have up to three headers and up to three footers (for first, even, and odd pages). If you want to remove all headers or all footers in a document, you need to loop through all the sections and remove each corresponding header node or footer node.

The following code example shows how to remove all footers from all sections but leave headers intact. You can remove only headers in a similar way:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Header and footer types.docx");
for (const auto& section : System::IterateOver<Section>(doc))
{
// 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(HeaderFooterType::FooterFirst);
if (footer != nullptr)
{
footer->Remove();
}
// Primary footer is the footer used for odd pages.
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary);
if (footer != nullptr)
{
footer->Remove();
}
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterEven);
if (footer != nullptr)
{
footer->Remove();
}
}
doc->Save(ArtifactsDir + u"RemoveContent.RemoveFooters.docx");