Convert Excel to PDF in Just 5 Steps Using C#

Convert Excel to PDF in Just 5 Steps Using C#

Convert Excel to PDF in Just 5 Steps Using C#

TLDR: Build a .NET Core console app with dependencies and convert Excel to PDF using Syncfusion’s Excel Library.

Microsoft Excel is a trusted tool for organizing, visualizing, and managing complex data. However, when you need to share data from an Excel workbook with other users, they will require Excel or an Excel file viewer to access it. To make sharing easier, you can convert Excel workbooks into the universal PDF format, allowing seamless access across various devices without the need for Excel.

The [Syncfusion Excel Library](syncfusion.com/document-processing/excel-fr.. ".NET Excel Framework"), also known as Essential XlsIO, is a powerful tool for creating, reading, and editing Excel documents using C# without any Microsoft Office dependencies. Its extensive capabilities include support in creating Excel workbooks from scratch, modifying existing ones, importing and exporting data, handling formulas, applying conditional formatting, managing data validations, and much more.

Let’s see how to convert an Excel workbook to a PDF document with the help of the Syncfusion Excel Library using C#!

[

Syncfusion Ad

Enjoy a smooth experience with Syncfusion’s Excel Library! Get started with a few lines of code and without Microsoft or interop dependencies.

Explore Now

](syncfusion.com/document-processing/excel-fr..)

Excel-to-PDF conversion

Our Excel Library can convert an [entire workbook or a single worksheet into a PDF document](syncfusion.com/document-processing/excel-fr.. "Overview: Convert Excel to PDF in C# and VB.NET") using the built-in [Excel-to-PDF converter](help.syncfusion.com/file-formats/xlsio/over.. "Convert Excel to PDF using Syncfusion Excel library"). You can customize the conversion process by embedding fonts, ignoring empty pages or worksheets, and managing the visibility of headers and footers. Conversion is fast, reliable, and compatible with hosting environments such as AWS, Google Cloud Platform, and Microsoft Azure Web Services. The Excel Library also supports [converting workbooks with charts to PDF](help.syncfusion.com/file-formats/xlsio/exce.. "Getting Started with the Excel with chart to PDF"), [substituting fonts](help.syncfusion.com/file-formats/xlsio/exce.. "Substitute Font in Excel-to-PDF Conversion") during conversion, and many useful [conversion options](help.syncfusion.com/file-formats/xlsio/exce.. "Excel to PDF Converter Settings").

Steps to convert Excel to PDF using C#

Put simply, this is the procedure:

  1. Create a new .NET Core console application.
  2. Install and configure the Excel Library.
  3. Add namespaces.
  4. Convert Excel to PDF format.
  5. Get an Excel document stream.

Let’s dig into these steps in detail.

Step 1: Create a new .NET Core console application

First, create a new [.NET Core console application](docs.microsoft.com/en-us/dotnet/core/tutori.. "Create a .NET console application using Visual Studio") in [Visual Studio](code.visualstudio.com/Download "Visual Studio"), as shown in the following figure:Create a .NET Core Console application

Step 2: Install and configure the Excel Library

Add the [Syncfusion.XlsIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIORenderer.Net.Core "Syncfusion.XlsIORenderer.Net.Core NuGet package") NuGet package to your application.Install Syncfusion XlsIORenderer Net Core NuGet package

[

Syncfusion Ad

Handle Excel files like a pro with Syncfusion’s C# Excel Library, offering well-documented APIs for each functionality.

Read Now

](help.syncfusion.com/file-formats/xlsio/over..)

Step 3: Add namespaces

Next, add the following namespaces to the Program.cs file.

using Syncfusion.XlsIO; using Syncfusion.XlsIORenderer; using Syncfusion.Pdf;

Step 4: Convert the Excel workbook to PDF format

After that, implement the following code in the Program.cs file to convert an Excel document stream into a PDF stream of data.

static Stream ConvertExcelToPDF(Stream inputExcelData) { MemoryStream pdfStream = new MemoryStream();

//Instantiate the spreadsheet creation engine. using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object. IApplication application = excelEngine.Excel;

//Set the default application version. application.DefaultVersion = ExcelVersion.Xlsx;

//Load the existing Excel file into IWorkbook. IWorkbook workbook = application.Workbooks.Open(inputExcelData);

//Settings for Excel to PDF conversion. XlsIORendererSettings settings = new XlsIORendererSettings();

//Set the layout option to fit all columns on one page. settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage;

//Initialize the XlsIORenderer. XlsIORenderer renderer = new XlsIORenderer();

//Initialize the PDF document. PdfDocument pdfDocument = new PdfDocument();

//Convert the Excel document to PDF. pdfDocument = renderer.ConvertToPDF(workbook, settings);

//Save the PDF file. pdfDocument.Save(pdfStream);

//Close the PDF document. pdfDocument.Close();

//Close the workbook. workbook.Close(); }

return pdfStream; }

[

Syncfusion Ad

Witness the possibilities in demos showcasing the robust features of Syncfusion’s C# Excel Library.

Try Now

](syncfusion.com/demos/fileformats/excel-libr..)

Step 5: Get Excel document stream

Finally, add the following code to the Main method to get the Excel document streamed and pass it to the ConvertExcelToPDF method in the Program.cs file.

static void Main(string[] args) { string filePath = "../../../Data/Invoice.xlsx"; Stream inputExcelData = File.OpenRead(filePath); Stream outputPDFData = ConvertExcelToPDF(inputExcelData); File.WriteAllBytes("../../../Output/Invoice.pdf", ((MemoryStream)outputPDFData).ToArray()); }

The following image shows the input Excel document:

Input Excel Document

Input Excel Document

After executing the previous code example, the output PDF document will resemble the following image:

Excel Workbook Converted to PDF Format Using Syncfusion Excel Library and C#

Excel Workbook Converted to PDF Format Using Syncfusion Excel Library and C#

Note: For more details, explore the [various Excel-to-PDF conversion settings](help.syncfusion.com/file-formats/xlsio/exce.. "Excel to PDF Converter Settings").

Reference

For more details, refer to [convert an Excel workbook to a PDF document using C# documentation](help.syncfusion.com/file-formats/xlsio/over.. "Convert an Excel workbook to a PDF document using C# documentation") and [GitHub demos](github.com/SyncfusionExamples/XlsIO-Example.. "Syncfusion Excel XlsIO GitHub demo").

[

Syncfusion Ad

Trusted by industry giants worldwide, Syncfusion's Excel Framework has a proven track record of reliability and excellence.

Try It Free

](syncfusion.com/downloads/fileformats)

Conclusion

Thanks for reading! In this blog, we explored how to convert Excel workbooks to PDF documents in C# with the [Syncfusion Excel Library](syncfusion.com/excel-framework/net ".NET Excel Framework"). With it, you can also export Excel data to [images](help.syncfusion.com/file-formats/xlsio/work.. "Worksheet to Image conversion in Excel Library"), [data tables](help.syncfusion.com/file-formats/xlsio/work.. "Exporting from Worksheet to Data Table"), [CSV](syncfusion.com/kb/9092/convert-excel-file-t.. "KB: Convert Excel file to CSV in C#, VB.NET"), [TSV](syncfusion.com/kb/8556/does-xlsio-support-t.. "KB: Does XlsIO support TSV files?"), [collections of objects](help.syncfusion.com/file-formats/xlsio/work.. "Working with Excel Data"), [ODS](help.syncfusion.com/file-formats/xlsio/exce.. "Excel to ODS Conversion"), [JSON](help.syncfusion.com/file-formats/xlsio/exce.. "Excel to JSON Conversion"), and more file formats.

Take a moment to peruse the [import data](help.syncfusion.com/file-formats/xlsio/work.. "Importing Data to Worksheets") documentation, where you’ll discover how to import [data tables](help.syncfusion.com/file-formats/xlsio/work.. "Import Data from DataTable"), [collection objects](help.syncfusion.com/file-formats/xlsio/work.. "Import Data from Collection Objects"), [grid view](help.syncfusion.com/file-formats/xlsio/work.. "Import Data from DataView"), [data columns](help.syncfusion.com/file-formats/xlsio/work.. "Importing Data from DataColumn"), and [HTML](help.syncfusion.com/file-formats/xlsio/work.. "Importing HTML Table to Excel Worksheet") into Excel workbooks, all accompanied by code samples.

Try out these conversions and share your feedback in the comment section of this blog post!

For current customers, the newest version of Essential Studio is available for download from the [License and Downloads](syncfusion.com/account/downloads "Essential Studio License and Downloads page") page. If you are not yet a Syncfusion customer, you can try our 30-day [free trial](syncfusion.com/downloads "Get the free 30-day evaluation of Essential Studio products") to check out the enormous collection of powerful features.

If you have any questions, you can contact us through our [support forum](syncfusion.com/forums "Syncfusion Support Forum"), [support portal](support.syncfusion.com "Syncfusion Support Portal"), or [feedback portal](syncfusion.com/feedback "Syncfusion Feedback Portal"). We are happy to assist you!