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:

C#

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);

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

            //Border
            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);
            System.Diagnostics.Process.Start(“Footer.docx”);
        }
    }
}

VB.NET

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)

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

            ‘Border
            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)
            System.Diagnostics.Process.Start(“Footer.docx”)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Freely Download Spire.Doc for .NET

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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