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.
Frequently Asked Questions
How can I add page numbers to a PDF document using C#?
You can add page numbers to a PDF using IronPDF by creating an HTML header or footer with placeholders like {page}
and {total-pages}
. These placeholders dynamically update to reflect the current page and total page count when using the RenderHtmlAsPdf
method.
What are the key differences between IronPDF and QuestPDF for PDF manipulation?
IronPDF is feature-rich, leveraging a Chromium-based engine, and is ideal for web developers needing precise layout control. It supports HTML, CSS, and JavaScript conversion into PDFs. QuestPDF offers a modern declarative API, focusing on simplicity and performance, suitable for dynamic reports with flexible layouts.
How do I install a PDF library in a .NET project?
To install a PDF library like IronPDF in a .NET project, use the NuGet Package Manager in Visual Studio. You can install it using the command line with Install-Package IronPdf
or find it in the NuGet Package Manager interface.
What advantages does IronPDF offer for web developers?
IronPDF is advantageous for web developers as it can convert HTML, CSS, and JavaScript into PDFs, maintaining precise layout and styling. It also supports adding custom headers, footers, and advanced document manipulations such as merging and splitting PDFs.
Is it possible to use IronPDF for free?
Yes, IronPDF can be used for free during the development phase, allowing developers to integrate and test its features without cost. However, a commercial license is required for production use.
What are the benefits of using a PDF library in C# for document management?
Using a PDF library like IronPDF in C# simplifies document management by enabling easy generation, manipulation, and conversion of PDFs. It provides tools to maintain consistent document presentation and supports advanced features like adding page numbers, custom headers, and merging documents.
Can I customize the appearance of PDFs with IronPDF?
Yes, IronPDF allows customization of PDFs by utilizing HTML and CSS for styling. You can create custom headers, footers, and watermarks, ensuring that the PDFs adhere to specific design requirements.