PDF Generation in C# (.NET): Overview of the Best Libraries
Generating PDF documents is one of the common tasks in corporate and web applications in C#. Reports, invoices, waybills, certificates — all of these are conveniently generated in PDF format. In the .NET ecosystem, there are several powerful libraries, both paid and free. In this article, we will review the most popular tools, their capabilities, and provide code examples.
Popular Libraries for Working with PDF in .NET
- iTextSharp / iText 7 — one of the oldest and most feature-rich libraries. Paid version (iText 7) for commercial use, but there is a legacy version iTextSharp under LGPL.
- PdfSharp / MigraDoc — a free, open-source library. Great for simple documents and reports.
- QuestPDF — a modern library with a fluent API. Free for commercial use (MIT license). Allows you to create complex documents with minimal code.
- IronPDF — a paid library, but with very powerful functionality: HTML to PDF conversion, working with forms, password protection.
- SelectPdf — another paid tool with HTML to PDF support and many settings.
Installing Libraries via NuGet
All libraries are installed in the standard way via the NuGet package manager. Installation examples:
// For PdfSharpInstall-Package PdfSharp
// For QuestPDFInstall-Package QuestPDF
// For iTextSharp (legacy)Install-Package iTextSharp.LGPLv2.CoreExample 1: Creating a PDF with PdfSharp
PdfSharp is an excellent choice for simple tasks. Below is an example of creating a document with text and a table:
using PdfSharp.Pdf;using PdfSharp.Drawing;
class Program{ static void Main() { // Create a new PDF document PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page);
// Draw text XFont font = new XFont("Arial", 14); gfx.DrawString("Hello, PDF world!", font, XBrushes.Black, new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);
// Save the document document.Save("HelloWorld.pdf"); Console.WriteLine("PDF created!"); }}Example 2: Generating a Report with QuestPDF
QuestPDF offers an elegant Fluent API. Let's create a simple invoice:
using QuestPDF.Fluent;using QuestPDF.Helpers;using QuestPDF.Infrastructure;
class Program{ static void Main() { Document.Create(container => { container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.DefaultTextStyle(x => x.FontSize(12));
page.Header().Element(ComposeHeader); page.Content().Element(ComposeContent); page.Footer().AlignCenter().Text("Page {0} of {1}", x => x.CurrentPageNumber(), x => x.TotalPages()); }); }).GeneratePdf("Invoice.pdf");
Console.WriteLine("Invoice created!"); }
static void ComposeHeader(IContainer container) { container.Row(row => { row.RelativeItem().Column(column => { column.Item().Text("INVOICE").Bold().FontSize(20); column.Item().Text($"Date: {DateTime.Now:dd.MM.yyyy}"); }); row.ConstantItem(100).Image(Placeholders.Image(100, 100)); }); }
static void ComposeContent(IContainer container) { container.Table(table => { // Table headers table.ColumnsDefinition(columns =>