How to convert Excel to PDF in C#, VB.NET?
Syncfusion Excel (XlsIO) library is the .NET Excel library used to create, read, and edit Excel documents. Excel helps in daily life to manage records, analyze data, calculations, analytical tool for business and 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 converts the loaded document using Convert method. This conversion is supported in the 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 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 worksheet or workbook, organizing and analysing data through Tables and Pivot Tables, appending multiple records to worksheet 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
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 Excel to PDF in C#, VB.NET.
You can refer to our WinForms Excel feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Excel example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!