PDF Generation in C# (.NET): A Complete Overview of Libraries and Tools
Creating PDF documents is one of the most common tasks in enterprise development. Reports, invoices, waybills, contracts — all of these require a reliable generation tool. In the C# (.NET) ecosystem, there are several powerful libraries, each with its own features. In this article, we will review the most popular options, their pros and cons, and provide code examples.
1. iTextSharp / iText 7 for .NET
iText is the de facto standard in the world of PDF generation. The library has existed since 2000 and is actively developed. For .NET, version iText 7 is available (NuGet package itext7).
Features:
- Full control over content: text, tables, images, annotations.
- Working with forms (AcroForms), digital signatures, encryption.
- Support for PDF/A (archival storage).
- High performance.
Disadvantages:
- Complex API (low-level work with coordinates).
- Commercial use requires a paid license (AGPL for open-source).
Example of creating a simple PDF:
using iText.Kernel.Pdf;using iText.Layout;using iText.Layout.Element;
public void CreatePdf(string filePath){ using (var writer = new PdfWriter(filePath)) using (var pdf = new PdfDocument(writer)) using (var document = new Document(pdf)) { document.Add(new Paragraph("Hello, world!")); document.Add(new Paragraph("This is a PDF created with iText 7.")); }}2. PDFsharp
PDFsharp is a lightweight open-source library for .NET (also available for GDI+). It is well suited for simple tasks: creating documents with text, tables, and images.
Features:
- Simple and intuitive API.
- Font support (including Cyrillic).
- Working with graphic primitives (lines, shapes).
- Free (MIT license).
Disadvantages:
- Limited support for complex elements (forms, bookmarks).
- Slower than iText on large volumes.
Example:
using PdfSharp.Pdf;using PdfSharp.Drawing;
public void CreatePdf(string filePath){ using (var document = new PdfDocument()) { var page = document.AddPage(); using (var gfx = XGraphics.FromPdfPage(page)) { var font = new XFont("Arial", 20, XFontStyle.Regular); gfx.DrawString("Hello, world!", font, XBrushes.Black, new XPoint(40, 100)); } document.Save(filePath); }}3. QuestPDF
QuestPDF is a modern library with a fluent API, inspired by the "layout-first" concept. It allows you to describe the document structure declaratively, which greatly simplifies the creation of complex layouts.
Features:
- Declarative style (like WPF or HTML).
- Automatic pagination.
- Support for tables, headers, and footers.
- Free version with limitations (up to 10 pages).
Disadvantages:
- Free version is limited.
- Less flexibility than iText (difficult to draw arbitrary graphics).
Example:
using QuestPDF.Fluent;using QuestPDF.Helpers;using QuestPDF.Infrastructure;
public void CreatePdf(string filePath){ Document.Create(container => { container.Page(page => { page.Size(PageSizes.A4); page.Margin(30); page.Content().Column(col => { col.Item().Text("Hello, world!").FontSize(20); col.Item().Text("This is QuestPDF.").FontSize(14); }); });