Table of Contents
Installed via NuGet
PM> Install-Package Spire.PDF
Related Links
Page numbers are essential for both electronic and paper documents. They make it easier for readers to find and access specific parts of a document quickly without having to browse page by page. In this article, you will learn how to add page numbers to Existing PDF documents in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Add Page Numbers to Existing PDF Documents using C# and VB.NET
You can add page numbers to a PDF document using the dynamic fields: PdfPageNumberField, PdfPageCountField, and PdfCompositeField.
As their names suggest, the PdfPageNumberField is used to display page number, the PdfPageCountField is used to display total page count, and the PdfCompositeField is used to combine two or more dynamic fields such as PdfPageNumberField and PdfPageCountField into a single field.
If you only want to add page numbers like Page 1, Page 2, Page 3… to a PDF document, you can just use PdfPageNumberField. If you wish to add page numbers like Page X of Y to the document, you need to use PdfPageNumberField, PdfPageCountField, and PdfCompositeField.
The following steps show you how to add "Page X of Y" page numbers to an existing PDF document:
- Initialize an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Initialize an instance of PdfPageNumberField class.
- Initialize an instance of PdfPageCountField class.
- Initialize an instance of PdfCompositeField class.
- Set the text alignment for the composite field through PdfCompositeField.StringFormat property.
- Loop through each page in the PDF document, then draw the composite field on the specific location of the page using PdfCompositeField.Draw() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddPageNumbers
{
class Program
{
static void Main(string[] args)
{
//Load a PDF document
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Regular), true);
//Create a PdfPageNumberField instance
PdfPageNumberField pageNumber = new PdfPageNumberField();
//Create a PdfPageCountField instance
PdfPageCountField pageCount = new PdfPageCountField();
//Create a PdfCompositeField instance
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", pageNumber, pageCount);
//Set the text alignment for the composite field
compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);
//Loop through the pages
for(int i = 0; i < pdf.Pages.Count; i ++)
{
//Draw composite filed on each page
compositeField.Draw(pdf.Pages[i].Canvas, pdf.Pages[i].Size.Width / 2 - 20, pdf.Pages[i].Size.Height - pdf.PageSettings.Margins.Bottom);
}
//Save the result document
pdf.SaveToFile("AddPageNumbers.pdf");
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.