Generating PDF Reports in C# (.NET) with QuestPDF

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) with QuestPDF



In modern .NET platform development, there is often a need to create professional PDF documents: reports, invoices, waybills, analytical summaries, and other business documents. The QuestPDF library is a modern, high-performance, and intuitive solution for generating PDFs in C#. It does not require installing third-party utilities (e.g., wkhtmltopdf) and runs entirely in managed .NET code.



In this article, we will thoroughly cover how to get started with QuestPDF, set up a project, and create a simple report with a table, an image, and text formatting.



Installation and Setup



QuestPDF is distributed via NuGet. To install it, run the command in the Package Manager Console or through the Visual Studio/Rider interface:



Install-Package QuestPDF


Or via .NET CLI:



dotnet add package QuestPDF


After installation, you need to initialize the license once. A paid license is required for commercial use, but a free mode with a watermark is available for testing and small projects. To remove the watermark, add the following at the beginning of your program:



QuestPDF.Settings.License = LicenseType.Community;// or for commercial use:// QuestPDF.Settings.License = LicenseType.Professional;


Creating Your First PDF Document



QuestPDF uses a Fluent API — you describe the document structure with sequential method calls. Let's look at a minimal example:



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

// License setup (required)QuestPDF.Settings.License = LicenseType.Community;

// Create the documentDocument.Create(container =>{ container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.DefaultTextStyle(x => x.FontSize(12));

page.Header() .Text("Sales Report") .SemiBold().FontSize(24).FontColor(Colors.Blue.Medium);

page.Content() .PaddingVertical(1, Unit.Centimetre) .Column(col => { col.Item().Text("Date: " + DateTime.Now.ToShortDateString()); col.Item().PaddingBottom(10).LineHorizontal(1); col.Item().Text("Below is a table with data:"); });

page.Footer() .AlignCenter() .Text(x => { x.Span("Page "); x.CurrentPageNumber(); }); });}).GeneratePdf("report.pdf");


This code creates a PDF file with a title, date, horizontal line, and page number in the footer.



Working with Tables



One of the most common tasks is displaying tabular data. QuestPDF provides a powerful API for creating tables with automatic column width adjustment:



container.Page(page =>{    page.Size(PageSizes.A4);    page.Margin(20);

page.Content().Table(table => { // Define columns (width as a percentage of available space) table.ColumnsDefinition(columns => { columns.RelativeColumn(3); // Product Name columns.RelativeColumn(1); // Quantity columns.RelativeColumn(1); // Price columns.RelativeColumn(1); // Total });

// Table header table.Header(header => { header.Cell().Background(Colors.Grey.Lighten2).Padding(5).Text("Product"); header.Cell().Background(Colors.Grey.Lighten2).Padding(5).AlignCenter().Text("Qty"); header.Cell().Background(Colors.Grey.Lighten2).Padding(5).AlignCenter().Text("Price"); header.Cell().Backgrou

Recommendations