Detailed Blog page Skeleton loader
Convert Excel to PDF in Just 5 Steps Using C#

TLDR: Learn how to build a .NET Core console app to convert Excel files to PDF using Syncfusion’s Excel Library (XlsIO) with C#, without Microsoft Office dependencies. Follow simple steps to create, configure, and execute the conversion, with customizable options like layouts, fonts, and PDF/UA compliance.

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, 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#!

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

Excel-to-PDF conversion

Our Excel Library can convert an entire workbook or a single worksheet into a PDF document using the built-in Excel-to-PDF converter. With this, you can convert Excel formats such as XLSX, XLS, XLTM, XLSM, XLTX, CSV, TSV, and XML Spreadsheet to PDF.

You can customize the conversion process by using different layouts, embedding fonts, ignoring empty pages or worksheets, and managing the visibility of headers and footers. The Excel Library also supports converting workbooks with charts to PDF, substituting fonts, fallback fonts during conversion, and many useful conversion options. It supports creating tagged PDFs from the Excel document conforming to the PDF/UA standard, along with the PDF conformance level.

The Excel to PDF conversion is fast, reliable, and compatible with hosting environments such as AWS, Google Cloud Platform, and Microsoft Azure Web Services.

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. Add Excel to PDF conversion code.
  5. Convert an XLSX file to PDF.

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 in 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 NuGet package to your application.Install Syncfusion XlsIORenderer Net Core NuGet package

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

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: Add Excel to PDF conversion code

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;
}

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

Step 5: Convert an XLSX file to PDF

Finally, add the following code to the Main method to pass the XLSX  document to the ConvertExcelToPDF method to convert to PDF format 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.

Reference

For more details, refer to the documentation on converting an Excel workbook to a PDF document using C# and GitHub demos. Explore the online demo of Excel to PDF conversion.

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

Conclusion

Thanks for reading! In this blog, we explored how to convert Excel documents to PDF format in C# with the Syncfusion® Excel Library. With it, you can also export Excel data to imagesdata tables, HTMLCSVTSVcollections of objectsODSJSON, and more file formats.

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

If you are new to our .NET Excel library, it is highly recommended that you follow our Getting Started guide.

Are you already a Syncfusion® user? You can download the product setup here. If you’re not yet a Syncfusion® user, you can download a free, here.

If you have any questions or require clarification about these features, please let us know in the comments below. You can also contact us through our support forumsupport portal, or feedback portal. We are happy to assist you!

Related blogs

Be the first to get updates

Mohan Chandran

Meet the Author

Mohan Chandran

Mohan Chandran is an employee at Syncfusion Software with 4+ years of experience working in an Excel-related library called XlsIO. He is good at finding solutions and resolving queries related to Excel.