How to Build a C# PDF Generator

Generating PDF files is an essential task for developers who need to produce reports and various other business documents. Business reports are very often generated as PDF documents. In this tutorial, we will learn how to generate a simple PDF document in C#.

There are multiple PDF-generating libraries on the market. But the easiest and most useful library for this purpose is IronPDF. We will be using IronPDF throughout this article for generating PDF files.

Let's begin our tutorial.

Create a Visual Studio Project

The very first step is to create a Visual Studio project. You can also open your existing project if you have already created it.

The template I will use for this project is the Windows Form application. You can choose any as per your preferences.

Open Visual Studio 2019.

Click on "Create New Project"

Select "Windows Form App" from the template and then click 'Next'. The following window will pop up. Name the project. I have named it "C # PDF Generator".

Naming the Project

Naming the Project

After that, click 'Next' to bring up the next window. From the drop-down menu choose .NET Core 3.1. You can choose any .NET framework as per your preferences.

Selecting .NET Framework

Selecting .NET Framework

Click on the 'Create' button. The project will be created as shown below:

Installing the IronPDF Library

There are three ways to install IronPDF in your project. I will demonstrate each of these three ways. You can choose whichever one seems best for you.

Package Manager Console

Write the following command in the Package Manager console. It will download and install the package for you.

```shell
:ProductInstall

<div class="content-img-align-center">
    <img src="/static-assets/pdf/blog/csharp-pdf-generator/csharp-pdf-generator-6.png" alt="" class="img-responsive add-shadow">
</div>

### NuGet Package Manager Solution

You can also install the IronPDF Library by using the NuGet Package Solution. Simply follow these steps:

Click on Tools => NuGet Package Manager => Manage NuGet package Solution.

This will open NuGet Package Manager for you. Click on Browse and search for IronPDF, then install the library.

<div class="content-img-align-center">
    <img src="/static-assets/pdf/blog/csharp-pdf-generator/csharp-pdf-generator-7.png" alt="" class="img-responsive add-shadow">
</div>

### Download from the Link

As an alternative, the <a href="/docs/#download-modal" target="_blank">IronPDF.Dll</a> can be downloaded and added to your project as a reference.

Download and unzip IronPDF to a location such as -/Libs within your solution directory.

In the Visual Studio Solution explorer, right-click references. Select Browse "IronPDF.dll".

## Designing the Windows Form

The project has been created and the NuGet package has now been installed. The next step is to create a Front End Design for our C# PDF Generator App. I will use the minimum design for the demonstration. You can use any design depending on your needs.

Go to the ToolBox=> Select Label (for name our example app), and select the "Rich Text Box", "Text Box", "Button".  Our design will look like the following:

<div class="content-img-align-center">
    <img src="/static-assets/pdf/blog/csharp-pdf-generator/csharp-pdf-generator-8.png" alt="" class="img-responsive add-shadow">
</div>

## Write Code for Generating PDF Documents:

Double-click on the "Generate PDF From Text" button; the following code will appear:

`private void GeneratePDFFromText_Click(object sender, EventArgs e) {}`

Add the namespace Ironpdf at the top of the .cs file.

`using IronPdf;`

The actual work starts from this point. We need a file path to save our new PDF document. For that, I am using SaveFileDialog which will ask the user to select a file path and file name.

Add the following code inside the GeneratePDFFromTex_Click Function.

```cs
// Code for Select the folder to save the file.
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = @"D:\";      
            saveFileDialog1.Title = "Generate Pdf File";
            saveFileDialog1.DefaultExt = "pdf";
            saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;
                // actual code that will generate Pdf document from html code
                var HtmlLine = new HtmlToPdf();
                HtmlLine.RenderHtmlAsPdf(PdfText.Text).SaveAs(filename);
                // MessageBox to display that file save
                MessageBox.Show("PDF Generated Successfully!");
            }

SaveFileDialog will open a file dialog to select the folder and file name where you want to generate a PDF file.

I have set the Initial Directory to D drive, but you can set it to any.

I have set the Default Extension to PDF files as we are only dealing with PDF files here.

Inside the "if" condition, I have put the actual code that will generate the PDF file.

Now we can see that we have managed to generate a PDF file with only two lines of code.

PdfText is the name of a Rich Text box that contains the text that will be written in a PDF file.

The filename is the file path and name which the user has selected via SaveFileDialog.

Run the Project

Press Ctrl + F5 to run the project; the following window will appear:

Write your text inside the text box.

I have written the following text:

<h1>C Sharp PDF Generator</h1>

<p>In this tutorial we have learnt to generate PDF Files with just a few lines of code</p>

<p>IronPDF is very easy compared to other PDF Generating Libraries</p>
<h1>C Sharp PDF Generator</h1>

<p>In this tutorial we have learnt to generate PDF Files with just a few lines of code</p>

<p>IronPDF is very easy compared to other PDF Generating Libraries</p>
HTML

Next, click on the 'Generate PDF From Text' button to generate and save the file; the following window will appear:

Select 'Folder' and write 'File' name. Press the 'Save' button.

Output PDF File

Now, let’s open the PDF file and take a look.

A PDF file is generated from HTML String as shown below:

Write Code for Generating PDF Files from URL:

Double-click on the "Generate PDF FROM URL" button; the following code will appear:

`private void GeneratePDF_FROM_URL_Click(object sender, EventArgs e){}

Add the following code inside this function.

var Renderer = new ChromePdfRenderer();
            var PDF = Renderer.RenderUrlAsPdf(URL.Text);
            PDF.SaveAs("IronPDF.pdf");
var Renderer = new ChromePdfRenderer();
            var PDF = Renderer.RenderUrlAsPdf(URL.Text);
            PDF.SaveAs("IronPDF.pdf");
Dim Renderer = New ChromePdfRenderer()
			Dim PDF = Renderer.RenderUrlAsPdf(URL.Text)
			PDF.SaveAs("IronPDF.pdf")
VB   C#

URL.Text is the URL provided by the user via form fields.

I have used the SaveAs function to save the generated PDF in the default directory. You can change it as per your needs.

Let's run the app.

Run the Project

Paste the URL in the URL Field. Click on the "Generate PDF from URL Button". The PDF file will be generated as follows:

Output PDF Document

You can see that the PDF format is the same as the URL.

Summary

In this tutorial, we have learned how to generate a PDF File in C# with just a few lines of code. If you want to learn about converting HTML to PDF by following a full tutorial, please click on this link.

If you want to learn more about Iron Software products, please click on this link. If you decide that you’d like to try it out for yourself, then go ahead and take advantage of the Iron Software 30-day free-trial. You may also want to take a look at the Iron Suite — this complete package includes five .NET Libraries — and the special offer means you can get all five products for the price of just two. For more information, please click here.