Master the process of inserting a Word header and footer using C#—no Interop required—by following this detailed, step‑by‑step tutorial. Learn how to programmatically add headers and footers to an MS Word document with C#, with full support for first, even, and odd pages. The C# code sample below demonstrates placing custom text in both the header and footer and then saving the document as DOCX.
Steps to Insert Word header and footer using C#
- Install Aspose.Words for .NET package
- Create an instance of Document and DocumentBuilder classes
- Set page setup to use different headers and footers for first, odd, and even pages
- Move the cursor to the header and footer and insert some text into them
- Insert page breaks into document’s body to view all headers and footers
- Finally, save the document to DOCX file format
In these steps, the Document class object is used to create and save a new Word document, while the DocumentBuilder class object is utilized to configure the page setup, move the cursor to different sections, and navigate to the header and footer of the Word file for inserting text and adding page breaks. Finally, after successfully adding a header and footer in Word using C#, the document is saved in DOCX format.
Code to Insert header and footer in Word using C#
By following the sample code, the DocumentBuilder class uses its PageSetup property to specify the header and footer type, while its MoveToHeaderFooter method lets you jump to any header, footer, or section in the Word document. The class also supports inserting page breaks, giving you fine‑grained control over the document’s formatting.
We’ve walked you through adding a header and footer to a Word document using C#. Ready to explore more Word‑processing capabilities? Check out our next guide on how to insert comment into Word document using C#.