This version of DocX contains support for Headers and Footers.
There are a few different options to choose from.
1) You can have a default Header and Footer for the entire document.
2) You can have a different Header and Footer for the first page.
3) You can have a different Header and Footer for odd and even pages.
The first code example generates a document with a default Header and Footer that is applied to all pages.
Default Header and Footer
- // Create a new document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add Header and Footer support to this document.
- document.AddHeaders();
- document.AddFooters();
-
- // Get the default Header for this document.
- Header header_default = document.Headers.odd;
-
- // Get the default Footer for this document.
- Footer footer_default = document.Footers.odd;
-
- // Insert a Paragraph into the default Header.
- Paragraph p1 = header_default.InsertParagraph();
- p1.Append("Hello Header.").Bold();
-
- // Insert a Paragraph into the document.
- Paragraph p2 = document.InsertParagraph();
- p2.AppendLine("Hello Document.").Bold();
-
- // Insert a Paragraph into the default Footer.
- Paragraph p3 = footer_default.InsertParagraph();
- p3.Append("Hello Footer.").Bold();
-
- // Save all changes to this document.
- document.Save();
- }// Release this document from memory.
The code directly above will generate a document that looks like the image below.
The second code example generates a document which has a different Header and Footer for the first page. All other pages use the default Header and Footer.
First Header and Footer
- // Create a new document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add Header and Footer support to this document.
- document.AddHeaders();
- document.AddFooters();
-
- // Get the first Header for this document.
- Header header_first = document.Headers.first;
-
- // Get the first Footer for this document.
- Footer footer_first = document.Footers.first;
-
- // Insert a Paragraph into the first Header.
- Paragraph p1 = header_first.InsertParagraph();
- p1.Append("Hello First Header.").Bold();
-
- // Insert a Paragraph into the document.
- Paragraph p2 = document.InsertParagraph();
- p2.AppendLine("Hello First page.");
-
- // Create a second page to show that only the first has a header and footer.
- p2.InsertPageBreakAfterSelf();
-
- // Insert a Paragraph after the page break.
- Paragraph p3 = document.InsertParagraph();
- p3.AppendLine("Hello Second page.");
-
- // Insert a Paragraph into the first Footer.
- Paragraph p4 = footer_first.InsertParagraph();
- p4.Append("Hello First Footer.").Bold();
-
- // Force the first page to have a different Header and Footer.
- document.DifferentFirstPage = true;
-
- // Save all changes to this document.
- document.Save();
- }// Release this document from memory.
The code directly above will generate a document that looks like the image below. Notice that the second page has an empty Header and Footer, this will also be the same for all proceeding pages.
The third code example generates a document which has a different Header and Footer for odd and even pages.
Odd and Even Header and Footer
- // Create a new document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add Header and Footer support to this document.
- document.AddHeaders();
- document.AddFooters();
-
- // Get the odd and even Headers for this document.
- Header header_odd = document.Headers.odd;
- Header header_even = document.Headers.even;
-
- // Get the odd and even Footer for this document.
- Footer footer_odd = document.Footers.odd;
- Footer footer_even = document.Footers.even;
-
- // Insert a Paragraph into the odd Header.
- Paragraph p1 = header_odd.InsertParagraph();
- p1.Append("Hello Odd Header.").Bold();
-
- // Insert a Paragraph into the even Header.
- Paragraph p2 = header_even.InsertParagraph();
- p2.Append("Hello Even Header.").Bold();
-
- // Insert a Paragraph into the document.
- Paragraph p3 = document.InsertParagraph();
- p3.AppendLine("Hello First page.");
-
- // Create a second page to show that even and odd pages have different headers and footers.
- p3.InsertPageBreakAfterSelf();
-
- // Insert a Paragraph after the page break.
- Paragraph p4 = document.InsertParagraph();
- p4.AppendLine("Hello Second page.");
-
- // Insert a Paragraph into the odd Footer.
- Paragraph p5 = footer_odd.InsertParagraph();
- p5.Append("Hello Odd Footer.").Bold();
-
- // Insert a Paragraph into the even Footer.
- Paragraph p6 = footer_even.InsertParagraph();
- p6.Append("Hello Even Footer.").Bold();
-
- // Force odd & even pages to have different Headers and Footers.
- document.DifferentOddAndEvenPages = true;
-
- // Save all changes to this document.
- document.Save();
- }// Release this document from memory.
The code directly above will generate a document that looks like the image below. Note that the first page has a different header and footer than the second.
It is of course possible to create a document with a different header and footer for odd, even and the first page. You just need to set the following two properties to true.
// Force the first, odd & even pages to have different Headers and Footers.
document.DifferentOddAndEvenPages = true;
document.DifferentFirstPage = true;
This version of DocX does not support page numbering inside Headers and Footers. This feature will require much more development time due to the shear number of options available for numbering. I hope to add this feature in the near future.
Happy coding,
Cathal