QuestPDF add page numbers to a PDF Alternatives VS IronPDF (Example)
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.
What is IronPDF?
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:
- Allows you to generate PDFs directly from C# code.
- Converts web pages, HTML, and JavaScript to high-quality PDFs.
- Provides options for adding custom elements like headers, footers, and watermarks.
- Facilitates merging, splitting, and editing existing PDFs.
- Works seamlessly with .NET applications, including ASP.NET and MVC frameworks.
To dive deeper into IronPDF's capabilities and more advanced examples, refer to the official documentation here.
Installing IronPDF
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
Alternatively, you can search for "IronPDF" in the NuGet package manager and install it.
What is QuestPDF?
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
- Easy-to-use API for building complex PDF documents.
- Supports flexible layout and document structuring, setting default pages, column items, and so on.
- Allows for easy styling of elements using CSS-like properties.
- Provides support for images, default text style settings, tables, barcodes, charts, columns, rows, multiple page types and more.
- Great for creating reports, invoices, and data-driven documents.
For more details, refer to the QuestPDF documentation.
Installing QuestPDF
To get started with QuestPDF, install it via the NuGet command line:
Install-Package QuestPDF
Or alternatively, through the NuGet package manager:
This will add the required libraries to your project for generating PDFs with QuestPDF.
Adding Page Numbers using IronPDF
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)
{
// HTML content for the PDF
var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";
// Set up the IronPDF renderer with header for page numbers
ChromePdfRenderer renderer = new ChromePdfRenderer
{
RenderingOptions =
{
HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
}
}
};
// Render the HTML as a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the PDF to a file
pdf.SaveAs("pageNumbers.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// HTML content for the PDF
var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";
// Set up the IronPDF renderer with header for page numbers
ChromePdfRenderer renderer = new ChromePdfRenderer
{
RenderingOptions =
{
HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
}
}
};
// Render the HTML as a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the PDF to a file
pdf.SaveAs("pageNumbers.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' HTML content for the PDF
Dim html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>"
' Set up the IronPDF renderer with header for page numbers
Dim renderer As New ChromePdfRenderer With {
.RenderingOptions = {
HtmlHeader = New HtmlHeaderFooter With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}
}
}
' Render the HTML as a PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
' Save the PDF to a file
pdf.SaveAs("pageNumbers.pdf")
End Sub
End Class
Output
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. This feature allows pages to be adjusted dynamically based on the document's length.
How to Add Page Numbers using QuestPDF
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;
class Program
{
static void Main(string[] args)
{
// Set the license type for QuestPDF
QuestPDF.Settings.License = LicenseType.Community;
// Create a PDF document using the QuestPDF fluent API
var document = Document.Create(container =>
{
// Define a page with content and a header with page numbers
container.Page(page =>
{
page.Content().Text("Hello, QuestPDF!");
// Add a centered header with page number and total pages
page.Header().AlignCenter().Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
});
// Generate and save the PDF document
document.GeneratePdf("QuestPdfOutput.pdf");
}
}
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
class Program
{
static void Main(string[] args)
{
// Set the license type for QuestPDF
QuestPDF.Settings.License = LicenseType.Community;
// Create a PDF document using the QuestPDF fluent API
var document = Document.Create(container =>
{
// Define a page with content and a header with page numbers
container.Page(page =>
{
page.Content().Text("Hello, QuestPDF!");
// Add a centered header with page number and total pages
page.Header().AlignCenter().Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
});
// Generate and save the PDF document
document.GeneratePdf("QuestPdfOutput.pdf");
}
}
Imports QuestPDF.Fluent
Imports QuestPDF.Infrastructure
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Set the license type for QuestPDF
QuestPDF.Settings.License = LicenseType.Community
' Create a PDF document using the QuestPDF fluent API
Dim document = Document.Create(Sub(container)
' Define a page with content and a header with page numbers
container.Page(Sub(page)
page.Content().Text("Hello, QuestPDF!")
' Add a centered header with page number and total pages
page.Header().AlignCenter().Text(Sub(text)
text.Span("Page ")
text.CurrentPageNumber()
text.Span(" of ")
text.TotalPages()
End Sub)
End Sub)
End Sub)
' Generate and save the PDF document
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.
Conclusion
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 is available 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.
Please note
Frequently Asked Questions
What features does this PDF library offer?
IronPDF allows you to generate PDFs directly from C# code, convert web pages and HTML to high-quality PDFs, add custom elements like headers, footers, and watermarks, and perform advanced manipulations such as merging or splitting documents.
How do you install this .NET library for PDF manipulation?
To install IronPDF, use the NuGet package manager in Visual Studio. You can either use the command line with 'Install-Package IronPdf' or search for IronPDF in the NuGet Package Manager.
What features does QuestPDF offer?
QuestPDF offers an easy-to-use API for building complex PDFs, supports flexible layout and document structuring, and allows easy styling of elements using CSS-like properties. It is great for creating reports, invoices, and data-driven documents.
How do you add page numbers using this library?
IronPDF allows adding page numbers by creating an HTML header for the PDF document, where placeholders like '{page}' and '{total-pages}' represent dynamic page details. The RenderHtmlAsPdf method converts the HTML into a PDF.
How do you add page numbers using QuestPDF?
In QuestPDF, you can add page numbers by defining a page with a header that includes page numbers using the CurrentPageNumber() and TotalPages() methods to dynamically generate the page number relative to each page.
Which library is better for adding page numbers, this one or QuestPDF?
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, making it an ideal choice for developers needing to add page numbers or manipulate PDFs with minimal effort.
Is this PDF library available for free?
Yes, IronPDF is available for free development use, allowing developers to experiment and integrate it into projects without any cost during the development phase. Commercial licensing is available for production use.