Syncfusion AI Assistant

How can I help you?

Create or Generate PDF file in Windows Forms

11 Jun 20264 minutes to read

The Syncfusion® .NET PDF library is used to create, read, and edit PDF documents. This library also offers functionality to merge, split, stamp, work with forms, and secure PDF files.

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

Steps to create PDF document in Windows Forms

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

Step 2: Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
Windows Forms PDF NuGet package

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 the Syncfusion.Licensing assembly reference and include a license key in your projects. Please refer to this link to learn about registering the Syncfusion® license key in your application to use our components.

Step 3: Include the following namespaces in the Form1.Designer.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

Step 4: Add a new button in Form1.Designer.cs to create 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 generate PDF file by Essential PDF";
  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 = "Create PDF";
  btnCreate.Click += new EventHandler(btnCreate_Click); 
                               
  //Create PDF
  ClientSize = new System.Drawing.Size(450, 150);
  Controls.Add(label);
  Controls.Add(btnCreate);
  Text = "Create PDF";
}

Step 5: Create the btnCreate_Click event and add the below code sample in btnCreate_Click to generate a PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw the text on the PDF page.

//Create a new PDF document. 
using (PdfDocument document = new PdfDocument())
{
  //Add a page to the document.
  PdfPage page = document.Pages.Add();
  //Create PDF graphics for a page.
  PdfGraphics graphics = page.Graphics;
  //Set the standard font.
  PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
  //Draw the text.
  graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
  //Save the document.
  document.Save("Output.pdf");
}

You can download a complete working sample from GitHub.

By executing the program, you will get the PDF document as follows.
WF output PDF document

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

An online sample link to create PDF document.