In this article, we will learn about how to create pdf file in C# .NET. Portable Document Format (PDF) can be created in programming languages like C# and these pdf files are preferred for distribution as the name suggests they are portable and will look the same on the recipient machine. Also, the pdf file content can be secured using a file password.
There is no direct support to generate pdf files in the C# programming language instead we will have to make use of a third-party library to generate PDF files. There is a number of libraries available for pdf creation that are paid as well as free libraries.
For demonstration in this article, we will make use of the PDF Sharp library which is a free library to create a pdf file in C# .NET
PDFsharp is a .NET library for processing PDF file. You create PDF pages using drawing routines known from GDI+. Almost anything that can be done with GDI+ will also work with PDFsharp. Only basic text layout is supported by PDFsharp, and page breaks are not created automatically. The same drawing routines can be used for screen, PDF, or meta files.
PDFsharp and MigraDoc Foundation are published under the MIT License.
Table of Contents
Create PDF File in C# .NET
Here are the steps to create a pdf document in C# .NET.
We will be using Visual Studio 2022 and .NET framework 4.8 for the demonstration of creating pdf file in C#
Step 1 – Create a Project of the type Windows Forms App C# .NET
For demonstration purposes, we will be creating a project of the type Windows Form App (.NET Framework) as per the screenshot shown below and name this project as ProCodeGuide.Samples.PDFSharp
Follow the below steps to create a new project
- Launch Visual Studio 2022 and select the option to create a new project
- Select project template as Windows Form App (.NET Framework) for C# and click on the Next button
- Specify project name, select location to save the project to, select framework as .NET Framework 4.8 and click on Create button
- The project should get created and loaded in the visual studio for modifications.
Step 2 – Add Reference to PDF Sharp Library
To create pdf file in C# we will be using the Pdf sharp library so you need to add a reference to this library in the project. Follow the below steps to add a reference to Pdf sharp library
- Right-click on the project file in Solution Explorer and select the menu option ‘Manage NuGet Packages..’, this should load the screen shown below for NuGet Management.
- Next search for PDF Sharp under the Browse tab in the NuGet Management screen and after locating the required library i.e. PdfSharp select & install the stable version of the PdfSharp library as per the screen shown above
- The above steps should add references to the required PdfSharp libraries in the project
Step 3 – Add Code to Generate PDF using PDF Sharp
To create pdf file in c# .NET we need to add code to create a Pdf file using the Pdf library. For demonstration purposes, we will be creating a sample Pdf file that contains the text ‘My First PDF Document” and the name or Pdf file as FirstPDFDocument.pdf.
You can even specify the path where this Pdf file should be created/saved but we have not specified any path so it will be created in the default project launch directory i.e. in bin/debug.
To add code for Pdf creation we will follow the below steps
- Add a button on the default Win form (Form1.cs) created with the project and name the button as it as Generate PDF
- Add the below code to create Pdf file in C# in the click event of the button
//Create PDF Document PdfDocument document = new PdfDocument(); //You will have to add Page in PDF Document PdfPage page = document.AddPage(); //For drawing in PDF Page you will nedd XGraphics Object XGraphics gfx = XGraphics.FromPdfPage(page); //For Test you will have to define font to be used XFont font = new XFont("Verdana", 20, XFontStyle.Bold); //Finally use XGraphics & font object to draw text in PDF Page gfx.DrawString("My First PDF Document", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); //Specify file name of the PDF file string filename = "FirstPDFDocument.pdf"; //Save PDF File document.Save(filename); //Load PDF File for viewing Process.Start(filename);
3. You will also have to include namespaces for PdfSharp Library & Diagnostics. Add the below code at the top in Form1.cs to import the required namespaces.
using PdfSharp.Drawing; using PdfSharp.Pdf; using System.Diagnostics;
Step 4 – Run and Test the Code
Now that we have added the required code to create Pdf file in C# we can run our code and check whether it is giving desired results or not i.e. Pdf file is being created or not.
Follow the below steps to run our code and test
- Press F5 to compile & run our code. This should launch the Form with Generate PDF button as shown on the screen below.
- Click on Generate PDF button, it will execute the code we added on the click event of the button to create Pdf file in C#
- On successful execution of the code in the click event of a button, a Pdf file with the specified name FirstPDFDocument.pdf will be created in the bin/debug folder and will be loaded in the PDF viewer.
Step 5 – View Generated PDF File
Now that the Pdf file was created and loaded in PDF Viewer you should be able to view the Pdf file. Below shown PDF gets created as per the code we added to create pdf file in C# .NET using PdfSharp Library
Summary
We learned in this article how to create a pdf file in C# .NET using PdfSharp Library. PDF Sharp Library is a free open source library covered under MIT License
PDF Sharp is not supported in the .NET Core framework. For .NET core based applications you can check NuGet package PdfSharpCore
You can also check my other article on Accurately Measure Elapsed Time (Execution Time) in C# .NET
Download Source Code
Here you can download the complete source code for this article demonstrating how to create Pdf file in C# .NET 4.8
https://github.com/procodeguide/ProCodeGuide.Samples.PDFSharp
Works on Linux?
No. It is a solution for .NET Framework 4.8 and that is not supported on Linux.
IronPdf is a great alternative that works on Windows, macOS, Linux, Docker, Azure, AWS.
Seems like a good solution but I have had numerous issues implementing it (such as having to resolve the error “No data is available for encoding 1252.” and some other minor ones). The only lingering issue I’m having is the Process.Start(filename) is throwing the Exception “the specified executable is not a valid application for this OS platform”. I really only needed it to save the file so I don’t need any process to start but thought I’d give a heads up to that issue.
Thanks for the article! I appreciate it.
Ive used this example successfully in .Net Core 3.1
The only line that gives a problem is the Process.Start
Excellent article to demonstrate basic functionality.