Syncfusion AI Assistant

How can I help you?

Convert HTML to PDF file in Windows Forms

10 Jun 20264 minutes to read

The Syncfusion® HTML to PDF converter is a .NET library used to convert HTML or web pages to PDF document in Windows Forms application.

Steps to convert HTML to PDF document in Windows Forms

Step 1: Create a new Windows Forms application project.
Create Windows Forms application

In project configuration window, name your project and select Create.
Project configuration window

Step 2: Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your WinForms application NuGet.org.
NuGet package installation

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.

Step 3: Add the following namespaces into Form1.Designer.cs file.

using System;
using System.Windows.Forms;

Step 4: Add a new button in Form1.Designer.cs to convert HTML to PDF document as follows.

private Button btnCreate;
private Label label;
private void InitializeComponent()
{
   btnCreate = new Button();
   label = new Label();

   //Label
   label.Location = new System.Drawing.Point(0, 40);
   label.Size = new System.Drawing.Size(426, 35);
   label.Text = "Click the button to convert Html to PDF file";
   label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

   //Button
   btnCreate.Location = new System.Drawing.Point(180, 110);
   btnCreate.Size = new System.Drawing.Size(85, 26);
   btnCreate.Text = "Convert Html to PDF";
   btnCreate.Click += new EventHandler(btnCreate_Click);

   //Create PDF
   ClientSize = new System.Drawing.Size(450, 150);
   Controls.Add(label);
   Controls.Add(btnCreate);
   Text = "Convert Html to PDF";
}

Step 5: Include the following namespaces in the Form1.cs file.

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System;

Step 6: Create the btnCreate_Click event and add the following code in btnCreate_Click to convert HTML to PDF document using Convert method in HtmlToPdfConverter class. The HTML content will be scaled based on the given ViewPortSize property of BlinkConverterSettings class.

//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set Blink viewport size.
blinkConverterSettings.ViewPortSize = new System.Drawing.Size(1280, 0);
//Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
//Create file stream.
FileStream stream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew);
//Save the document into stream.
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
stream.Dispose();

By executing the program, you will get the PDF document as follows.
Convert HTMLToPDF Windows Forms output

A complete working sample can be downloaded from Github.

Click here to explore the rich set of Syncfusion® HTML to PDF converter library features.

An online sample link to convert HTML to PDF document in ASP.NET Core.