Top
ArticleCity.comArticle Categories How To Create PDF File In C# Using IronPdf

How To Create PDF File In C# Using IronPdf

Photo from Unsplash

Originally Posted On: https://www.c-sharpcorner.com/article/how-to-create-pdf-file-in-c-sharp-using-ironpdf/

 

Software developers must know how to create a pdf file dynamically through C# code, as this is considered a need in almost all projects in today’s world. You may come across various problems where you need to create a Pdf file such as generating a report from user input, saving user text as pdf, extracting HTML content and converting it into pdf, etc.

You might be thinking that it would be a complex task. But thanks to IronPdf, It’s as easy as calculating 2+3.

You can create a pdf file from C# with just 2 lines of code.

Explore more about Ironpdf from this URL.

I have prepared a step-by-step demo for you, to show how we can create pdf document using C#.

Prerequisite Knowledge

You must have the following basic knowledge to understand it easily,

  • Basic Knowledge of C# Programming
  • Basic Knowledge of C# GUI Controls
  • Basic Knowledge of HTML for formatting the pdf document.

If you don’t have this knowledge, don’t worry, you will still be able to understand it easily.

I have used the window Form App for the demonstration. However, you can use Asp.Net, console app, WPF App, or any other of your choice.

This Library is supported by both C# and VB.Net

Step 1 – Create Visual Studio Project

I am using Visual Studio 2019, You can use any. Visual Studio 2019 is recommended as it provides 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 popup. Name the project. I have named it “Create Pdf using IronPdf”.

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,

How To Create PDF File In C# By Ironpdf

Step 2 – Install Nuget Package of IronPdf

To develop a solution, we need to install Nuget Package. Select “Project” from the Menu Bar, a Drop Down List appears. 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, 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.

How To Create PDF File In C# By Ironpdf

Select and Click on IronPdf. The following Window will appear.

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.

How To Create PDF File In C# By Ironpdf

Once you’ve pressed the ‘Ok’ button, you’re ready to go.

Following Readme.txt file will open.

How To Create PDF File In C# By Ironpdf

I recommend that you click on all of the links and learn more about this Library.

Step 3 – Design Window Form

The project has been created, and the Nuget package has 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’.

How To Create PDF File In C# By Ironpdf

We need a label to name our Application. A label can be found by searching for it from the toolbar and then dragging and dropping it onto the Form Design.

Give the label a name. I have named it as C# Create Pdf using IronPdf,

How To Create PDF File In C# By 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).

How To Create PDF File In C# By Ironpdf

Step 4 – Add the Back End Code for Save Button

Double Click on Save Button, the following code will appear.

private void Save_Click(object sender, EventArgs e) {}
C#

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

using IronPdf;
C#

Now, the actual work will start from here. We need a File path where we have to save our newly created pdf file. For that I am using SaveFile Dialog, that will ask user to select file path and file name.

Add 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!");
    }
}
C#

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.

You can see that, With only two lines of code, we have managed to generate pdf file.

PdfText is the name of a Rich text box in which we have our text to be written in a pdf file.

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

Step 5 – Add the Back End Code for Clear and Close Button

Double Click on the clear button, the following code will appear.

private void Clear_Click(object sender, EventArgs e) {}
C#

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

private void Clear_Click(object sender, EventArgs e) {
    PdfText.Text = "";
}
C#

Now, Double click on Close Button, the following code will appear.

private void Close_Click(object sender, EventArgs e) {}
C#

Add the following code inside the Close_Click Function.

private void Close_Click(object sender, EventArgs e) {
    this.Dispose();
}
C#

Run the Project.

Press Ctrl + F5 to run the Project, the following window will appear.

How To Create PDF File In C# By Ironpdf

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>

How To Create PDF File In C# By Ironpdf

Now Click on the ‘Save’ button to save the file, the following window will appear.

How To Create PDF File In C# By Ironpdf

Select ‘Folder’ and write ‘File’ name. I have select D drive and set the file name as “C# Create Pdf”. Press the ‘Save’ button, the following message box will appear after successful creation of Pdf file.

How To Create PDF File In C# By Ironpdf

Now, Let’s open the pdf file and see.

A file is created as shown below,

How To Create PDF File In C# By Ironpdf

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

 

No Comments

Sorry, the comment form is closed at this time.