USING IRONPDF

How to Build a C# PDF Generator

Updated August 6, 2024
Share:

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. This tutorial will use the Windows Form application template.

Open Visual Studio.

Csharp Pdf Generator 1 related to Create a Visual Studio Project

Click on "Create New Project"

Csharp Pdf Generator 2 related to Create a Visual Studio Project

Select "Windows Form App" from the template and then click 'Next'. The following window will pop up. Name the project.

Naming the Project

Naming the Project

After that, click 'Next' to bring up the next window. From the drop-down menu choose a .NET Framework.

Selecting .NET Framework

Selecting .NET Framework

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

Csharp Pdf Generator 5 related to Create a Visual Studio Project

Installing the IronPDF Library

There are three ways to install IronPDF in your project.

Package Manager Console

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

Install-Package IronPdf
Csharp Pdf Generator 6 related to Package Manager Console

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.

Csharp Pdf Generator 7 related to NuGet Package Manager Solution

As an alternative, the IronPDF.Dll 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.

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:

Csharp Pdf Generator 8 related to Designing the Windows Form

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. The SaveFileDialog will be used to ask the user to select a file path and file name.

Add the following code inside the GeneratePDFFromTex_Click Function.

IronPdf.License.LicenseKey = "YourLicenseKey";

// 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 ChromePdfRenderer();
    HtmlLine.RenderHtmlAsPdf(PdfText.Text).SaveAs(filename);
    // MessageBox to display that file save
    MessageBox.Show("PDF Generated Successfully!");
}
IronPdf.License.LicenseKey = "YourLicenseKey";

// 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 ChromePdfRenderer();
    HtmlLine.RenderHtmlAsPdf(PdfText.Text).SaveAs(filename);
    // MessageBox to display that file save
    MessageBox.Show("PDF Generated Successfully!");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

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

On line 3, the initial directory path is set to the D drive.

On line 5, the code example sets the default extension property to pdf, as we are only dealing with PDF files here.

Lines 11 - 16 contain the actual code that will generate the PDF file. Notice that IronPDF can 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:

Csharp Pdf Generator 9 related to Run the Project

Write your text inside the text box. An example is given below.

<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
Csharp Pdf Generator 10 related to Run the Project

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

Csharp Pdf Generator 11 related to Run the Project

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

Output PDF File

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

Csharp Pdf Generator 12 related to Output PDF File

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.

IronPdf.License.LicenseKey = "YourLicenseKey";

var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderUrlAsPdf(URL.Text);
PDF.SaveAs("IronPDF.pdf");
IronPdf.License.LicenseKey = "YourLicenseKey";

var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderUrlAsPdf(URL.Text);
PDF.SaveAs("IronPDF.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

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

Use the SaveAs function to save the generated PDF in the default directory.

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:

Csharp Pdf Generator 13 related to Run the Project

Output PDF Document

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

Csharp Pdf Generator 14 related to Output PDF Document

Summary

This tutorial shows how to generate a PDF File in C# with just a few lines of code. Learn more about converting HTML to from the full HTML to PDF Tutorial.

Learn more about Iron Software products, and try IronPDF for free for 30-days.

The Iron Suite includes nine .NET libraries. Get all nine products for the price of two Iron product libraries. For more information, please click here.

< PREVIOUS
How to Remove a Password from a PDF File
NEXT >
C# Create PDF File Programmatically

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,622,374 View Licenses >