Insert Header/Footer in Word Document in C#

Word header appears in the top of each page in a document, while Word footer appears in the bottom. Header and footer can be text or images, for example, document title, time and date, page numbers, company logo etc. Also, users can change or format header/footer contents according to own requirement.

Spire.Doc for .NET, a professional component to manipulate Word document with .NET, enables developers to insert header/footer in Word document by using C#/VB.NET. And this guide will show details about how to realize this function.

Users can set header or footer for Word document through HeadersFooters property of Section Class which Spire.Doc for .NET provides. Then, invoke header/footer.AddParagraph() method to add header/footer paragraph and add contents by invoking paragraph.AppendText(string) method. Now Download and Install Spire.Doc for .NET. Then, use the following code to insert header/footer in Word.

Insert Word Header

[C#]
           //Add Header
            Section section = document.Sections[0];
            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph HParagraph = header.AddParagraph();
            TextRange HText = HParagraph.AppendText("Spire.Doc for .NET");

            //Set Header Text Format
            HText.CharacterFormat.FontName = "Algerian";
            HText.CharacterFormat.FontSize = 15;
            HText.CharacterFormat.TextColor = Color.RoyalBlue;

            //Set Header Paragraph Format
            HParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
            HParagraph.Format.Borders.Bottom.BorderType=BorderStyle.ThickThinMediumGap;
            HParagraph.Format.Borders.Bottom.Space = 0.05f;
            HParagraph.Format.Borders.Bottom.Color = Color.DarkGray;
[VB.NET]
             'Add Header
            Dim section As Section = document.Sections(0)
            Dim header As HeaderFooter = section.HeadersFooters.Header
            Dim HParagraph As Paragraph = header.AddParagraph()
            Dim HText As TextRange = HParagraph.AppendText("Spire.Doc for .NET")

            'Set Header Text Format
            HText.CharacterFormat.FontName = "Algerian"
            HText.CharacterFormat.FontSize = 15
            HText.CharacterFormat.TextColor = Color.RoyalBlue

            'Set Header Paragraph Format
            HParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left
            HParagraph.Format.Borders.Bottom.BorderType = BorderStyle.ThickThinMediumGap
            HParagraph.Format.Borders.Bottom.Space = 0.05F
            HParagraph.Format.Borders.Bottom.Color = Color.DarkGray

Header Result:

Insert Text Header/Footer

Insert Word Footer

[C#]
           //Add Footer
            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph FParagraph = footer.AddParagraph();
            TextRange FText = FParagraph.AppendText("E-iceblue Co., Ltd. 2012 All Rights Reserverd");

            //Set Footer Text Format
            FText.CharacterFormat.FontName = "Calibri";
            FText.CharacterFormat.FontSize = 12;
            FText.CharacterFormat.TextColor = Color.DarkCyan;

            //Set Footer Paragrah Format
            FParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
            FParagraph.Format.Borders.Top.BorderType=BorderStyle.ThinThinSmallGap;
            FParagraph.Format.Borders.Top.Space = 0.15f;
            FParagraph.Format.Borders.Color = Color.DarkGray;
[VB.NET]
           'Add Footer
            Dim footer As HeaderFooter = section.HeadersFooters.Footer
            Dim FParagraph As Paragraph = footer.AddParagraph()
            Dim FText As TextRange = FParagraph.AppendText("E-iceblue Co., Ltd. 2012 All Rights Reserverd")

            'Set Footer Text Format
            FText.CharacterFormat.FontName = "Calibri"
            FText.CharacterFormat.FontSize = 12
            FText.CharacterFormat.TextColor = Color.DarkCyan

            'Set Footer Paragrah Format
            FParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right
            FParagraph.Format.Borders.Top.BorderType = BorderStyle.ThinThinSmallGap
            FParagraph.Format.Borders.Top.Space = 0.15F
            FParagraph.Format.Borders.Color = Color.DarkGray

Footer Result:

Insert Text Header/Footer

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.