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. Source – http://www.pdfsharp.net/Overview.ashxTable of Contents Toggle
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#
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
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
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
//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;
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
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
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
Here you can download the complete source code for this article demonstrating how to create Pdf file in C# .NET 4.8