Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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");
}
}
Further Reading: How to Add Page Numbers in a PDF