Use Free Spire.Doc to Satisfy All Word Header and Footer Tools in C#

Headers and footers in Microsoft Word are widely used to keep documents organized. It is where we insert headers, footers, page numbers, pictures and the date and time to increase the readability. There are two types of headers and footers, one is the first page header and footer, the other is the odd and even headers and footers. Sometimes there is also a need to create various headers, footers and page numbers for sections of a document. It requires a huge amount of work to satisfy all the features described above in C#. So I searched on Nuget and I am very lucky to have found a solution using the community Word library Free Spire.Doc. It works like a charm so I think it's worthwhile to share it with you guys.

This article introduces:

  • How to insert the page number on the header/footer in C#.
  • How to insert and format an image and text mixed header/footer in C#.
  • How to insert a different first page and different odd and even page header/footer in C#.

Please note: this library could create or load Word independently, here Microsoft Word is only used to view the effect.

Part 1: Insert page number on the header/footer

Word documents with multiple pages could display the page number on each page, that usually appears in the header or the footer of the document. The page number could show the total page of a document and which page you are reading. After adding the DLL in the bin folder of the free spire.doc as the reference of Visual Studio, I use the following code to insert a page number into Word in C#.

  1. //Create a Word document and add a section  
  2. Document doc = new Document();  
  3. Section section = doc.AddSection();  
  4.   
  5. //Initial a HeaderFooter class   
  6. HeaderFooter header = doc.Sections[0].HeadersFooters.Header;  
  7. Paragraph headerParagraph = header.AddParagraph();  
  8.   
  9. //Use the AppendField method to get the FieldPage and FieldNumpages  
  10. headerParagraph.AppendField("page number", FieldType.FieldPage);  
  11. headerParagraph.AppendText(" of ");  
  12. headerParagraph.AppendField("number of pages", FieldType.FieldNumPages);  
  13. headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;  
  14.   
  15. //Save and launch the document  
  16. doc.SaveToFile("Test.docx", FileFormat.Docx);  
  17. System.Diagnostics.Process.Start("Test.docx");  
It's also very easy to add the footer page number with a similar method, so I think the preceding code is enough to show the method. Besides, since I have not added extra pages to the new Word document, the page number in the effect only shows 1/1.

header

Part 2: How to insert, format image-text mix header/footer

Compared with text, the image is easier to catch the eye. More often than not, the image and text are used simultaneously to attract more attention and make full use of the advantages of header or footer. Here I use the logo of Wikipedia and its short introduction to illustrate an image and text mixed footer code in C# using the free spire.doc. Please do remember to use “System. Drawing“.
  1. //Create a Word document and initial the footer class  
  2. Document document = new Document();  
  3. Section section = document.AddSection();  
  4. HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;  
  5.   
  6. //Add text and image to the footer  
  7. Paragraph paragraph = footer.AddParagraph();  
  8. DocPicture footerImage = paragraph.AppendPicture(Image.FromFile("Wiki.bmp"));  
  9. TextRange TR = paragraph.AppendText("Supported and Hosted by the non-profit Wikimedia Foundation.");  
  10.   
  11. //Format the text and image  
  12. paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;  
  13. TR.CharacterFormat.FontName = "Calibri";  
  14. TR.CharacterFormat.FontSize = 13;  
  15. TR.CharacterFormat.TextColor = Color.Black;  
  16. TR.CharacterFormat.Bold = true;  
  17.   
  18. // Save the document and launch to see the output  
  19. document.SaveToFile("Test.docx", FileFormat.Docx);  
  20. System.Diagnostics.Process.Start("Test.docx");  
text mix header

It is worthy mentioning that “TextWrappingStyle” and “TextWrappingType” can be used to wrap text around a picture in the footer paragraph:
  1. footerImage.TextWrappingStyle = TextWrappingStyle.Through;  
  2. footerImage.TextWrappingType = TextWrappingType.Left;  
I have also tried to add a table to the footer with this library and it is working. Those who need a table in the footer could have a try.

Part 3: How to insert a different first page and different odd and even page header/footer

By default, Microsoft Word sets the same headers and footers on each page. However, in some situation like reports or books, a different header/footer is necessary to increase the beauty of page layout. If we need the different first page header/footer, of course, we could set the first page as the separate section in programming and the rest as the other sections. Free Spire.doc has provided an easier and faster solution to set a different first page and different odd and even pages directly. Here, I start with the different odd and even page header code in C#.

Different odd and even page header:
  1. //Create a document and section  
  2. Document document = new Document();  
  3. Section section = document.AddSection();  
  4.   
  5. //Set the bool true and add the Odd Header and Even Header  
  6. section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;  
  7. Paragraph oddHeader = section.HeadersFooters.OddHeader.AddParagraph();  
  8. TextRange oddHT = oddHeader.AppendText("Coding is the art of life");  
  9. Paragraph evenHeader = section.HeadersFooters.EvenHeader.AddParagraph();  
  10. TextRange evenHT = evenHeader.AppendText("Time is the most valuable thing");  
  11.   
  12. //Format the headers  
  13. oddHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;  
  14. oddHT.CharacterFormat.FontName = "Calibri";  
  15. oddHT.CharacterFormat.FontSize = 20;  
  16. oddHT.CharacterFormat.TextColor = Color.Green;  
  17. oddHT.CharacterFormat.Bold = true;  
  18. evenHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;  
  19. evenHT.CharacterFormat.FontName = "Calibri";  
  20. evenHT.CharacterFormat.FontSize = 20;  
  21. evenHT.CharacterFormat.TextColor = Color.Green;  
  22. evenHT.CharacterFormat.Bold = true;  
  23.   
  24. //Launch to see effects  
  25. document.SaveToFile("R.docx", FileFormat.Docx2010);  
  26. System.Diagnostics.Process.Start("R.docx");  
footer

It works fine to add a different page number format, pictures or table to different odd and even footer. Please refer to the code I have listed above and it would be very easier to work them out. For different first page header and footer, the code is similar to the different odd and even one. Here I only list the difference between the two for your information:
  1. //set the first page header and footer   
  2. section.PageSetup.DifferentFirstPageHeaderFooter = true;  
  3. Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();  
  4. Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();  
  5. //set the rest page header and footer   
  6. Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();  
  7. Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();  
If you only need the first page header or footer, please don't bother to set the rest of the page header and footer. In this way, you'll only get the first one.

Conclusion

In my opinion, the free Spire.Doc works like a charm when I use it to satisfy the Microsoft Word header and footer tools in C#. It meets my needs and I believe it won't let you down for personal use or some small projects.

Thanks a lot for your reading. Happy coding and have a good day.


Similar Articles