USING IRONPDF

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

Published October 27, 2021
Share:

C# developers can use IronPDF to generate PDFs from HTML. This article will demonstrate this with a C# Windows Forms application created with the .NET Framework.

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.

5 steps to Generate a PDF File in C# using IronPDF, Figure 1: Main Window of Visual Studio 2019 Main Window of Visual Studio 2019

Click on 'Create a new project'.

Now, select 'Windows Forms App' from Template, and press 'Next'. The following window will appear:

5 steps to Generate a PDF File in C# using IronPDF, Figure 2: Create a New Project Window Create a New Project Window

Write the Project Name 'Create PDF using IronPDF'.

5 steps to Generate a PDF File in C# using IronPDF, Figure 3: Set Name of the Project Set Name of the Project

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

5 steps to Generate a PDF File in C# using IronPDF, Figure 4: First window after creating a new project First window after creating a new project

Step 2. Install IronPDF using NuGet

  • First, click the 'Tools' button on the Menu bar.
  • A menu will open. Now click on the NuGet Package Manager Option.
  • Another sub-menu will open. Now click on the option named Package Manager Console.

5 steps to Generate a PDF File in C# using IronPDF, Figure 5: Open Package Manager Console window Open Package Manager Console window

You will get a new screen under the write command. In it, write a command line to install the IronPdf package.

Install-Package IronPdf

5 steps to Generate a PDF File in C# using IronPDF, Figure 6: Enter command window Enter command window

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

5 steps to Generate a PDF File in C# using IronPDF, Figure 7: Successfully Package Install window Successfully Package Install window

The above screen shows the package added successfully to 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 IronPDF.

5 steps to Generate a PDF File in C# using IronPDF, Figure 8: 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'.

5 steps to Generate a PDF File in C# using IronPDF, Figure 9: Design RichText Box And Button window Design RichText Box And Button window

Step 4. Write back-end code to create PDF file

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

5 steps to Generate a PDF File in C# using IronPDF, Figure 10: Back-end code window Back-end code window

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

First, add the following code for using IronPDF Library methods.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

We have a blank btnConvert_Click event code in the .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 getting 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 presses Save.
  if (saveFileDialog.ShowDialog() == DialogResult.OK)
  {
    //Get File path by user.
    string filePath = saveFileDialog.FileName;

    //Convert HTML to PDF & save on path.
    using var PDF = HtmlLine.RenderHtmlAsPdf(strHtml);
    PDF.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 getting 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 presses Save.
  if (saveFileDialog.ShowDialog() == DialogResult.OK)
  {
    //Get File path by user.
    string filePath = saveFileDialog.FileName;

    //Convert HTML to PDF & save on path.
    using var PDF = HtmlLine.RenderHtmlAsPdf(strHtml);
    PDF.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 getting 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 presses 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.
	Dim PDF = HtmlLine.RenderHtmlAsPdf(strHtml)
	PDF.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 IronPDF. Get HTML input by the user. As shown above, a SaveFileDialog is being used to get the user's desired file 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.

Step 5. Run the Project

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

5 steps to Generate a PDF File in C# using IronPDF, Figure 11: Write HTML in RichTextBox window Write HTML in RichTextBox window

Write HTML in the TextBox:

<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.

5 steps to Generate a PDF File in C# using IronPDF, Figure 12: Set Output file path & name window Set Output file path & name window

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

Output File

Our PDF document output will be like this.

5 steps to Generate a PDF File in C# using IronPDF, Figure 13: Output file Output file

Conclusion

The above tutorial is an introduction to creating a PDF from HTML using the IronPDF Library.

You can visit the IronPDF Official Site for more information regarding IronPDF. The library also provides other functionalities that support fully customizable PDF files, merging and splitting files programmatically, or simply checking sample codes demonstrating various features.

You can assess it using the 30-day trial key. There is also an excellent special offer now available where you can get five Iron Software products for the price of just two. Visit this IronPDF Licensing Information for more information about licensing.

< PREVIOUS
How to view PDF files in ASP.NET using C# and IronPDF
NEXT >
PDF Library for .NET Converter

Ready to get started? Version: 2024.12 just released

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