Convert an Excel document to PDF in Windows Forms
8 Dec 20233 minutes to read
Syncfusion XlsIO is a .NET Excel library used to create, read, edit and convert Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can convert an Excel document to PDF in Windows Forms.
Steps to convert an Excel document to PDF in Windows Forms
Step 1: Create a new Windows Forms application project.
Step 2: Name the project, choose the framework and click Create button.
Step 3: Install the Syncfusion.ExcelToPdfConverter.WinForms NuGet package as a reference to your project from NuGet.org.
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 applications to use our components.
Step 4: Add a new button in the Form1.Designer.cs as shown below.
private Button btnCreate;
private Label label;
private void InitializeComponent()
{
label = new Label();
btnCreate = new Button();
//Label
label.Location = new System.Drawing.Point(0, 40);
label.Size = new System.Drawing.Size(426, 35);
label.Text = "Click the button to Convert Exel document to PDF generated by Essential XlsIO. Please note that Microsoft Excel Viewer or Microsoft Excel is required to view the resultant Excel document";
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//Button
btnCreate.Location = new System.Drawing.Point(180, 110);
btnCreate.Size = new System.Drawing.Size(85, 36);
btnCreate.Text = "Convert Excel document to PDF";
btnCreate.Click += new EventHandler(btnConvert_Click);
//Create Word
ClientSize = new System.Drawing.Size(450, 150);
Controls.Add(label);
Controls.Add(btnCreate);
Text = "Convert Excel document to PDF";
}
Step 5: Include the following namespaces in Form1.Designer.cs.
using Syncfusion.XlsIO;
using Syncfusion.Pdf;
using Syncfusion.ExcelToPdfConverter;
Step 6: Include the below code snippet in btnConvert_Click to convert an Excel document to PDF.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the converted PDF document
pdfDocument.Save("Sample.pdf");
}
A complete working example of how to convert an Excel document to PDF in Windows Forms is present on this GitHub page.
By executing the program, you will get the PDF document as follows.
Click here to explore the rich set of Syncfusion Excel library (XlsIO) features.
An online sample link to convert an Excel document to PDF in ASP.NET Core.