How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (2024)

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

PDFsharpis 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 theMIT License.

Source –http://www.pdfsharp.net/Overview.ashx

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

  1. Launch Visual Studio 2022 and select the option to create a new project
  2. Select project template as Windows Form App (.NET Framework) for C# and click on the Next button
  3. Specify project name, select location to save the project to, select framework as .NET Framework 4.8 and click on Create button
  4. The project should get created and loaded in the visual studio for modifications.
How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (1)

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

  1. 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.
  2. 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
  3. The above steps should add references to the required PdfSharp libraries in the project
How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (2)

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

  1. Add a button on the default Win form (Form1.cs) created with the project and name the button as it as Generate PDF
  2. Add the below code to create Pdf file in C# in the click event of the button
//Create PDF DocumentPdfDocument document = new PdfDocument();//You will have to add Page in PDF DocumentPdfPage page = document.AddPage();//For drawing in PDF Page you will nedd XGraphics ObjectXGraphics gfx = XGraphics.FromPdfPage(page);//For Test you will have to define font to be usedXFont font = new XFont("Verdana", 20, XFontStyle.Bold);//Finally use XGraphics & font object to draw text in PDF Pagegfx.DrawString("My First PDF Document", font, XBrushes.Black,new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);//Specify file name of the PDF filestring filename = "FirstPDFDocument.pdf";//Save PDF Filedocument.Save(filename);//Load PDF File for viewingProcess.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

  1. Press F5 to compile & run our code. This should launch the Form with Generate PDF button as shown on the screen below.
  2. 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#
  3. 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.
How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (3)

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

How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (4)

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 underMIT License

PDF Sharp is not supported in the .NET Core framework. For .NET core based applications you can check NuGet packagePdfSharpCore

You can also check my other article onAccurately 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

Hope you found this article useful. Please support the Author

How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (5)

How to Create PDF file in C# .NET – 5 Easy Steps | Pro Code Guide (2024)
Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5344

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.