Add Page Number in Word Document using C#

Hi,

How to implement pagination in word document?

We have a requirement to implement pagination in word document. Please let us know your suggestion to implement pagination in word document.

Thanks.

@nageshcrm,

Please note that Pages are created on the fly when you open a Word document with MS Word. Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your use-case along with a sample input and your expected Word documents. You can create expected document (showing the desired output) manually by using MS Word. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

Hi Awais,

Thanks for your quick response.

We are trying to show page number of each page in word document. Attached document for reference.

SampleReport.docx (14.2 KB)

Thanks.

@nageshcrm,

You can add PAGE and NUMPAGES fields in each Page of Word document by using the following C# code:

C# Code to Add Page Number in Word Document

using Aspose.Words.Fields;

namespace Aspose.Words.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            ApplyLicense();

            Document word = new Document();
            DocumentBuilder builder = new DocumentBuilder(word);

            // Add Page Numbering on Page 1
            builder.Writeln("first page");
            AddPageNumbering(builder);

            // Add Page Numbering on Page 2
            builder.InsertBreak(BreakType.PageBreak);
            builder.Writeln("second 2");
            AddPageNumbering(builder);

            // Add Page Numbering on Page 3
            builder.InsertBreak(BreakType.PageBreak);
            builder.Writeln("third 3");
            AddPageNumbering(builder);

            word.UpdateFields();

            word.Save("page numbering added to .docx");
        }


        public static void AddPageNumbering(DocumentBuilder builder)
        {
            builder.Write("Page ");
            builder.InsertField(FieldType.FieldPage, false);
            builder.Write(" of ");
            builder.InsertField(FieldType.FieldNumPages, false);
        }

        public static void ApplyLicense()
        {
            License license = new License();
            license.SetLicense("path to .lic");
        }
    }
}

@awais.hafeez

Now I am able to add pagination in document.

Please suggest how to align only pagination to left side in footer section.

Note: In footer we have logos along with pagination. Pagination need to align left and Logos need to align right.

Attached image for reference.

FooterAlign.JPG (6.8 KB)

Thanks.

@nageshcrm,

Please also provide an expected Word DOCX file showing the desired output here for our reference. You can create this file manually by using MS Word. We will then provide you code to achieve the same desired output by using Aspose.Words.

@awais.hafeez

Attached document for reference.

Thanks.

SampleDocument.docx (30.8 KB)

@nageshcrm,

You can build logic on the following C# code of Aspose.Words for .NET to add page numbering and image in footer area of Word document:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;

builder.InsertImage(@"c:\temp\image1.jpg");

builder.Writeln();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;

AddPageNumbering(builder);

doc.UpdateFields();

doc.Save("C:\\Temp\\21.10.docx");

public static void AddPageNumbering(DocumentBuilder builder)
{
    builder.Write("Page ");
    builder.InsertField(FieldType.FieldPage, false);
    builder.Write(" of ");
    builder.InsertField(FieldType.FieldNumPages, false);
}