Convert HTML to PDF file in Windows Forms

13 Jan 20233 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.

To include the .NET HTML to PDF converter library into your Windows Forms application, please refer to the NuGet Package Required or Assemblies Required documentation.

Steps to convert Html to PDF document in Windows Forms

Step 1: Create a new Windows Forms application project.
Convert HTMLToPDF Windows Forms Step1
In project configuration window, name your project and select Create.
Convert HTMLToPDF Windows Forms Step2

Step 2: Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your WinForms application
Convert HTMLToPDF Windows Forms Step3

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

  • C#
  • using System;
    using System.Windows.Forms;

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

  • C#
  • private Button btnCreate;
    private Label label;
    private void InitializeComponent()
       btnCreate = new Button();
       label = new 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;
       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);
       Text = "Convert Html to PDF";

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

  • C#
  • 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.

  • C#
  • //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("");
    //Create file stream.
    FileStream stream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew);
    //Save the document into stream.
    //If the position is not set to '0' then the PDF will be empty.
    stream.Position = 0;
    //Close the document.

    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.