Adding Page Numbers to PDFs with C# and .NET

Adding page numbers to a PDF document is a common requirement for both personal and professional documents. It helps in keeping the pages organized and enhances the navigability of the document.

In this article, we will look at the process of adding page numbers to an existing PDF document using PDF.NET.

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF document. Iterate by all pages.
  3. Draw the page numbering using "orange" color atop of all content.
  4. Save the document.

By following the steps outlined in this article, you can quickly implement page numbering in your PDF documents, enhancing their professionalism and readability.

Complete code

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Add page numbers to a PDF document in C# and .NET
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/add-page-numbers-to-a-pdf-document-in-csharp-dotnet.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            string inpFile = Path.GetFullPath(@"..\..\..\Wine turism.pdf");
            string outFile = Path.GetFullPath("Result.pdf");

            using (PdfDocument document = PdfDocument.Load(inpFile))
            {
                // Iterate by all pages
                int pageNum = 1;
                foreach (var page in document.Pages)
                {
                    // Draw the page numbering atop of all.
                    // The method PdfContent(PdfContentGroup).DrawText always adds text to the end of the content.
                    using (var formattedText = new PdfFormattedText())
                    {
                        formattedText.Font = new PdfFont(new PdfFontFace("Helvetica"), 16.0);
                        // Set "orange" color
                        formattedText.Color = PdfColor.FromRgb(1, 0.647, 0);
                        formattedText.AppendLine($"Page {pageNum++}");
                        page.Content.DrawText(formattedText, new PdfPoint((page.CropBox.Width / 2) - formattedText.Width, page.CropBox.Height - 50));

                        // Because of the trial version, we'll add page numbers only to two pages.
                        if (pageNum > 2)
                            break;
                    }
                }
                document.Save(outFile);
            }
            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.