Add or Remove Header and Footer in Word Documents using C++

Add or Remove Header and Footer in Word Documents using C++

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++.

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

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.

The following sample code demonstrates how to add headers and footers in Word documents using C++.

Print preview of the output generated by the sample code
Print preview of the output generated by the sample code

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.

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.

See Also