How to convert WinForms Excel to PDF in C#, VB.NET?
Syncfusion® WinForms Excel library is the .NET Excel library used to create, read, and edit Excel documents. Excel helps in daily life to manage records, analyze data, perform calculations, serve as an analytical tool for business, and provide visualization. PDF (Portable Document Format) is one of the file formats like Excel, and it can capture all the elements of a printed document as an electronic image that you can view, navigate, and print. Syncfusion® Excel (XlsIO) library helps you to convert Excel file to PDF in C# and VB.NET.
XlsIO helps to convert Excel to PDF by loading the workbook or worksheet into ExcelToPDFConverter and converting the loaded document using the Convert method. This conversion is supported on platforms like Windows Forms, WPF, ASP.NET, ASP.NET MVC, ASP.NET Core, Xamarin, and Azure.
This article shows you how to convert Excel to PDF in C#, VB.NET.
Steps to convert an Excel file to PDF programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install Syncfusion.ExcelToPdfConverter.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.
Install NuGet package
Step 3: Include the following namespaces in the Program.cs file:
C#
using Syncfusion.ExcelToPdfConverter; using Syncfusion.Pdf; using Syncfusion.XlsIO; using System.IO; using System.Reflection;
VB.NET
Imports System.IO Imports System.Reflection Imports Syncfusion.ExcelToPdfConverter Imports Syncfusion.Pdf Imports Syncfusion.XlsIO
Step 4: Open an existing Excel document or create a new document and load it into IWorkbook instance.
C#
// Initialize Application IApplication application = excelEngine.Excel; // Set the default application version as Xlsx application.DefaultVersion = ExcelVersion.Xlsx; // Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("ExcelToPDFConversion.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(fileStream);
VB.NET
'Initialize Application Dim application As IApplication = excelEngine.Excel 'Set the default application version as Xlsx application.DefaultVersion = ExcelVersion.Xlsx 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Program).GetTypeInfo.Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("ExcelToPDFConversion.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream)
Step 5: Convert the Excel file to PDF.
C#
// Initialize ExcelToPDF Converter ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook); // Initialize PDF document PdfDocument pdfDocument = new PdfDocument(); // Convert Excel document into PDF document pdfDocument = converter.Convert(); // Save the PDF file pdfDocument.Save("Output.pdf");
VB.NET
'Initialize ExcelToPDF Converter Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook) 'Initialize PDF document Dim pdfDocument As PdfDocument = New PdfDocument 'Convert Excel document into PDF document pdfDocument = converter.Convert 'Save the PDF file pdfDocument.Save("Output.pdf")
A complete working example to convert Excel file to PDF along with the input file used for conversion can be downloaded from Convert-Excel-to-PDF-Sample.zip.
By executing the program, you will get the PDF file as below.
Output PDF document
Know more about Syncfusion® Excel (XlsIO) library through the documentation, where you can find the supported features like Excel file with worksheets, charts and chart sheets to PDF conversion and different printer settings along with Worksheet to Image conversion, Chart to Image conversion and Excel to ODS conversion.
An online sample link for Excel to PDF conversion.
To learn more about the Syncfusion® Excel (XlsIO) library, refer to the documentation where you will find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheets or workbooks, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheets using Template Markers with code examples.
See Also:
How to convert the PDF document into Excel
Convert Excel to PDF with Azure function in .NET Standard application
Convert Excel to PDF in Azure platform
How to convert an Excel file to CSV
How to convert Excel worksheet to image
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to convert WinForms Excel to PDF in C#, VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!