5 steps to Generate a PDF File in C# using IRON PDF

If you're a developer and you want to generate PDF files from HTML without using HTML tags, you can use this IronPDF package. IronPDF also works with VB .NET but here I will guide you with C# Windows Form applications with .net frame. I am using Visual Studio 2019 IDE to create a project.

To get started you will need to know the following:

Requirements when using Iron PDF:

  • You should have basic knowledge of C# languages.
  • You should have a basic knowledge of Windows applications.

In this article, you will learn how to create a PDF file using HTML easily.

Please follow these steps to create a project in Visual Studio 2019.

Step 1. Create a Visual Studio Project

First, you have to open Visual Studio 2019.

Main Window of Visual Studio 2019

Main Window of Visual Studio 2019

Click on 'Create a new project'.

Now, Select 'Windows Form App' from Template, and press 'Next', The following window will appear:

Create a New Project Window

Create a New Project Window

Write Project Name 'Create PDF using IronPDF'.

Set Name of the Project

Set Name of the Project

Click on the 'Create' Button, the Project will be created as shown below.

First window after create project

First window after create project

Step 2. Install Iron PDF using NuGet

You can add Iron PDF using DLL or NuGet package. I will show you via NuGet Package. You can find more package details here.

The following steps are for adding IronPDF package to our existing projects.

  • First Click the 'Tools' button on the Menu bar.
  • Then a menu will open, now click on NuGet Package Manager Option.
  • Then another sub-menu will open, now click on the option named Package Manager Console.
Open Package Manager Console window

Open Package Manager Console window

You will get a new screen under the write command. In it write command Install-Package Iron PDF.

PM> Install-Package IronPdf
Enter command window

Enter command window

Press Enter after the right command. (Make sure your computer/laptop is connected to the Internet). The Iron PDF package will auto-add to our existing projects.

Successfully Package Install window

Successfully Package Install window

The above screen shows the package added successfully in our projects.

Step 3. Design Form for a User Input

Now add a label and write the text as Create a PDF from HTML using Iron PDF

Set a Label Window

Set a Label Window

Now add a Rich Text Box & a Button from the Toolbox. Next, set the Button Text as 'Convert'. I have added a Rich Text Box for the input of HTML.

Design RichText Box And Button window

Design RichText Box And Button window

Step 4. Write back end code, to create PDF

Double click on the 'Convert' button then you will get a code window with a convert button click events.

Back end code window

Back end code window

Add code for importing IronPDF library on the top of the .cs file.

Firstly, add the following code for using Iron PDF Library methods.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

We have a blank btnConvert_Click event code in .cs file.

private void btnConvert_Click(object sender, EventArgs e)
{

}
private void btnConvert_Click(object sender, EventArgs e)
{

}
Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As EventArgs)

End Sub
VB   C#

Now write the following code in a button click event.

private void btnConvert_Click(object sender, EventArgs e)
{
  //Declare HTMLToPdf object.
  var HtmlLine = new HtmlToPdf();

  //Get HTML Text from User.
  string strHtml = txtHtml.Text;

  //Create SaveFileDialog for get Save PDF file path.
  SaveFileDialog saveFileDialog = new SaveFileDialog
  {
    InitialDirectory = @"D:\",
    Title = "Save PDF",
    CheckPathExists = true,
    DefaultExt = "pdf",
    Filter = "pdf files (*.pdf)|*.pdf",
    FilterIndex = 2,
    RestoreDirectory = true
  };

  //If User press Save.
  if (saveFileDialog.ShowDialog() == DialogResult.OK)
  {
    //Get File path by user.
    string filePath = saveFileDialog.FileName;

    //Convert HTML to PDF & save on path.
    HtmlLine.RenderHtmlAsPdf(strHtml).SaveAs(filePath);

    //Clear HTML & Show Message.
    txtHtml.Text = "";
    MessageBox.Show("File created successfully.");
  }                                                                                                            
}
private void btnConvert_Click(object sender, EventArgs e)
{
  //Declare HTMLToPdf object.
  var HtmlLine = new HtmlToPdf();

  //Get HTML Text from User.
  string strHtml = txtHtml.Text;

  //Create SaveFileDialog for get Save PDF file path.
  SaveFileDialog saveFileDialog = new SaveFileDialog
  {
    InitialDirectory = @"D:\",
    Title = "Save PDF",
    CheckPathExists = true,
    DefaultExt = "pdf",
    Filter = "pdf files (*.pdf)|*.pdf",
    FilterIndex = 2,
    RestoreDirectory = true
  };

  //If User press Save.
  if (saveFileDialog.ShowDialog() == DialogResult.OK)
  {
    //Get File path by user.
    string filePath = saveFileDialog.FileName;

    //Convert HTML to PDF & save on path.
    HtmlLine.RenderHtmlAsPdf(strHtml).SaveAs(filePath);

    //Clear HTML & Show Message.
    txtHtml.Text = "";
    MessageBox.Show("File created successfully.");
  }                                                                                                            
}
Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As EventArgs)
  'Declare HTMLToPdf object.
  Dim HtmlLine = New HtmlToPdf()

  'Get HTML Text from User.
  Dim strHtml As String = txtHtml.Text

  'Create SaveFileDialog for get Save PDF file path.
  Dim saveFileDialog As New SaveFileDialog With {
	  .InitialDirectory = "D:\",
	  .Title = "Save PDF",
	  .CheckPathExists = True,
	  .DefaultExt = "pdf",
	  .Filter = "pdf files (*.pdf)|*.pdf",
	  .FilterIndex = 2,
	  .RestoreDirectory = True
  }

  'If User press Save.
  If saveFileDialog.ShowDialog() = DialogResult.OK Then
	'Get File path by user.
	Dim filePath As String = saveFileDialog.FileName

	'Convert HTML to PDF & save on path.
	HtmlLine.RenderHtmlAsPdf(strHtml).SaveAs(filePath)

	'Clear HTML & Show Message.
	txtHtml.Text = ""
	MessageBox.Show("File created successfully.")
  End If
End Sub
VB   C#

First, declare the object of HTML To PDF which is provided by Iron PDF. Get HTML input by user. I have used Save File Dialog to get the user file save path.

If users press Save on the dialog box we get the file path and set it. HtmlLine.RenderHtmlAsPdf functions convert HTML text to PDF.Add HTML string and file path with name in the above function parameter.

Next, clear the textbox and show the 'message' box. I have written a comment in code for better understanding line by line. Now run your project.

Step 5. Run the Project

When you run the project you will get the following screen.

Write Html in Rich Text Box window

Write Html in Rich Text Box window

Write HTML in textbox. I wrote:

<h1> A Simple PDF File </h1> <br> <h6> Heading 6 </h6>
<h1> A Simple PDF File </h1> <br> <h6> Heading 6 </h6>
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'<h1> A Simple PDF File </h1> <br> <h6> Heading 6 </h6>
VB   C#

Now click on convert. You will get a save file dialog.

Set Output file path & name window

Set Output file path & name window

After you click on the save button file it will be saved on your selected path with HtmlToPdf file name.

Output File

Our pdf document output will be like this.

Output file

Output file

Conclusion: The above tutorial is an introduction to Creating a PDF from HTML using IronPDF Library. Here I end my article about generating a PDF using IronPDF which is easy to use and I try to explain it simply. So try creating it yourself. IronPDF also provides information regarding the use of the package in c#. You can visit the Official Site for an FAQ regarding IronPDF.