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
The PDF (Portable Document Format) is a universally accepted format for sharing documents across various platforms. Its ability to preserve formatting, fonts, and layout makes it indispensable for business, legal, and educational purposes. As the need for PDF manipulation grows in software development, the .NET ecosystem provides developers with multiple libraries for working with PDFs. Among them, PDFSharp and IronPDF stand out as powerful solutions for PDF creation and manipulation.
This article will show you how to add page numbers to PDF documents using PDFsharp and IronPDF in C#, and compare the features of both libraries to help you decide which one best suits your needs.
IronPDF is a powerful .NET library designed for seamless PDF creation, manipulation, and rendering. It stands out for its ability to convert HTML, CSS, and JavaScript directly into high-quality PDFs using a Chromium-based engine. This makes IronPDF an ideal solution for developers who need to convert dynamic web pages or complex HTML content into a well-formatted PDF, preserving the layout and style as it appears in the browser.
Key Features of IronPDF:
To explore more advanced features and examples, refer to the official IronPDF documentation.
To add IronPDF to your project, use the NuGet package manager in Visual Studio. You can install it using the Visual Command-Line interface or search directly in the NuGet Package Manager.
Command-line installation:
Install-Package IronPdf
Install-Package IronPdf
Alternatively, you can search for "IronPDF" in the NuGet Package Manager and install it from there.
PDFsharp is a versatile .NET library focused on creating and manipulating PDF documents with high flexibility. Unlike other libraries, PDFsharp allows detailed control over the structure and design of PDFs, making it a great choice for developers who want to create documents from scratch or modify existing PDFs. With its rich API, PDFsharp supports a wide array of document manipulation tasks, including adding text, images, tables, and even page numbers.
Key Features of PDFSharp:
To dive deeper into PDFSharp's functionality and usage, check out the PDFSharp GitHub repository.
To get started with PDFSharp, install the package via NuGet using the command line:
Command-line installation:
Install-Package PDFSharp
Install-Package PDFSharp
Alternatively, you can search for "PDFSharp" in the NuGet Package Manager and install it from there.
IronPDF is a versatile PDF library designed to handle a wide range of PDF operations, including adding page numbers. IronPDF operates internally on a Chromium engine, allowing it to offer precise rendering of HTML content as PDFs. With its simple API, adding page numbers to your PDF is both efficient and straightforward.
The following code is an example of how to add page numbers using IronPDF in C#:
using IronPdf;
class Program
{
static void Main(string[] args)
{
var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";
ChromePdfRenderer renderer = new ChromePdfRenderer()
{
RenderingOptions = {
HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
},
}
};
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("pageNumbers.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";
ChromePdfRenderer renderer = new ChromePdfRenderer()
{
RenderingOptions = {
HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
},
}
};
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("pageNumbers.pdf");
}
}
This code demonstrates how to easily insert page numbers in the header of a PDF document. The HtmlHeaderFooter object is used to specify the header content, and placeholders like {page} and {total-pages} automatically populate with the page number and the total page count respectively.
With this approach, IronPDF simplifies the process of rendering HTML as PDFs while managing page number placement effortlessly.
PDFSharp is a comprehensive library for working with PDFs in C#. It provides tools for creating, modifying, and reading PDFs. Though it is not designed specifically for HTML-to-PDF conversion, it offers robust control over PDF documents, including the ability to add page numbers using custom page events. Whether you wish to add page numbers to just the first page, or second page, a range of pages, or just all of them.
Here’s an example of how to add page numbers using PDFSharp:
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
internal class Program
{
static void Main(string[] args)
{
// Create a new PDF document
var doc = new PdfSharp.Pdf.PdfDocument();
doc.Info.Title = "Page Numbers Example";
// Create an empty first page
PdfPage page = doc.AddPage();
// Add page number in footer
var gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
var font = new PdfSharp.Drawing.XFont("Arial", 12);
gfx.DrawString("Page " + (doc.PageCount), font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XPoint(500, 770));
// Save the document
doc.Save("PdfSharpOutput.pdf");
}
}
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
internal class Program
{
static void Main(string[] args)
{
// Create a new PDF document
var doc = new PdfSharp.Pdf.PdfDocument();
doc.Info.Title = "Page Numbers Example";
// Create an empty first page
PdfPage page = doc.AddPage();
// Add page number in footer
var gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
var font = new PdfSharp.Drawing.XFont("Arial", 12);
gfx.DrawString("Page " + (doc.PageCount), font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XPoint(500, 770));
// Save the document
doc.Save("PdfSharpOutput.pdf");
}
}
In summary, IronPDF stands out for its ability to easily add page numbers to existing PDF documents in one concise block of code, giving you full control over how and where your page numbers are displayed. While PDFsharp is a versatile option for creating and manipulating PDFs with fine-grained control, it does lead to a more manual, harder to implement approach to adding page numbers to PDF files.
IronPDF's commercial licensing is available, allowing users to evaluate its features before committing to a purchase.
For more information on IronPDF, visit the documentation page, and for PDFSharp, check out the GitHub repository.