PDF Generation in C# (.NET): Library Overview and Practical Examples
Generating PDF documents is one of the most common tasks in corporate C# development. Reports, invoices, waybills, contracts — all of these are convenient to generate programmatically. In the .NET ecosystem, there are several mature libraries, each with its own strengths. In this article, we will review four main tools, show code examples, and help you choose the right one for your project.
1. iTextSharp / iText 7 for .NET
iText is perhaps the most well-known library for working with PDF. The iTextSharp version was a port of the Java library for .NET Framework. The current version is iText 7 for .NET (AGPL license for open-source projects or commercial).
Features:
- Creating PDF from scratch
- Working with templates (PdfTemplate)
- Adding images, tables, lists
- Unicode and Cyrillic support
- Digital signatures, encryption
Example: Simple document with a table
using iText.Kernel.Pdf;using iText.Layout;using iText.Layout.Element;
public void CreateSimplePdf(string filePath){ using (PdfWriter writer = new PdfWriter(filePath)) using (PdfDocument pdf = new PdfDocument(writer)) using (Document doc = new Document(pdf)) { doc.Add(new Paragraph("Hello, World!")); Table table = new Table(3); table.AddCell("Name"); table.AddCell("Age"); table.AddCell("City"); table.AddCell("Anna"); table.AddCell("28"); table.AddCell("Moscow"); doc.Add(table); }}2. PDFsharp / MigraDoc
PDFsharp is a lightweight open-source library (MIT). Paired with MigraDoc, it allows you to describe documents in a "word processor" style: paragraphs, styles, headers and footers. It is great for generating simple reports without complex graphics.
Advantages:
- Low entry barrier
- Good Cyrillic support
- No licensing required
- Works with .NET Framework and .NET Core
Example: Creating a document via MigraDoc
using MigraDoc.DocumentObjectModel;using MigraDoc.Rendering;
public void CreateWithMigraDoc(string filePath){ Document doc = new Document(); Section section = doc.AddSection(); Paragraph para = section.AddParagraph("Report Title"); para.Format.Font.Size = 18; para.Format.Font.Bold = true; section.AddParagraph("This is the report text. Generated using PDFsharp and MigraDoc."); PdfDocumentRenderer renderer = new PdfDocumentRenderer(true); renderer.Document = doc; renderer.RenderDocument(); renderer.PdfDocument.Save(filePath);}3. QuestPDF
QuestPDF is a modern library with a fluent interface (syntax similar to LINQ). It is distributed under the MIT license (free for commercial use). It is actively developed and supports .NET 6/7/8.
Key Features:
- Declarative layout building
- Automatic page breaking
- Support for images, SVG, QR codes
- High performance
Example: Generating an invoice
using QuestPDF.Fluent;using QuestPDF.Helpers;using QuestPDF.Infrastructure;
public byte[] GenerateInvoice(){ return 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(x => {