Convert Excel to PDF in Just 5 Steps Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
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, 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. 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, substituting fonts during conversion, and many useful conversion options.

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

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

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.

Reference

For more details, refer to convert an Excel workbook to a PDF document using C# documentation and GitHub demos.

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 workbooks to PDF documents in C# with the Syncfusion Excel Library. With it, you can also export Excel data to imagesdata tablesCSVTSVcollections of objectsODSJSON, and more file formats.

Take a moment to peruse the import data documentation, where you’ll discover how to import data tablescollection objectsgrid viewdata columns, and HTML 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 page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out the enormous collection of powerful features.

If you have any questions, you can contact us through our support forumsupport portal, or feedback portal. We are happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed