How to Add Page Numbers to Existing PDF Document in C#

Page numbers of a document are helpful for readers to remember where they leave last time or which page they would like to continue with next time. Adding page numbers helps to make a PDF document well-organized, especially when the PDF document contains a huge number of pages. In this article, we will introduce how to add page numbers to an existing PDF document using Spire.PDF in C#.

Spire.PDF provides a PdfPageNubmerField class, which when added to specific page will shows the page number automatically. Follow parts show you how to add page number field to a PDF document at the specified location.

Code Snippet:

Step 1: Define a DrawPageNumber() method that takes PdfPageCollection and PdfMargins as two arguments. In this method, we create an object of PdfPageNubmerField class, set its properties, and draw the field at the center of the bottom in PDF page.

private static void DrawPageNumber(PdfPageCollection collection, PdfMargins margin)
{
    foreach (PdfPageBase page in collection)
    {
        PdfBrush brush = PdfBrushes.Black;
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);              
        int x = Convert.ToInt32(page.Canvas.ClientSize.Width / 2);
        int y = Convert.ToInt32(page.Canvas.ClientSize.Height-margin.Bottom);
        Rectangle bounds = new Rectangle(x, y, 50, 20);        
        PdfPageNumberField field = new PdfPageNumberField();
        field.Font = font;
        field.Brush = brush;
        field.StringFormat = format;
        field.Bounds = bounds;
        field.Draw(page.Canvas);
    }
}

Step 2: Create an object of PdfDocument class and load an existing PDF file to it.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 3: Set the margins.

PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;

Step 4: Call DrawPageNumber() to add page numbers to the PDF document.

DrawPageNumber(doc.Pages, margin);

Step 5: Save the file.

doc.SaveToFile("AddNumber.pdf", FileFormat.PDF);

Output:

How to Add Page Numbers to Existing PDF Document in C#

Full Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using Spire.Pdf.Widget;
using System.Drawing;
using Spire.Pdf.AutomaticFields;

namespace AddPageNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
            //set the margins
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            //draw page number
            DrawPageNumber(doc.Pages, margin);
            //save the file
            doc.SaveToFile("AddPageNumbers.pdf", FileFormat.PDF);
        }
        private static void DrawPageNumber(PdfPageCollection collection, PdfMargins margin)
        {
            foreach (PdfPageBase page in collection)
            {
                PdfBrush brush = PdfBrushes.Black;
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);              
                int x = Convert.ToInt32(page.Canvas.ClientSize.Width / 2);
                int y = Convert.ToInt32(page.Canvas.ClientSize.Height-margin.Bottom);
                Rectangle bounds = new Rectangle(x, y, 50, 20);        
                PdfPageNumberField field = new PdfPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }
        }
    }
}