How to Generate PDF Files in C# Windows Application

Generate PDF files in C# windows applications. let’s create a simple desktop application in C# to generate pdf files on a button click.

How to Create PDF Files in C#

Portable Document Format (PDF) is a file format that represents all the properties of a printed document that we can read, write print and forward to someone else. We can create PDF files with the help of programming from our C# Windows Applications easily. PDFsharp is the Open Source library that easily creates and allows us to create PDF files directly from our C# Windows Applications.

We can freely download the Assemblies version from the following link: Download PDFsharp Assemblies-1_3 1.zip

Follow the simple steps for creating a PDF file by code in C# Windows Application.

Step 1:

  •  Download the Required Assemblies from the above-mentioned URL.

  • Extract the .zip file into your System (filename :PDFsharp-MigraDocFoundation-Assemblies-1_31.zip)

Step 2:

Open your Visual Studio > Go to File Menu > Create a New Project > Select n Visual C# Windows Application>

 Name the Project then Click on OK.

Step 3:

  • Now Add the  PDFsharp Assemblies in your VB.Net Project
  •  Go to Solution Explorer, then right-click on the project name and click on Add Reference. Here we are using GDI+ libraries.

  • Add Reference dialogue box will appear, Click on the Browse Tab.
  • choose the Assemblies File in Look In textbox where you had saved it.

  • Select all files and Click on OK.

  • After adding the References your Solution Explorer will look something like this. Now your project is ready to add code.

Step 4:

Import the following Namespaces on top of the Form1.cs.

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

Step 5:

Add one button control from toolbox. Name it Generate PDF, here i have also change the background color of the form.

Double click on the button and add the below code there.

 PdfDocument pdf = new PdfDocument();
            pdf.Info.Title = "Demo PDF";
            PdfPage pdfPage = pdf.AddPage();
            XGraphics graph = XGraphics.FromPdfPage(pdfPage);
            XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
            graph.DrawString("This is my demo pdf document", font, XBrushes.Maroon, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
            string pdfFilename = "Demo.pdf";
            pdf.Save(pdfFilename);
            Process.Start(pdfFilename);

Our full code will look like this.

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace PDF_Generater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PdfDocument pdf = new PdfDocument();
            pdf.Info.Title = "Demo PDF";
            PdfPage pdfPage = pdf.AddPage();
            XGraphics graph = XGraphics.FromPdfPage(pdfPage);
            XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
            graph.DrawString("This is my demo pdf document", font, XBrushes.Maroon, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
            string pdfFilename = "Demo.pdf";
            pdf.Save(pdfFilename);
            Process.Start(pdfFilename);
        }
    }
}

Step  6:

Now Press F5  and run the Application.

Output:

First the Form1 will show to us here when we click on the Show PDF button.

The PDF file will generate and save with name and formatting which we have given in the program. and the PDF document will be displayed to us as shown below.

you can set any name and formatting as you want and also add the text as per your choice as you can see i have set the maroon color in the code that is why my pdf text is looking in this color.

So with the help of these steps, you can also create the PDF file in C# Windows Application.