Adding page numbers to a PDF document is not only practical but also aesthetically pleasing, as it provides a polished look akin to professionally published materials. Whether you're dealing with a digital copy of a novel, a report, or any other type of lengthy document, having page numbers can significantly improve its readability and utility. In this article, you will learn how to add page numbers to a PDF document in C# using Spire.PDF for .NET.
- Add Left-Aligned Page Numbers in the Footer in C#
- Add Center-Aligned Page Numbers in the Footer in C#
- Add Right-Aligned Page Numbers in the Footer in C#
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 DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
PDF Coordinate System
When utilizing Spire.PDF for .NET to manipulate an existing PDF document, it's important to note that the coordinate system's origin is located at the top left corner of the page. The x-axis extends to the right, while the y-axis extends downward.
Typically, page numbers are positioned within the header or footer section of a document. Therefore, it is crucial to take into account the page size and margins when determining the appropriate placement for the page numbers.
Add Left-Aligned Page Numbers in the Footer in C#
In the Spire.PDF for .NET library, there are two classes available: PdfPageNumberField and PdfPageCountField. These classes allow you to retrieve and display the current page number and the total page count when they are added to a page of a PDF document. If you wish to insert text such as "Page X" or "Page X of Y", you can utilize the PdfCompositeField class, which enables you to combine the desired text with one or more fields into a single field.
The following are the steps to add left-aligned page numbers in the PDF footer using C#.
- Create a Document object.
- Load a PDF file from a specified page.
- Create a PdfPageNumberField object and a PdfPageCountField object.
- Create a PdfCompositeField object to create a "Page X of Y" format.
- Specify the location of the PdfCompositeField object using PdfCompositeField.Location property.
- Iterate though the pages in the document, and add "Page X of Y" to the left corner of the footer section using PdfCompositeField.Draw() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; using Spire.Pdf; using System.Drawing; using Spire.Pdf.License; namespace AddPageNumbersToLeftCorner { class Program { static void Main(string[] args) { // Apply your license key LicenseProvider.SetLicenseKey("License Key"); // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Create font, brush and pen, which determine the appearance of the page numbers to be added PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true); PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 1.0f); // Create a PdfPageNumberField object and a PdfPageCountField object PdfPageNumberField pageNumberField = new PdfPageNumberField(); PdfPageCountField pageCountField = new PdfPageCountField(); // Create a PdfCompositeField object to combine page count field and page number field in a single field PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField); // Get the page size SizeF pageSize = doc.Pages[0].Size; // Set the location of the composite field compositeField.Location = new PointF(72, pageSize.Height - 45); // Iterate through the pages in the document for (int i = 0; i < doc.Pages.Count; i++) { // Get a specific page PdfPageBase page = doc.Pages[i]; // Draw a line at the specified position page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50); // Draw the composite field on the page compositeField.Draw(page.Canvas); } // Save to a different PDF file doc.SaveToFile("AddPageNumbersToLeftCorner.pdf"); // Dispose resources doc.Dispose(); } } }
Add Center-Aligned Page Numbers in the Footer in C#
In order to align the page number in the footer section to the center, it is crucial to dynamically calculate the width of the text "Page X of Y." This calculation is essential as it determines the X coordinate of the page number (PdfCompositeField). To achieve center alignment, the X coordinate is calculated by subtracting the width of the page number from the page width and dividing the result by 2, as follows: (PageWidth - PageNumberWidth)/2.
The following are the steps to add center-aligned page numbers in the PDF footer using C#.
- Create a Document object.
- Load a PDF file from a specified page.
- Create a PdfPageNumberField object and a PdfPageCountField object.
- Create a PdfCompositeField object to create a "Page X of Y" format.
- Specify the location of the PdfCompositeField object using PdfCompositeField.Location property.
- Iterate though the pages in the document, and add "Page X of Y" to the center of the footer section using PdfCompositeField.Draw() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; using Spire.Pdf; using System.Drawing; using Spire.Pdf.License; namespace AddPageNumbersToCenter { class Program { static void Main(string[] args) { // Apply your license key LicenseProvider.SetLicenseKey("License Key"); // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Create font, brush and pen, which determine the appearance of the page numbers to be added PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true); PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 1.0f); // Create a PdfPageNumberField object and a PdfPageCountField object PdfPageNumberField pageNumberField = new PdfPageNumberField(); PdfPageCountField pageCountField = new PdfPageCountField(); // Create a PdfCompositeField object to combine page count field and page number field in a single field PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField); // Iterate through the pages in the document for (int i = 0; i < doc.Pages.Count; i++) { // Get a specific page PdfPageBase page = doc.Pages[i]; // Get the page size SizeF pageSize = doc.Pages[i].Size; // Draw a line at the specified position page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50); // Measure the size the "Page X of Y" SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count)); // Set the location of the composite field compositeField.Location = new PointF((pageSize.Width - pageNumberSize.Width) / 2, pageSize.Height - 45); // Draw the composite field on the page compositeField.Draw(page.Canvas); } // Save to a different PDF file doc.SaveToFile("AddPageNumbersToCenter.pdf"); // Dispose resources doc.Dispose(); } } }
Add Right-Aligned Page Numbers in the Footer in C#
To position the page number in the footer section's right corner, it is essential to dynamically calculate the width of the text "Page X of Y" as well. Because the X coordinate of the page number (PdfCompositeField) is determined by subtracting the combined width of the page number and the right page margin from the page width, as follows: PageWidth - (PageNumberWidth + RightPageMargin).
Below are the steps to add right-aligned page numbers in the PDF footer in C#.
- Create a Document object.
- Load a PDF file from a specified page.
- Create a PdfPageNumberField object and a PdfPageCountField object.
- Create a PdfCompositeField object to create a "Page X of Y" format.
- Specify the location of the PdfCompositeField object using PdfCompositeField.Location property.
- Iterate though the pages in the document, and add "Page X of Y" to the right corner of the footer section using PdfCompositeField.Draw() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; using Spire.Pdf; using System.Drawing; using Spire.Pdf.License; namespace AddPageNumbersToRigthCorner { class Program { static void Main(string[] args) { // Apply your license key LicenseProvider.SetLicenseKey("License Key"); // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Create font, brush and pen, which determine the appearance of the page numbers to be added PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true); PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 1.0f); // Create a PdfPageNumberField object and a PdfPageCountField object PdfPageNumberField pageNumberField = new PdfPageNumberField(); PdfPageCountField pageCountField = new PdfPageCountField(); // Create a PdfCompositeField object to combine page count field and page number field in a single field PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField); // Iterate through the pages in the document for (int i = 0; i < doc.Pages.Count; i++) { // Get a specific page PdfPageBase page = doc.Pages[i]; // Get the page size SizeF pageSize = doc.Pages[i].Size; // Draw a line at the specified position page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50); // Measure the size the "Page X of Y" SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count)); // Set the location of the composite field compositeField.Location = new PointF(pageSize.Width - pageNumberSize.Width - 72, pageSize.Height - 45); // Draw the composite field on the page compositeField.Draw(page.Canvas); } // Save to a different PDF file doc.SaveToFile("AddPageNumbersToRigthCorner.pdf"); // Dispose resources doc.Dispose(); } } }
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.