Skip to main content Skip to footer

Programmatically Convert Excel XLSX to PDF Using C# .NET

Quick Start Guide
Tutorial Concept

C# XLSX Conversion - Use a C# .NET Excel API Library to convert XLSX files to other formats. This tutorial will show how to convert the XLSX to a PDF document in a .NET application.

What You Will Need

.NET application
Document Solutions for Excel, .NET Edition

Controls Referenced

Document Solutions for Excel, .NET (DsExcel) - C# Excel Library

Documentation | Online Demos

Converting Excel spreadsheets into PDF files is a common requirement for developers who need to generate professional-looking reports, invoices, or records in a portable and secure format. Using a .NET Excel API library, like Document Solutions for Excel (DsExcel), developers can easily automate this process in your C# .NET applications—no need for Microsoft Excel or additional dependencies.

Download a Free Trial of Document Solutions for Excel, .NET Edition, Today!

Guide To Programmatically Converting Excel XLSX to PDF

Download a finished sample application to follow along with the blog!


Why Convert Excel Files to PDF?

PDFs remain the go-to format for sharing and archiving business documents. They preserve your data layout, look the same on any device, and are difficult to edit—making them ideal for distributing finalized reports.

Some of the main advantages of converting Excel spreadsheets to PDF include:

  • Consistent Formatting: Your fonts, styles, borders, and colors appear exactly as intended, ensuring layout consistency across devices.
  • Universal Compatibility: Recipients can open PDFs without requiring Microsoft Excel, making them ideal for external distribution.
  • Protection Against Edits: Unlike spreadsheets, PDF files are not easily altered, reducing the chance of accidental data modification.
  • Professional Presentation: PDFs deliver a clean and polished appearance for business reports and presentations.

Common scenarios for Excel-to-PDF conversion include:

  • Financial summaries such as income statements or balance sheets
  • Budget or expense reports for project tracking
  • Sales and forecasting dashboards
  • Inventory management data
  • Internal or external audit reports

Document Solutions for Excel enables developers to handle these tasks programmatically, with high performance and zero reliance on Microsoft Office. It supports .NET, .NET Core, .NET Framework, Mono, and Xamarin—allowing deployment on Windows, macOS, or Linux environments.

Notes on PDF Export in .NET

When exporting spreadsheets to PDF on macOS or Linux, ensure you specify the font folder path:

workbook.FontFolderPath = "path_to_fonts";

Place the required font files in this directory to preserve styling and layout during export.
For more information, visit the official documentation on Configuring Fonts and Setting Styles.


Create and Export .NET Excel Workbooks to PDF

Step 1: Create a .NET Excel Workbook

Start by creating a new .NET Console Application within Visual Studio. Open the console for the project and install the Ds.Documents.Excel NuGet package:

dotnet add package DS.Documents.Excel --version 8.2.4

Once installed, modify your Program.cs file so that it contains the proper using statements and code to generate a new Excel Workbook with dummy data inside of it:

using GrapeCity.Documents.Excel;

var workbook = new Workbook();
var random = new Random();

for (int i = 0; i < 10; i++)
{
    workbook.Worksheets[0].Range["A" + (i+1).ToString()].Value = random.Next(20);
}
workbook.Save("RandomValues.xlsx");

After completing the setup, your generated spreadsheet should resemble the following example:

Example Excel File for a .NET XLSX to PDF Conversion Sample App

Step 2: Save the .NET Excel Workbook to PDF

Instead of saving your workbook as an .xlsx file, you can directly output it to PDF format. If you need the spreadsheet to fit neatly on a single PDF page, configure print settings using the PageSetup class:

var worksheet = workbook.Worksheets[0];
worksheet.PageSetup.Orientation = PageOrientation.Landscape;
worksheet.PageSetup.IsPercentScale = false;
worksheet.PageSetup.FitToPagesWide = 1;
worksheet.PageSetup.FitToPagesTall = 1;
workbook.Save(@"RandomValues.pdf");

The resulting document will look similar to this:

Resulting PDF in .NET Excel to PDF Conversion Application


Loading and Converting Existing Excel Files in .NET

You can also convert pre-existing Excel files, whether they were created using Excel, DsExcel, or a third-party library, into PDF in just a few lines of code.

For example, consider a workbook that summarizes account data by age group. Here’s how you can transform it into a PDF document:

Excel Example for XLSX to PDF Developer Tutorial Conversion Sample App

Step 1: Create a .NET Workbook

Create an empty .NET Excel workbook using the constructor of the Workbook class.

var workbook = new GrapeCity.Documents.Excel.Workbook();

Step 2: Load the Excel file

Load an Excel file into the .NET workbook instance using the Open method.

workbook.Open("AgingReport.xlsx");

Step 3: Configure Page Layout

Configure the page layout to ensure the entire sheet fits on one page.

workbook.Worksheets[0].PageSetup.Orientation = PageOrientation.Landscape;
workbook.Worksheets[0].PageSetup.IsPercentScale = false;
workbook.Worksheets[0].PageSetup.FitToPagesWide = 1;
workbook.Worksheets[0].PageSetup.FitToPagesTall = 1;

Step 4: Save Excel as PDF

Save the .NET workbook as a PDF file using the Excel API's Save method.

workbook.Save("AgingReport.pdf");

This quick process seamlessly transforms your Excel data into a neatly formatted PDF file:

Final PDF from a Convert XLSX to PDF in .NET Application Tutorial


Additional PDF Export Features

Document Solutions for Excel provides an extensive list of export capabilities, ensuring your PDF looks exactly as expected. Supported features include:

Try viewing our online demos or review the official documentation for more details on how to perform Excel-to-PDF conversion using Document Solutions for Excel for .NET.


Ready to Try It Yourself?

Start building high-performance spreadsheet applications today.
Download Document Solutions for Excel, .NET Edition and experience fast, accurate Excel-to-PDF conversion with full cross-platform support.

Tags:

comments powered by Disqus