Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
The Portable Document Format (PDF) is a universally used file format that ensures consistency in document presentation across all platforms and devices. Its fixed layout makes it the go-to format for sharing papers, contracts, invoices, and more. PDF files are indispensable in the corporate world for formal documentation. With the growing need for PDF generation and manipulation, several libraries have emerged, simplifying the process for developers.
In this article, we'll explore how to add page numbers to a PDF using QuestPDF in C#, while also comparing QuestPDF with IronPDF to help you decide which library fits your project needs.
IronPDF is a feature-rich library built for the .NET ecosystem, designed to handle PDF creation, manipulation, and rendering tasks efficiently. It leverages a Chromium-based engine to provide precise conversion of HTML, CSS, and JavaScript into PDF documents. This makes it an excellent choice for web developers who need to convert HTML content directly into a PDF format while retaining the original layout and styling.
With IronPDF, you can easily integrate PDF functionalities into your .NET applications, including creating custom headers and footers, adding new pages to your PDFs, embedding images and tables, and performing advanced PDF manipulations such as merging or splitting documents. The library supports various formats and offers a wide range of customization options, making it ideal for generating professional-grade PDFs from dynamic web content.
Key Features of IronPDF:
To dive deeper into IronPDF's capabilities and more advanced examples, refer to the official documentation here.
To add IronPDF to your project, use the NuGet package manager in Visual Studio. You can either use the Visual Command-Line interface or search directly in the NuGet Package Manager.
Command-line installation:
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
Alternatively, you can search for "IronPDF" in the NuGet package manager and install it.
QuestPDF is a modern .NET library designed for PDF document generation. It focuses on providing developers with a flexible and efficient tool for creating PDFs from C#. QuestPDF allows for an intuitive and fluid approach to designing documents using a declarative style.
QuestPDF emphasizes simplicity, speed, and performance, making it a great choice for generating dynamic reports and documents. The library also supports advanced layout features, custom styling, and easy-to-use templates.
QuestPDF Features
For more details, refer to the QuestPDF documentation.
To get started with QuestPDF, install it via the NuGet command line:
Install-Package QuestPDF
Install-Package QuestPDF
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package QuestPDF
Or alternatively, through the NuGet package manager:
This will add the required libraries to your project for generating PDFs with QuestPDF.
IronPDF offers an easy way to add page numbers to PDFs. The following code demonstrates how to do this:
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");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>"
Dim renderer As New ChromePdfRenderer() With {
.RenderingOptions = {
HtmlHeader = New HtmlHeaderFooter With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}
}
}
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("pageNumbers.pdf")
End Sub
End Class
In this code, we create an HTML header for the PDF document, where {page} and {total-pages} represent dynamic placeholders for the current page number and total pages. The RenderHtmlAsPdf method converts the HTML into a PDF. With this feature, you can choose to include a single page or however many of the next pages within the PDF.
In QuestPDF, adding page numbers can be done in a similar fashion. Below is the code to add page numbers using QuestPDF:
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
using QuestPDF;
class Program
{
static void Main(string[] args)
{
QuestPDF.Settings.License = LicenseType.Community;
var document = Document.Create(container =>
{
container.Page(page =>
{
page.Content().Text("Hello, QuestPDF!");
page.Header().AlignCenter().Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
});
document.GeneratePdf("QuestPdfOutput.pdf");
}
}
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
using QuestPDF;
class Program
{
static void Main(string[] args)
{
QuestPDF.Settings.License = LicenseType.Community;
var document = Document.Create(container =>
{
container.Page(page =>
{
page.Content().Text("Hello, QuestPDF!");
page.Header().AlignCenter().Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
});
document.GeneratePdf("QuestPdfOutput.pdf");
}
}
Imports QuestPDF.Fluent
Imports QuestPDF.Infrastructure
Imports QuestPDF
Friend Class Program
Shared Sub Main(ByVal args() As String)
QuestPDF.Settings.License = LicenseType.Community
Dim document = Document.Create(Sub(container)
container.Page(Sub(page)
page.Content().Text("Hello, QuestPDF!")
page.Header().AlignCenter().Text(Sub(text)
text.Span("Page ")
text.CurrentPageNumber()
text.Span(" of ")
text.TotalPages()
End Sub)
End Sub)
End Sub)
document.GeneratePdf("QuestPdfOutput.pdf")
End Sub
End Class
This QuestPDF code defines a simple document with a page number in the header. The CurrentPageNumber() and TotalPages() methods are used to dynamically generate the page number relative to each page.
In conclusion, both IronPDF and QuestPDF offer effective solutions for adding page numbers to PDFs in C#. However, IronPDF provides a more streamlined and user-friendly approach. Its flexibility and ease of use make it an ideal choice for developers needing to add page numbers or manipulate existing PDFs with minimal effort.
IronPDF is available for free development use, allowing developers to experiment and integrate it into projects without any cost during the development phase. Once you're ready for production, commercial licensing for these licensing options.
By choosing IronPDF, developers gain access to a reliable and feature-rich tool that simplifies PDF creation and editing, including page number insertion, with the added benefit of ongoing maintenance and updates.
For more information on IronPDF's free version and commercial licensing, visit IronPDF's official website.
10 .NET API products for your office documents