C#/VB.NET: Add Header and Footer in Word Documents

In Word documents, you can add time, date, title, reference information, page number, content description and image /logo in header or footer to enrich the document. This article shows how to add headers and footers in C# and VB.NET applications using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

  • Package Manager
PM> Install-Package Spire.Doc

Add Header and Footer

The table gives a list of main classes, properties and methods used in the operation.

Name Description
Document Class Represents a Word document model.
Document. LoadFromFile() Method Load a Word document.
Section Class Represents a section in a Word document.
Document.Sections Property Gets document sections.
HeaderFooter Class Represents a header and footer model for Word.
Section.HeadersFooters.Header Property Gets headers/footers of current section.
Paragraph Class Represents a paragraph in a document.
HeaderFooter. AddParagraph() Method Adds paragraph at end of section.
TextRange Class Represents a text range.
Paragraph.AppendText() Method Appends text to the end of paragraph.
Document. SaveToFile() Method Saves the document to file in Microsoft Word or another file format.

The following are the steps about adding header and footer.

  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;

namespace AddHeaderAndFooter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("input.docx");

            //Get the first section of Word Document
            Section section = document.Sections[0];

            //Get header via HeadersFooters.Header property 
            HeaderFooter header = section.HeadersFooters.Header;

            //Add a paragraph and set paragraph alignment style
            Paragraph headerPara = header.AddParagraph();
            headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left;
            
            //Append text and set font name, size, color,etc.
            TextRange textrange = headerPara.AppendText("E-iceblue Co. Ltd." + "\n Your Office Development Master");
            textrange.CharacterFormat.FontName = "Arial";
            textrange.CharacterFormat.FontSize = 13;
            textrange.CharacterFormat.TextColor = Color.DodgerBlue;
            textrange.CharacterFormat.Bold = true;  
            
            //Get footer, add paragraph and append text
            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph footerPara = footer.AddParagraph();
            footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;
            textrange = footerPara.AppendText("Copyright © 2021  All Rights Reserved.");
            textrange.CharacterFormat.Bold = false;
            textrange.CharacterFormat.FontSize = 11;        
            
            //Save to file
            document.SaveToFile("output.docx", FileFormat.Docx);
        }
    }
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports Spire.Doc.Fields

Namespace AddHeaderAndFooter
	Class Program
		Private Shared Sub Main(args As String())
			'Create an instance of Document class
			Dim document As New Document()

			'Load a Word document
			document.LoadFromFile("input.docx")

			'Get the first section of Word Document
			Dim section As Section = document.Sections(0)

			'Get header via HeadersFooters.Header property 
			Dim header As HeaderFooter = section.HeadersFooters.Header

			'Add a paragraph and set paragraph alignment style
			Dim headerPara As Paragraph = header.AddParagraph()
			headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left

			'Append text and set font name, size, color ,etc.
			Dim textrange As TextRange = headerPara.AppendText("E-iceblue Co. Ltd." + vbLf & " Your Office Development Master")
			textrange.CharacterFormat.FontName = "Arial"
			textrange.CharacterFormat.FontSize = 13
			textrange.CharacterFormat.TextColor = Color.DodgerBlue
			textrange.CharacterFormat.Bold = True


			'Get footer, add paragraph and append text
			Dim footer As HeaderFooter = section.HeadersFooters.Footer
			Dim footerPara As Paragraph = footer.AddParagraph()
			footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center
			textrange = footerPara.AppendText("Copyright © 2021  All Rights Reserved.")
			textrange.CharacterFormat.Bold = False
			textrange.CharacterFormat.FontSize = 11

			'Save to file
			document.SaveToFile("output.docx", FileFormat.Docx)
		End Sub
	End Class
End Namespace

C#/VB.NET: Add Header and Footer in Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.