Headers and Footers in Word documents are used to format and show important information such as the topic, chapter, page number, Copywrite, etc. While working with Word documents programmatically, you might need to add or remove headers and footers. To that end, this article will teach you how to add and remove headers and footers in Word documents using C++.
- C++ API for Adding and Removing Header and Footer in Word Documents
- Add Header and Footer in Word Documents using C++
- Delete Header and Footer in a Word Document using C++
C++ API for Adding and Removing Header and Footer in Word Documents
To add headers and footers in Word documents, we will use the Aspose.Words for C++ API. It is a native C++ API that supports creating, reading, and modifying Word documents without requiring Microsoft Word to be installed. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Words.Cpp
Add Header and Footer in Word Documents using C++
The headers and footers in a Word document are divided into three sections, the title page, even pages, and odd pages. You can add different headers and footers for these sections. Additionally, you can also add elements like images and tables in headers and footers.
In this example, we will create a new Word document and add a different header for the title page. We will add a header with an image and a footer with a table to the subsequent pages. The following are the steps to add headers and footers in Word documents.
- Create an instance of the Document class to represent the Word document.
- Create an instance of the DocumentBuilder class using the Document object created previously.
- Specify that you want a different header and footer for the title page using the PageSetup->set_DifferentFirstPageHeaderFooter(bool value) method.
- Set the font properties for the header text.
- Create the header and footer for the subsequent pages.
- Add image in the header using the DocumentBuilder->InsertImage(System::SharedPtr<System::Drawing::Image> image, Aspose::Words::Drawing::RelativeHorizontalPosition horzPos, double left, Aspose::Words::Drawing::RelativeVerticalPosition vertPos, double top, double width, double height, Aspose::Words::Drawing::WrapType wrapType) method.
- Add table in the footer.
- Save the Word document using the Document->Save(System::String fileName) method.
The following sample code demonstrates how to add headers and footers in Word documents using C++.
Delete Header and Footer in a Word Document using C++
In the previous section, you learned how to add headers and footers in Word documents. Similar to adding, you can remove the headers and footers from the title, even and odd pages according to your needs. The following are the steps to delete all headers and footers in a Word document.
- Load the Word document using the Document class.
- Create two instances of the HeaderFooter class to represent the headers and the footers.
- Retrieve the headers and footers for the title, even and odd pages using the Section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType headerFooterType) method.
- Remove the headers and footers using the HeaderFooter->Remove() method.
- Save the Word document using the Document->Save(System::String fileName) method.
The following sample code shows how to delete all headers and footers in a Word document using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
Working with headers and footers in Word documents is a common and frequent use case. In this article, you have learned how to work with headers and footers in Word documents using C++. Specifically, you have learned how to add and remove headers and footers in Word documents using Aspose.Words for C++ API. The API provides a bunch of additional features for working with Word documents that you can explore in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.