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

 Often, the header displays additional information about the document at the top of each page. Inserting time, graphics, company microlabels, document titles, file names, or author names is possible. Footer is the area at the bottom of each page in the document. It is often used to display additional information about the document. You can insert text or graphics into the footer. The purpose of today's article is to show you how to add headers and footers to Word documents programmatically. For your reference, I have attached C#/VB.NET code with the ideas and methods I have sorted out.

Programming Environment

In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.8.0

Specific Steps:

The following are the steps about adding header and footer.

  •     Create an instance of Document class.
  •     Load the sample document using Document.LoadFromFile(string fileName) method.
  •     Get the specified section of Word Document using Document.Sections Property

    Add Header

  1.    Get header using HeadersFooters.Header property.
  2.    Add paragraph using HeaderFooter. AddParagraph() method and set paragraph alignment.
  3.    Append text using Paragraph.AppendText(string text) method and set font name, size, color ,etc.

    Add Footer

  1.  Get footer using HeadersFooters.Footer proterty.
  2.  Add paragraph and text in footer.
  3. Save Word document using Document. SaveToFile(string filename, FileFormat fileFormat) method.

Full Code

C#

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("The Great Gatsby.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("The Great Gatsby" + "\nF. Scott Fitzgerald");

            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("We will stand up the tide that keeps going back to the past and strive to move forward.");

            textrange.CharacterFormat.Bold = false;

            textrange.CharacterFormat.FontSize = 11;

            //Save to file

            document.SaveToFile("output.docx", FileFormat.Docx);

        }

    }

}

VB.NET

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports System.Drawing

Imports Spire.Doc.Fields

 

Namespace AddHeaderAndFooter

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())

            'Create an instance of Document class

            Dim document As Document = New Document()

            'Load a Word document

            document.LoadFromFile("The Great Gatsby.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("The Great Gatsby" & vbLf & "F. Scott Fitzgerald")

            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("We will stand up the tide that keeps going back to the past and strive to move forward.")

            textrange.CharacterFormat.Bold = False

            textrange.CharacterFormat.FontSize = 11

            'Save to file

            document.SaveToFile("output.docx", FileFormat.Docx)

        End Sub

    End Class

End Namespace

Effective Shot


Conclusion:

     In this post, you have learned how to add headers and footers to Word documents in C#/VB.NET. Not only that, we also have other functions, such as, Adjust the Height of Headers and Footers in a Word document in C#, Copy Header/Footer between Word Documents in C#, VB.NET and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for .NET.

 


Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application