Convert Excel to PDF in Console application

18 Nov 20187 minutes to read

Syncfusion® XlsIO is a .NET Core 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 Console application.

Convert Excel to PDF using .NET Core

Step 1: Create a new C# .NET Core console application. Create .NET Core console project in Visual Studio

Step 2: Name the project.

Name the project

Step 3: Select the framework and click Create button.

Framework version

Step 4: Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.XlsIORenderer.Net.Core 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 “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 5: Include the following Namespaces in the Program.cs file.

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

Step 6: Add the following code snippet in Program.cs file.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    //Load existing Excel file
    FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Sample.xlsx"), FileMode.Open, FileAccess.Read);
	IWorkbook workbook = application.Workbooks.Open(inputStream);

    //Convert to PDF
    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    //Create the MemoryStream to save the converted PDF.      
    MemoryStream pdfStream = new MemoryStream();

    #region Save
    //Saving the workbook
    FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.pdf"), FileMode.Create, FileAccess.Write);
    pdfDocument.Save(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();
    inputStream.Dispose();
}

Step 1: Create a new C# .NET Core console application. Create .NET Core console project in Visual Studio Code

Step 2: Name the project and create the project.

Name the project

Alternatively, create a ASP.NET Core console application using the following command in the terminal(Ctrl+`).

dotnet new console -o Convert-Excel-to-PDF
cd Convert-Excel-to-PDF

Step 3: To Convert an Excel document to PDF in .NET Core app, run the following command to install Syncfusion.XlsIORenderer.Net.Core package. Install Syncfusion.XlsIORenderer.Net.Core Nuget Package

dotnet add package Syncfusion.XlsIORenderer.Net.Core

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: Include the following Namespaces in the Program.cs file.

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

Step 5: Add the following code snippet in Program.cs file.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    //Load existing Excel file
    FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Sample.xlsx"), FileMode.Open, FileAccess.Read);
	IWorkbook workbook = application.Workbooks.Open(inputStream);

    //Convert to PDF
    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    //Create the MemoryStream to save the converted PDF 
    MemoryStream pdfStream = new MemoryStream();

    #region Save
    //Saving the workbook
    FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.pdf"), FileMode.Create, FileAccess.Write);
    pdfDocument.Save(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();
    inputStream.Dispose();
}

A complete working example of how to convert an Excel document to PDF using .NET Core is present on this GitHub page.

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

Output File

Convert Excel to PDF using .NET Framework

Step 1: Create a new C# .NET Framework console application. Create .NET Core console project in Visual Studio

Step 2: Name the project.

Name the project

Step 3: Install the Syncfusion.ExcelToPdfConverter.WinForms NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.ExcelToPdfConverter.WinForms 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 “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: Include the following Namespaces in the Program.cs file.

using Syncfusion.XlsIO;
using Syncfusion.ExcelToPdfConverter;
using Syncfusion.Pdf;

Step 5: Add the following code snippet in Program.cs file.

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 to 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 using .NET Framework is present on this GitHub page.

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

Output File

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.