Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to create a PDF file in CSharp

PDF (Portable Document Format) is a file format that has to replicate all the characteristics of a printed document that you can read, write, print or forward to someone else. You can create PDF file programmatically from C# applications very quickly. When you create documents, graphics etc. they look just like they would if printed. PDFsharp is the Open Source library that easily creates PDF documents from c# language. PDFSharp library allows you to create PDF files directly from your C# application.

Free PDF Library

There are many PDF libraries available on the web like PDFsharp, iTextSharp etc. The following program uses the PDFsharp library for creating a pdf file from a C# application.

PDFsharp library

PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language. You can freely download the Assemblies version from the following link: Download PDFsharp Assemblies

Steps to create PDF file programmatically.

1. Download the Assemblies from the URL mentioned above.

2. Extract the .zip file to your desired location (

3. Create a New C# Project

4. Add pdfsharp reference in C# Project

5. In Solution Explorer, right-click the project node and click Add Reference. In this project, we are using GDI+ libraries.

6. In the Add Reference dialog box, select the BROWSE tab and select the Assembly file location (step 2)

7. Select all files and click OK

After you add the reference files to your C# project, solution explorer look like the following image.

Now you can start programming to create a New PDF document.

First, you should create a PDF document Object

PdfDocument pdf = new PdfDocument();

Next step is to create an empty page.

PdfPage pdfPage = pdf.AddPage();

Then create an XGraphics Object

Also, create the Font object from XFont

XFont font = new XFont("Verdana", 20, XFontStyle.Bold);

Next step is that you should write the content to PDF File.

graph.DrawString("This is my first PDF document", font, XBrushes.Black,
new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

XStringFormats.Center will place your content to the centre of the PDF page.

Now you can save the document as .pdf


You can specify the file path in the function.

After saving the file, you can double click and open the pdf file. Then you can see the following content in your pdf file.

Drag a Button on the Form and copy and paste the following code in the button1_Click event.

using System;
using System.Windows.Forms;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            PdfDocument pdf = new PdfDocument();
            pdf.Info.Title = "My First PDF";
            PdfPage pdfPage = pdf.AddPage();
            XGraphics graph = XGraphics.FromPdfPage(pdfPage);
            XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
            graph.DrawString("This is my first PDF document", font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
            string pdfFilename = "firstpage.pdf";

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More