Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
First, you have to open 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:
Create a New Project Window
Write the Project Name 'Create PDF using IronPDF'.
Set Name of the Project
Click on the 'Create' Button, the Project will be created as shown below.
First window after creating a new project
Open Package Manager Console window
You will get a new screen under the write command. In it write a command-line to install IronPdf
package.
Install-Package IronPdf
Enter command window
Press Enter after the right command. (Make sure your computer/laptop is connected to the Internet). The IronPdf
package will auto-add to our existing projects.
Successfully Package Install window
The above screen shows the package added successfully to our projects.
Now add a label and write the text as Create a PDF from HTML using IronPDF
Set a Label Window
Now add a Rich Text Box & a Button from the Toolbox. Next, set the Button Text as 'Convert'.
Design RichText Box And Button window
Double-click on the 'Convert' button then you will get a code window with a convert button click events.
Back-end code window
Add code for importing the IronPDF library on the top of the .cs
file.
Firstly, add the following code for using IronPDF Library methods.
using IronPdf;
using IronPdf;
Imports IronPdf
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
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.
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 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.
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 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.
Dim PDF = HtmlLine.RenderHtmlAsPdf(strHtml)
PDF.SaveAs(filePath)
'Clear HTML & Show Message.
txtHtml.Text = ""
MessageBox.Show("File created successfully.")
End If
End Sub
First, declare the object of HTML To PDF which is provided by IronPDF. Get HTML input by 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.
When you run the project you will get the following screen.
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>
Now click on convert. You will get a save file dialog.
Set Output file path & name window
After you click on the save button, the file will be saved on your selected path with HtmlToPdf
file name.
Our PDF document output will be like this.
Output file
The above tutorial is an introduction to Creating a PDF from HTML using IronPDF Library.
You can visit the 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 our vast features from our sample codes.
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 licensing page for more information about licensing.
9 .NET API products for your office documents