How to Add Page Numbers to PDFs Using C#

In this comprehensive tutorial, learn how to add page numbers to PDFs using the Iron PDF library in a C#.NET application. Begin by ensuring the Iron PDF NuGet package is installed in your project. Start by importing the Iron PDF namespace and setting the license key. Create a text header and an HTML footer to display page numbers on each page. Use the Chrome PDF renderer to render HTML content into a PDF and add the headers and footers to ensure page numbers are displayed. Save the PDF with the 'Save As' method.

The tutorial also covers adding page numbers to specific pages. Define a multi-page HTML string to create a multi-page PDF. Use the Enumerable.Range method to generate page indices and apply headers to even, odd, or specific pages, such as the first or last page. You can also skip adding headers to certain pages and start numbering from a specific page. This method offers flexibility in managing page numbers in PDF documents. Explore further features of Iron PDF for advanced documentation generation. For more tutorials, subscribe to Iron Software's channel and try out Iron PDF by signing up for a trial on their website.

using IronPdf; // Import the Iron PDF namespace to use its features

class Program
{
    static void Main()
    {
        // Set the license key for Iron PDF library
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Create a PDF document
        HtmlToPdf renderer = new HtmlToPdf();

        // Create HTML content for the PDF
        string htmlContent = "<html><body><h1>Welcome to Iron PDF Tutorial</h1></body></html>";

        // Render the HTML to a PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);

        // Define a text header
        var headerHtml = "<div style='font-size: 14px;'>Header - Page {page} of {total-pages}</div>";

        // Define a footer with page numbers
        var footerHtml = "<div style='text-align: center; font-size: 12px;'>Page {page} of {total-pages}</div>";

        // Add header and footer to the document
        pdf.Header = new PdfTemplate(headerHtml);
        pdf.Footer = new PdfTemplate(footerHtml);

        // Save the PDF to a file
        pdf.SaveAs("output.pdf");
    }
}
using IronPdf; // Import the Iron PDF namespace to use its features

class Program
{
    static void Main()
    {
        // Set the license key for Iron PDF library
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Create a PDF document
        HtmlToPdf renderer = new HtmlToPdf();

        // Create HTML content for the PDF
        string htmlContent = "<html><body><h1>Welcome to Iron PDF Tutorial</h1></body></html>";

        // Render the HTML to a PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);

        // Define a text header
        var headerHtml = "<div style='font-size: 14px;'>Header - Page {page} of {total-pages}</div>";

        // Define a footer with page numbers
        var footerHtml = "<div style='text-align: center; font-size: 12px;'>Page {page} of {total-pages}</div>";

        // Add header and footer to the document
        pdf.Header = new PdfTemplate(headerHtml);
        pdf.Footer = new PdfTemplate(footerHtml);

        // Save the PDF to a file
        pdf.SaveAs("output.pdf");
    }
}
$vbLabelText   $csharpLabel

Further Reading: How to Add Page Numbers in a PDF

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Compress PDFs in C#
NEXT >
How to Set and Edit PDF Metadata in C#