Instantly share code, notes, and snippets.
Last active
May 17, 2021 12:10
-
Save aspose-com-gists/0402aac0b8cb5d122683b2002e8e9cff to your computer and use it in GitHub Desktop.
Add and Remove Header and Footer in Word Documents using C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Examples for Adding and Removing Header and Footer in Word Documents using C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void CopyHeadersFootersFromPreviousSection(const System::SharedPtr<Section>& section) | |
{ | |
System::SharedPtr<Section> previousSection = System::DynamicCast<Section>(section->get_PreviousSibling()); | |
if (previousSection == nullptr) | |
{ | |
return; | |
} | |
section->get_HeadersFooters()->Clear(); | |
for (System::SharedPtr<Node> headerFooterNode : System::IterateOver(previousSection->get_HeadersFooters())) | |
{ | |
section->get_HeadersFooters()->Add(headerFooterNode->Clone(true)); | |
} | |
} | |
int main() | |
{ | |
// Source and output directory paths. | |
System::String inputDataDir = u"SourceDirectory\\"; | |
System::String outputDataDir = u"OutputDirectory\\"; | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Section> currentSection = builder->get_CurrentSection(); | |
System::SharedPtr<PageSetup> pageSetup = currentSection->get_PageSetup(); | |
// Specify if we want headers/footers of the first page to be different from other pages. | |
// You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify | |
// Different headers/footers for odd and even pages. | |
pageSetup->set_DifferentFirstPageHeaderFooter(true); | |
// --- Create header for the first page. --- | |
pageSetup->set_HeaderDistance(20); | |
builder->MoveToHeaderFooter(HeaderFooterType::HeaderFirst); | |
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center); | |
// Set font properties for header text. | |
builder->get_Font()->set_Name(u"Arial"); | |
builder->get_Font()->set_Bold(true); | |
builder->get_Font()->set_Size(14); | |
// Specify header title for the first page. | |
builder->Write(u"Aspose.Words Header/Footer Creation Primer - Title Page."); | |
// --- Create header for pages other than the first page. --- | |
pageSetup->set_HeaderDistance(20); | |
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary); | |
// Insert absolutely positioned image into the top/left corner of the header. | |
// Distance from the top/left edges of the page is set to 10 points. | |
System::String imageFileName = inputDataDir + u"Desert.jpg"; | |
builder->InsertImage(imageFileName, RelativeHorizontalPosition::Page, 10, RelativeVerticalPosition::Page, 10, 50, 50, WrapType::Through); | |
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Right); | |
// Specify header title for other pages. | |
builder->Write(u"Aspose.Words Header/Footer Creation Primer."); | |
// --- Create footer for pages other than the first page. --- | |
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary); | |
// We use table with two cells to make one part of the text on the line (with page numbering) | |
// To be aligned left, and the other part of the text (with copyright) to be aligned right. | |
builder->StartTable(); | |
// Clear table borders. | |
builder->get_CellFormat()->ClearFormatting(); | |
builder->InsertCell(); | |
// Set the first cell to 1/3 of the page width. | |
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 / 3)); | |
// Insert page numbering text here. | |
// It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages. | |
builder->Write(u"Page "); | |
builder->InsertField(u"PAGE", u""); | |
builder->Write(u" of "); | |
builder->InsertField(u"NUMPAGES", u""); | |
// Align this text to the left. | |
builder->get_CurrentParagraph()->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Left); | |
builder->InsertCell(); | |
// Set the second cell to 2/3 of the page width. | |
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 * 2 / 3)); | |
builder->Write(u"(C) 2001 Aspose Pty Ltd. All rights reserved."); | |
// Align this text to the right. | |
builder->get_CurrentParagraph()->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Right); | |
builder->EndRow(); | |
builder->EndTable(); | |
builder->MoveToDocumentEnd(); | |
// Add a page break to create a second page on which the primary headers/footers will be seen. | |
builder->InsertBreak(BreakType::PageBreak); | |
// Add a section break to create a third page with different page orientation. | |
builder->InsertBreak(BreakType::SectionBreakNewPage); | |
// Get the new section and its page setup. | |
currentSection = builder->get_CurrentSection(); | |
pageSetup = currentSection->get_PageSetup(); | |
// Set page orientation of the new section to landscape. | |
pageSetup->set_Orientation(Orientation::Landscape); | |
// This section does not need different first page header/footer. | |
// We need only one title page in the document. The header/footer for this page | |
// has already been defined in the previous section | |
pageSetup->set_DifferentFirstPageHeaderFooter(false); | |
// This section displays headers/footers from the previous section by default. | |
// Call currentSection.HeadersFooters.LinkToPrevious(false) to cancel this behaviour. | |
// Page width is different for the new section and therefore we need to set | |
// different cell widths for a footer table. | |
currentSection->get_HeadersFooters()->LinkToPrevious(false); | |
// If we want to use the already existing header/footer set for this section | |
// but with some minor modifications then it may be expedient to copy headers/footers | |
// from the previous section and apply the necessary modifications where we want them. | |
CopyHeadersFootersFromPreviousSection(currentSection); | |
// Find the footer that we want to change. | |
System::SharedPtr<HeaderFooter> primaryFooter = currentSection->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary); | |
System::SharedPtr<Row> row = primaryFooter->get_Tables()->idx_get(0)->get_FirstRow(); | |
row->get_FirstCell()->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 / 3)); | |
row->get_LastCell()->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 * 2 / 3)); | |
System::String outputPath = outputDataDir + u"CreateHeaderFooter.docx"; | |
// Save the resulting document. | |
doc->Save(outputPath); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source and output directory paths. | |
System::String inputDataDir = u"SourceDirectory\\"; | |
System::String outputDataDir = u"OutputDirectory\\"; | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"SampleHeaderFooter.docx"); | |
for (System::SharedPtr<Section> section : System::IterateOver<System::SharedPtr<Section>>(doc)) | |
{ | |
// Up to three different header and footers are possible in a section (for first, even and odd pages). | |
// We check and delete all of them. | |
System::SharedPtr<HeaderFooter> header; | |
System::SharedPtr<HeaderFooter> footer; | |
header = section->get_HeadersFooters()->idx_get(HeaderFooterType::HeaderFirst); | |
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterFirst); | |
if (header != nullptr) | |
{ | |
header->Remove(); | |
} | |
if (footer != nullptr) | |
{ | |
footer->Remove(); | |
} | |
// Primary header and footer is used for odd pages. | |
header = section->get_HeadersFooters()->idx_get(HeaderFooterType::HeaderPrimary); | |
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary); | |
if (header != nullptr) | |
{ | |
header->Remove(); | |
} | |
if (footer != nullptr) | |
{ | |
footer->Remove(); | |
} | |
header = section->get_HeadersFooters()->idx_get(HeaderFooterType::HeaderEven); | |
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterEven); | |
if (header != nullptr) | |
{ | |
header->Remove(); | |
} | |
if (footer != nullptr) | |
{ | |
footer->Remove(); | |
} | |
} | |
// Output file path | |
System::String outputPath = outputDataDir + u"RemoveFooters.docx"; | |
// Save the document. | |
doc->Save(outputPath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment