How to Create a Pdf File in C#
Tags: ironpdf, csharp, dotnet, pdffile
Published: 2021-11-26 05:07:29

In this tutorial we will learn how to create a pdf document in C# with IronPdf. We will cover the following topics:

##How to create a PDF file using C#  

1. Download C# Create PDF Library and Open Visual Studio
2. Install the Nuget Package from the Menu Bar
3. Design the Windows Form and Write Code for Creating Pdf
4. Add the Back-End Code for the "Clear and Close" Button
5. Run the Project to See your PDF Document

##[IronPdf](https://ironpdf.com/):
[IronPdf](https://ironpdf.com/) is a .Net library that helps us create and read PDF documents, as well as giving us the tools to manage and adapt PDF documents for our own purposes. The following tutorial will show you how to create PDF files with IronPdf. This tutorial assumes that you already know the basics of C# and Visual Studio, and that you possess a working knowledge of HTML. We need Visual Studio for writing, compiling and running our application, C# for writing logic and code, and HTML for formatting the pdf file, including making titles, headings, adding images, paragraphs, etc. This Library fully supports .Net 5, .Net core, standard and Framework.

We can create a pdf file in C# with just a few lines of code. This is a very easy task given basic knowledge of C# and HTML. You can explore more about Ironpdf with this [URL](https://ironpdf.com/).
[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ty0n8n094v5ovu42qazv.png) ](https://ironpdf.com/)
##Creating a C# Project
I am using Visual Studio 2019, but you can use any version. Visual Studio 2019 is recommended as it provides some wonderful features.

Open Visual Studio 2019.
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hs5yfm72nir9jdggc7v.png)
Click on "Create New Project".
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cfxwunholsuqrx59poi.png)
Select "Windows Form App" from the template and then click 'Next'. The following window will pop up. Name the project. I have named it "Create Pdf using IronPdf".
#####Configure project:
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tylyug0jmrah4tiz2cr.png)
After that, click 'Next' to bring up the next window. From the drop-down menu choose .Net Core 3.1.
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xpulce0ikcqy7sxmfgu.png)
Click on the 'Create' button. The project will be created as shown below:
#####Project:
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61i74v5iufyozv3tcxh5.png)
##Installing IronPdf:
To develop a solution we need to install the [Nuget](https://www.nuget.org/packages/IronPdf) Package. Select "Project" from the Menu Bar, and a drop-down list will appear. Select Manage [NuGet Packages](https://www.nuget.org/packages/IronPdf) from the drop-down menu and click it. The window below will display:
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50enmfk219qqp2mxpcyj.png)
Select the "Browse" tab, and the following window will appear:
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpa5ualhh558c39mjv91.png)
In the search box, type 'IronPdf' and press "Enter". The following window will appear: 
#####Browse Nuget Package Manager:
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt9apf0ojw92ejjkzaos.png)
Select and click on IronPdf. The following window will appear: 
#####Nuget Package:
![How To Create PDF File In C# By Ironpdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mn6cvemjz8k2bj6ew57b.png)
Press on the 'Install' button, and wait for the installation to finish. The following window will appear after successful installation:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enazqp9ftdbicsfao473.png)
Once you have pressed the 'Ok' button, you're ready to go.
The following Readme.txt file will open. 
#####Readme File:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjty5o73xaam01leboxi.png)
I recommend that you click on all of the links and learn more about this Library.
##Designing the Windows Form
The project has been created and the Nuget package has now been installed. The next step is to create a Window form that allows the user to enter text. The content can be saved as a pdf document.
Open Form1 Design
On the left side of the window, click on the 'Toolbar'.
#####ToolBar:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78wx18yenkdx0b45ps6m.png)
We need a label to name our Application. A label can be found by searching for it on the toolbar and then dragging and dropping it onto the Form Design.
Give the label a name. I have named mine as "C# Create Pdf using IronPdf".
Drag and drop one label named "Write Text Here (Use HTML Tags for Formatting)", one Rich text Box, and three buttons (one for saving the text to a pdf file, the other for clearing the text, and the third for closing the window).
#####Form:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vjb8hnycnpoej53tf1u.png)
##Writing Code for Creating Pdf Documents
Double-click on the "Save" Button; the following code will appear:
```
private void Save_Click(object sender, EventArgs e) {}
```
Add the namespace [Ironpdf](https://ironpdf.com/docs/questions/generate-pdf-in-csharp/) at the top of the .cs file.
```
using IronPdf;
```
The actual work starts from this point. We need a file path to save our newly created pdf file. 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 Save_Click Function.  
```
private void Save_Click(object sender, EventArgs e)
{
    // Code for Select the folder to save the file.
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory = @"D:\";      
    saveFileDialog1.Title = "Save 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 create Pdf files
        var HtmlLine = new HtmlToPdf();
        HtmlLine.RenderHtmlAsPdf(PdfText.Text).SaveAs(filename);
        // MessageBox to display that file save
        MessageBox.Show("File Saved Successfully!");
    }
}
```
SaveFileDialog will open a file dialog to select the folder and file name where you want to create a pdf file. 
I have set Initial Directory to D drive, but you can set it to any.
I have set 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 create 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 which 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.
##Add the Back-End Code for the "Clear and Close" Button
Double-click on the "clear" button; the following code will appear:
``` 
private void Clear_Click(object sender, EventArgs e) {}
```
Add the following code inside the Clear_Click function to clear the text fields.
```
private void Clear_Click(object sender, EventArgs e) {
    PdfText.Text = "";
}
```
Now, double-click the "Close" button; the following code will appear: 
``` 
private void Close_Click(object sender, EventArgs e) {}
```
Add the following code inside the Close_Click Function:
```
private void Close_Click(object sender, EventArgs e) {
    this.Dispose();
}
```
##Run the Project
Press Ctrl + F5 to run the Project; the following window will appear:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrgs9ut9np3fei1pnljz.png)
Write your text inside the text box. 
I have written the following text: 
> <h1> This is Sample Pdf File </h1>
> <p> This is the demo for C# Create Pdf using Iron Pdf </p>
> <p> Iron Pdf is a library which provides building functions for creating, reading <br>
> and manipulating pdf files with just few lines of code. </p>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6d0voqrhiqdl3imd34p.png)
Next, click on the 'Save' button to save the file; the following window will appear:
#####File Explorer:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/510z7r2rbf2dnx7na9k5.png)
Select 'Folder' and write 'File' name. I have selected D drive and set the file name as “C# Create Pdf”. Press the 'Save' button; the following message box will appear after the successful creation of a Pdf file: 
#####Message Box:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tebd15iq0hokb2x87y3a.png)
Now, let’s open the pdf file and take a look. 
A file is created as shown below:
#####Output file
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08946iq98w1ugy736n6c.png)
##Summary
In this tutorial we can see that it is very easy to read a pdf file using C#. We can easily convert our html text to a pdf document, our web link to a pdf document, as well as manage the data easily across various applications. For Full tutorial, Please visit this [link](https://ironpdf.com/docs/questions/create-pdf-csharp/)

Iron Software also provides other extremely useful libraries to help us create, read, and manipulate Excel files, generate QR and bar codes, extract text from images using OCR, and much more. Our current special offer gives you the chance to purchase all five libraries for the price of two. For more details, please refer to the following [link](https://ironsoftware.com/suite/).

This brings the guide to a close. I hope you found it easy to follow and comprehend. If you have any questions, please post them in the comments section.

You can download the Project from this [link](https://ironpdf.com/downloads/csharp-create-pdf.zip)