DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How to Create a Pdf File in C#
Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on • Updated on

How to Create a Pdf File in C#

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:

IronPdf 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.
Image description

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
Click on "Create New Project".
How To Create PDF File In C# By Ironpdf
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
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
Click on the 'Create' button. The project will be created as shown below:

Project:

How To Create PDF File In C# By Ironpdf

Installing IronPdf:

To develop a solution we need to install the Nuget Package. Select "Project" from the Menu Bar, and a drop-down list will appear. Select Manage NuGet Packages from the drop-down menu and click it. The window below will display:
How To Create PDF File In C# By Ironpdf
Select the "Browse" tab, and the following window will appear:
How To Create PDF File In C# By Ironpdf
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
Select and click on IronPdf. The following window will appear:

Nuget Package:

How To Create PDF File In C# By Ironpdf
Press on the 'Install' button, and wait for the installation to finish. The following window will appear after successful installation:
Image description
Once you have pressed the 'Ok' button, you're ready to go.
The following Readme.txt file will open.

Readme File:

Image description
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
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

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) {}
Enter fullscreen mode Exit fullscreen mode

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

using IronPdf;
Enter fullscreen mode Exit fullscreen mode

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!");
    }
}
Enter fullscreen mode Exit fullscreen mode

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) {}
Enter fullscreen mode Exit fullscreen mode

Add the following code inside the Clear_Click function to clear the text fields.

private void Clear_Click(object sender, EventArgs e) {
    PdfText.Text = "";
}
Enter fullscreen mode Exit fullscreen mode

Now, double-click the "Close" button; the following code will appear:

private void Close_Click(object sender, EventArgs e) {}
Enter fullscreen mode Exit fullscreen mode

Add the following code inside the Close_Click Function:

private void Close_Click(object sender, EventArgs e) {
    this.Dispose();
}
Enter fullscreen mode Exit fullscreen mode

Run the Project

Press Ctrl + F5 to run the Project; the following window will appear:
Image description
Write your text inside the text box.
I have written the following text:

This is Sample Pdf File

This is the demo for C# Create Pdf using Iron Pdf

Iron Pdf is a library which provides building functions for creating, reading
and manipulating pdf files with just few lines of code.

Image description
Next, click on the 'Save' button to save the file; the following window will appear:

File Explorer:

Image description
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
Now, let’s open the pdf file and take a look.
A file is created as shown below:

Output file

Image description

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

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.

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

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›