Convert an Excel document to PDF in ASP.NET

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 ASP.NET Web Forms.

Steps to convert an Excel document to PDF in C#

Step 1: Create a new ASP.NET Web Application Project.

Create a ASP.NET Web App project in visual studio

Step 2: Name the project.

Name the project

Step 3: Select the Empty project.

Select the Empty App

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

Install Syncfusion.ExcelToPdfConverter.AspNet 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: Add a new Web Form in your project. Right click on the project and select Add > New Item and add a Web Form from the list. Name it as MainPage.

Step 6: Add a new button in the MainPage.aspx as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="Convert_Excel_to_PDF.MainPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Convert Excel to PDF" OnClick="OnButtonClicked" />
        </div>
    </form>
</body>
</html>

Step 7: Include the following namespaces in MainPage.aspx.cs.

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

Step 8: Include the below code snippet in the click event of the button in MainPage.aspx.cs 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();

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

  //Save the converted PDF document to MemoryStream.
  pdfDocument.Save("sample.pdf", HttpContext.Current.Response, HttpReadType.Save);
}

A complete working example of how to convert an Excel document to PDF in ASP.NET 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 MVC.