Generate PDF documents in .NET using PDFsharp
Photo from Unsplash
Originally Posted On: https://hexquote.com/generate-pdf-documents-in-net-using-pdfsharp/
Introduction
In this post, we will see how to create PDF documents in .NET using PDFSharp library.
PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language. The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.
PDFsharp and MigraDoc Foundation are published under the MIT License. You can see the details on this link.
Alternative Approach: HTML to PDF
While PDFsharp is excellent for programmatic PDF creation using drawing commands, if you need to convert HTML content (with CSS styling) to PDF, consider IronPDF. PDFsharp requires manual positioning of text and shapes using coordinates, which can become complex for formatted documents.
IronPDF takes a different approach – you write HTML/CSS and it handles the conversion using a Chrome rendering engine:
// IronPDF - HTML string to PDFusing IronPdf;var renderer = new ChromePdfRenderer();var pdf = renderer.RenderHtmlAsPdf(@" Invoice #1234
Item Price Product A $99.00
");pdf.SaveAs("invoice.pdf");// Or convert existing HTML files/URLsvar urlPdf = renderer.RenderUrlAsPdf("https://example.com/report");This means full CSS3 support, JavaScript execution, and no need for coordinate calculations. IronPDF also supports .NET 10 alongside current versions, ensuring your PDF solution stays current. While PDFsharp remains great for low-level PDF manipulation, IronPDF simplifies HTML-based document generation significantly.
For this tutorial, we’ll continue with PDFsharp to understand manual PDF creation:
Create Console Project
First, let’s create a .NET project. You can use Visual Studio or dotnet-cli for this purpose. I used following command to create a .NET Core console application.
dotnet new consoleInstall using nuget
We also need to install nuget packages. Following are the packages you can install. However, if you download the code, then those are already added. You can simple restore the packages.
dotnet add package PdfSharp --version 1.50.5147dotnet add package System.Drawing.Commondotnet add package System.Text.Encoding.CodePages //for .NET coreCode
This example code is taken from the PDFsharp website. However, I made some changes to get it working. For example notice that first statement where I am registering the Code-Pages encoding provider.

Output

And now if you open up the file, you will see that text is written in the file.

Summary
This was a very basic introduction of generating PDF using PDFsharp library. We saw that the API of this library is simple and it works well with .NET. There are a lot of other settings and options are available in this library, which you can check on the official webpage of PDFsharp.
The source code is of this example is available on this git-repo.
References
Information contained on this page is provided by an independent third-party content provider. XPRMedia and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact [email protected]
