How to Insert Page Number as Footer in Word with C#/VB.NET

Word Footer, as header, is used to present additional information (brief and short) or page number of one document. It is often put on the bottom of page and usually separated with contents by a border. The footer contents can be document title, company name, even image. In this post, I want to realize function to insert footer in Word with C#/VB.NET and the footer content is set as page number. 

In my example, I will add page number with format X of Y. X is number of current page number. Y is number of total pages.

*.NET Word Component, Spire.Doc for .NETis used to realize this function. 

Detailed Step Shown as Following:

  1. Load document which I want to add footer from computer.
  2. Insert footer in document. Define footer and add footer paragraph. In order to realize format X of Y. I append one field to show current page number firstly. There are two parameters passed to AppendField method: field name and field type. Then, append text of. Finally, append another field to show total page number.
  3. Set format for footer, including font style, font size, font color and alignment.
  4. Add border to separate content and footer. Set border type and top space.
  5. Save and launch file.

Main Coding:


using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertFooter
    class Footer
        static void Main(string[] args)
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\My Documents\Essays.docx”);
            Section section = document.Sections[0];

            //Insert Footer
            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph footerParagraph = footer.AddParagraph();
            footerParagraph.AppendField(“page number”, FieldType.FieldPage);
            footerParagraph.AppendText(” of “);
            footerParagraph.AppendField(“number of pages”, FieldType.FieldNumPages);

            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = “Footer Style”;
            style.CharacterFormat.FontName = “Calibri”;
            style.CharacterFormat.FontSize = 11;
            style.CharacterFormat.TextColor = Color.GhostWhite;
                = Spire.Doc.Documents.HorizontalAlignment.Right;

                = Spire.Doc.Documents.BorderStyle.Single;
            footerParagraph.Format.Borders.Top.Space = 0.05F;

            //Save and Launch
            document.SaveToFile(“Footer.docx”, FileFormat.Docx);


Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace InsertFooter
    Friend Class Footer
        Shared Sub Main(ByVal args() As String)
            ‘Load Document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\My Documents\Essays.docx”)
            Dim section As Section = document.Sections(0)

            ‘Insert Footer
            Dim footer As HeaderFooter = section.HeadersFooters.Footer
            Dim footerParagraph As Paragraph = footer.AddParagraph()
            footerParagraph.AppendField(“page number”, FieldType.FieldPage)
            footerParagraph.AppendText(” of “)
            footerParagraph.AppendField(“number of pages”, FieldType.FieldNumPages)

            Dim style As New ParagraphStyle(document)
            style.Name = “Footer Style”
            style.CharacterFormat.FontName = “Calibri”
            style.CharacterFormat.FontSize = 11
            style.CharacterFormat.TextColor = Color.GhostWhite
            footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right

            footerParagraph.Format.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single
            footerParagraph.Format.Borders.Top.Space = 0.05F

            ‘Save and Launch
            document.SaveToFile(“Footer.docx”, FileFormat.Docx)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Freely Download Spire.Doc for .NET


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s