Generating PDF Reports in C# (.NET): iTextSharp, QuestPDF, PDFsharp

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Generating PDF Reports in C# (.NET): Overview of the Best Libraries



Creating PDF reports is one of the most common tasks in corporate applications built with C# (.NET). Whether it's exporting data for a client, generating an invoice, or a complex analytical dashboard, you need a reliable tool. In this article, we will review three main libraries: iTextSharp (and its successor iText 7), QuestPDF, and PDFsharp. You will learn which one to choose for a specific task and see working code examples.



1. iTextSharp / iText 7 — Classic and Powerful



iTextSharp is a port of the popular Java library iText for .NET. Version iText 7 (NuGet package itext7) has been completely rewritten and supports a modern API. The library allows you to create PDFs from scratch, edit existing ones, add watermarks, forms, bookmarks, and metadata.



Key features:

  • Full control over document structure (paragraphs, tables, images, lists).
  • Support for fonts, including Cyrillic (TrueType).
  • Working with forms (AcroForms) and digital signatures.
  • License: AGPL (commercial use requires purchasing a license).


Example: creating a simple report with a table

using iText.Kernel.Pdf;using iText.Layout;using iText.Layout.Element;

public void CreateReport(string filePath){ using (var writer = new PdfWriter(filePath)) using (var pdf = new PdfDocument(writer)) using (var document = new Document(pdf)) { document.Add(new Paragraph("Sales Report") .SetFontSize(20).SetBold());

Table table = new Table(3); table.AddCell("Product"); table.AddCell("Quantity"); table.AddCell("Price");

table.AddCell("Laptop"); table.AddCell("10"); table.AddCell("75000");

table.AddCell("Mouse"); table.AddCell("50"); table.AddCell("1500");

document.Add(table); }}


2. QuestPDF — Modern and Concise API



QuestPDF is a relatively young library (since 2021) that is quickly gaining popularity. Its main feature is a Fluent API reminiscent of working with HTML/CSS. The code is readable and compact. QuestPDF is completely free for commercial use (MIT license).



Features:

  • Declarative layout description: containers, columns, rows.
  • Built-in support for styles, colors, borders, and backgrounds.
  • Automatic text wrapping and table fitting.
  • Export to PDF and XPS.


Example: creating a report using QuestPDF

using QuestPDF.Fluent;using QuestPDF.Helpers;using QuestPDF.Infrastructure;

public void CreateInvoice(string filePath){ Document.Create(container => { container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.DefaultTextStyle(x => x.FontSize(12));

page.Header().Text("Invoice No. 123") .SemiBold().FontSize(24).FontColor(Colors.Blue.Darken2);

page.Content().Table(table => { table.ColumnsDefinition(columns => { columns.ConstantColumn(100); columns.RelativeColumn(); columns.ConstantColumn(80); });

table.Header(header => { header.Cell().Text("Name"); header.Cell().Text("Description"); header.Cell().Text("Amount"); });

table.Cell().Text("Consultation"); table.Cell().Text("Architecture development"); table.Cell().Text

Recommendations