Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
C# developers can use IronPDF to generate PDFs from HTML. This article will demonstrate this process with a C# Windows Forms application created using the .NET Framework.
Please follow these steps to create a project in Visual Studio 2019.
First, open Visual Studio 2019.
Click on 'Create a new project'.
Now, select 'Windows Forms App' from the Template, and press 'Next'. The following window will appear:
Enter the Project Name 'Create PDF using IronPDF'.
Click on the 'Create' button. The Project will be created as shown below.
You will get a new screen under the command line. In it, write the command to install the IronPdf
package.
Install-Package IronPdf
Press Enter after typing the command. Make sure your computer/laptop is connected to the Internet. The IronPdf
package will automatically add to your existing project.
The screen above shows the package added successfully to your project.
Now add a label and set the text to "Create a PDF from HTML using IronPDF".
Next, add a Rich Text Box and a Button from the Toolbox. Set the Button Text as 'Convert'.
Double-click on the 'Convert' button. A code window with a convert button click event will open.
Add code for importing the IronPDF library at the top of the .cs
file.
First, add the following code to use IronPDF Library methods.
using IronPdf;
using IronPdf;
Imports IronPdf
Below is the initial code for the btnConvert_Click
event, which is currently empty:
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 the button click event:
private void btnConvert_Click(object sender, EventArgs e)
{
// Declare an HtmlToPdf object
var HtmlLine = new HtmlToPdf();
// Get HTML text from the user
string strHtml = txtHtml.Text;
// Create SaveFileDialog to choose the save path for the PDF file
SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = @"D:\",
Title = "Save PDF",
CheckPathExists = true,
DefaultExt = "pdf",
Filter = "pdf files (*.pdf)|*.pdf",
FilterIndex = 2,
RestoreDirectory = true
};
// If the user presses Save
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Get the file path from the user
string filePath = saveFileDialog.FileName;
// Convert HTML to PDF and save at the specified path
using var PDF = HtmlLine.RenderHtmlAsPdf(strHtml);
PDF.SaveAs(filePath);
// Clear the TextBox and show a message confirming the successful creation
txtHtml.Text = "";
MessageBox.Show("File created successfully.");
}
}
private void btnConvert_Click(object sender, EventArgs e)
{
// Declare an HtmlToPdf object
var HtmlLine = new HtmlToPdf();
// Get HTML text from the user
string strHtml = txtHtml.Text;
// Create SaveFileDialog to choose the save path for the PDF file
SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = @"D:\",
Title = "Save PDF",
CheckPathExists = true,
DefaultExt = "pdf",
Filter = "pdf files (*.pdf)|*.pdf",
FilterIndex = 2,
RestoreDirectory = true
};
// If the user presses Save
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Get the file path from the user
string filePath = saveFileDialog.FileName;
// Convert HTML to PDF and save at the specified path
using var PDF = HtmlLine.RenderHtmlAsPdf(strHtml);
PDF.SaveAs(filePath);
// Clear the TextBox and show a message confirming the successful creation
txtHtml.Text = "";
MessageBox.Show("File created successfully.");
}
}
Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As EventArgs)
' Declare an HtmlToPdf object
Dim HtmlLine = New HtmlToPdf()
' Get HTML text from the user
Dim strHtml As String = txtHtml.Text
' Create SaveFileDialog to choose the save path for the PDF file
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 the user presses Save
If saveFileDialog.ShowDialog() = DialogResult.OK Then
' Get the file path from the user
Dim filePath As String = saveFileDialog.FileName
' Convert HTML to PDF and save at the specified path
Dim PDF = HtmlLine.RenderHtmlAsPdf(strHtml)
PDF.SaveAs(filePath)
' Clear the TextBox and show a message confirming the successful creation
txtHtml.Text = ""
MessageBox.Show("File created successfully.")
End If
End Sub
Explanation:
HtmlToPdf
object is created to utilize IronPDF's conversion capabilities.SaveFileDialog
is used to prompt the user to specify where the resulting PDF should be saved.RenderHtmlAsPdf
and saved to the chosen path.When you run the project, you will see the following screen:
Enter HTML code in the RichTextBox, for example:
<h1>A Simple PDF File</h1><br><h6>Heading 6</h6>
<h1>A Simple PDF File</h1><br><h6>Heading 6</h6>
Click on 'Convert'. You will get a save file dialog.
Once you click the save button, the file will be saved at your specified path with the name and location defined.
The output PDF document will look like this:
The tutorial above explains creating a PDF from HTML using the IronPDF Library.
For more information, visit the IronPDF Official Site. 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 evaluate it using the 30-day trial key. There's currently an excellent offer 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.
IronPDF is a C# library that allows developers to generate PDF files from HTML, facilitating the creation, editing, and management of PDF documents programmatically.
To create a new project, open Visual Studio 2019, select 'Create a new project', choose 'Windows Forms App' from the template options, and set your project name before clicking 'Create'.
You can install IronPDF using NuGet by opening the Package Manager Console in Visual Studio and typing the command: Install-Package IronPdf.
Add a label to the form for instructions, a Rich Text Box for HTML input, and a Button labeled 'Convert' to trigger the PDF generation.
Use the IronPDF library to declare an HtmlToPdf object. Retrieve HTML input from a text box, prompt the user to specify where to save the PDF, and render the HTML to a PDF using the RenderHtmlAsPdf method.
The HtmlToPdf object in IronPDF is used to convert HTML strings into PDF documents, leveraging the library's conversion capabilities.
Run the project in Visual Studio, enter some HTML code in the RichTextBox, and click 'Convert'. Use the SaveFileDialog to specify where to save the resulting PDF file.
IronPDF supports fully customizable PDF files, merging and splitting files programmatically, and offers sample codes demonstrating various features.
Yes, you can evaluate IronPDF using a 30-day trial key available on their official site.
Visit the IronPDF Licensing Information page on their website to learn more about licensing options and current offers.