How to Generate PDF in C# (.NET 5) using PDFSharp

Updated April 21, 2023


The video is an instructional guide on how to use the PDFsharp library to generate a PDF file using C# code. The speaker begins by introducing PDFsharp as a tricky but effective library for generating PDF files. The library has advanced features, and unlike many of its competitors, it is completely free. The speaker emphasizes that despite its complexity, PDFsharp is an excellent choice for generating PDF files using C#.

To begin the tutorial, the speaker presents an example of a generated PDF file that consists of three lines of text. The first line of text is not exactly the first one, while the third line of text appears at the bottom right-hand corner. The speaker notes that the order of the text will make sense once they get into the code. They also explain that they will be using .NET 5 to demonstrate how PDFsharp works.

Install and Setup

Before getting into the code, the speaker highlights that setting up the environment for PDFsharp can be quite challenging. They explain that three NuGet packages must be installed before working with PDFsharp. The first package is PDFsharp itself, which may generate a warning that it may not match with .NET due to its older version. The other two packages are Common System.Drawing.Common and System.Text.Encoding.CodePages.How to Generate PDF in C# (.NET 5) using PDFSharp: Figure 1

The speaker emphasizes that all three packages must be installed before working with PDFsharp; otherwise, the code will throw an error. They explain that the CodePages package is especially important since it provides the encoding support necessary for generating PDF files. The speaker also notes that failure to install the packages is complex and could be difficult to troubleshoot.

To make PDFSharp work properly, the speaker explains that a provider must be registered for System.Text.Encoding. The provider comes from the CodePages package, and the speaker highlights the importance of registering the package to ensure that PDFSharp functions correctly. They explain that failing to register the provider will result in an error, and it can be challenging to troubleshoot.

Creating a PDF File

Next, the speaker presents a simple console application that uses PDFSharp to generate a PDF file. They explain that before using the library, the using statements for PDFSharp Drawing and PDFSharp PDF must be declared. The speaker then presents the code to generate the PDF file, which involves creating a new document, adding a new page, and drawing the text on the page.

The speaker notes that the code for generating the PDF file can be adjusted to include more complex features, such as adding images, tables, and charts. They also explain that PDFSharp provides various features for working with fonts, including embedding fonts in the PDF file.

How to Generate PDF in C# (.NET 5) using PDFSharp: Figure 2 The speaker explains the importance of setting up the environment correctly by installing the necessary NuGet packages and registering a provider for System.Text.Encoding. They also present a simple console application that uses PDFsharp to generate a PDF file and demonstrate how the code can be adjusted to include more complex features. The video is an excellent resource for anyone interested in generating PDF files using C# and PDFsharp.

Creating lists and more complex items

In the second part of the video, the speaker begins by introducing a more complex example of creating a PDF document. He emphasizes that it is pointless to have an empty PDF document, and proceeds to explain how to create a new page using the PDFSharp library. He notes that the process is similar to creating things in Excel or PowerPoint add-ins, and that once the page is created, it works with a reference that does not require the invocation of another method.How to Generate PDF in C# (.NET 5) using PDFSharp: Figure 3

The speaker then introduces the XGraphics variable, which provides methods to draw things like text, lines, and images. The XFont variable is also introduced as a means of setting up a font for use in the PDF document. The speaker notes that the font can be generated somewhere else, but that it is useful to set it up globally if there is a more complex arrangement.

Next, the speaker discusses the DrawString method, which writes text into the PDF file. He notes that the complex part of the method is determining where the text will be drawn, and explains the various parameters involved, such as the font, color, and boundaries of where the text might align. He also explains the use of the XBrushes variable to set the color of the text.

Explaining DrawString and other methods

The speaker then presents several examples of using the DrawString method with different parameters, such as aligning the text in the center, aligning it at the bottom left, and specifying the exact coordinates where the text will be placed. He notes that the last option is the most customizable and offers a great deal of flexibility.How to Generate PDF in C# (.NET 5) using PDFSharp: Figure 4

The speaker explains how to create a table using graphics in the C# programming language. They cover how to insert images, strings, and lines, as well as how to draw arcs and barcodes using the graphics feature. They also explain the importance of trial and error when working with coordinates and how to draw lines to separate rows.

The speaker also discusses the table header and the need for a starting point for values and the line. They explain how to adjust the y position and add rows of records, and how to move to another page when the table exceeds a certain number of records. Finally, they mention the importance of resetting values for each page.


In conclusion, the speaker emphasizes the importance of being familiar with Microsoft Office add-ins when working with PDFSharp, and offers his own course on the topic. He also notes that the library is highly versatile and can be used to create a wide range of PDF documents, from simple text documents to more complex ones with images, graphics, and custom layouts.

IronPDF: A C# PDF Library

IronPDF is a C# PDF library for creating, editing, and manipulating PDF documents. It is designed to be easy to use and integrates with .NET applications seamlessly.

Compared to PDF Sharp, IronPDF offers several advantages. Firstly, it is a commercial product and comes with professional support, which is not available with PDF Sharp. Secondly, IronPDF has better documentation and a more intuitive API, making it easier for developers to get started and work with the library. Additionally, IronPDF has a richer set of features, including advanced PDF creation options and support for working with HTML, CSS, and JavaScript.



Convert HTML to PDF

Professional support

HTML, CSS, and JavaScript Support

Digital signatures


One of the key features of IronPDF is its ability to convert HTML to PDF, making it easy to create PDF documents from web pages. This feature is not available in PDF Sharp, which limits its use cases.

IronPDF also has support for digital signatures and encryption, which is crucial for sensitive documents that require secure handling. This feature is not available in PDF Sharp.

IronPDF is a more powerful and user-friendly PDF library compared to PDF Sharp, with additional features and better support options. However, it is also a commercial product, so it may not be the best choice for developers looking for a free or open-source solution.

What is iText? (C# and Java PDF Library Alternatives Using IronPDF)
A Comparison Between Wkhtmltopdf in Windows & IronPDF

Ready to get started? Version: 2024.6 just released

Start Free Trial Total downloads: 9,580,872
View Licenses >