In enterprise applications, programmatically converting Excel documents to PDF format is a common task. PDF ensures a document’s layout remains consistent across different devices and does not require the recipient to have spreadsheet software installed.
This article walks through how to convert Excel (XLS/XLSX) files to PDF in C# using the Spire.XLS for .NET library. It covers basic conversion, exporting specific sheets, fitting content to one page, customizing page setup, and adding PDF security.
Why Convert Excel to PDF?
Preserved Formatting – PDF locks layout, fonts, and images.
Cross-Platform Compatibility – PDF readers are widely available.
Optional Security – PDF can be encrypted or password-protected.
Setting Up the Environment
Install the Spire.XLS for .NET library via NuGet Package Manager:
Install-Package Spire.XLS
Once installed, add the namespace at the top of your code file:
using Spire.Xls;
The library does not require Microsoft Excel to be installed on the server.
1. Basic Excel to PDF Conversion
The following code loads an entire Excel workbook and saves it as a single PDF file.
using Spire.Xls;
namespace ExcelToPdf.Basic
{
class Program
{
static void Main(string[] args)
{
using (Workbook workbook = new Workbook())
{
// Load an Excel file (.xls or .xlsx)
workbook.LoadFromFile("Sample.xlsx");
// Save the workbook as a PDF
workbook.SaveToFile("Output/BasicConversion.pdf", FileFormat.PDF);
}
System.Console.WriteLine("Conversion completed.");
}
}
}
Explanation:
Workbook represents the entire Excel file.
LoadFromFile loads an existing Excel file from disk.
SaveToFile with FileFormat.PDF triggers the conversion.
2. Advanced Customizations
A. Exporting a Specific Worksheet or Cell Range
To export only one sheet or a defined cell range:
using Spire.Xls;
namespace ExcelToPdf.Advanced
{
class Program
{
static void Main(string[] args)
{
using (Workbook workbook = new Workbook())
{
workbook.LoadFromFile("SalesData.xlsx");
// Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Define a print area (e.g., cells B1 to E6)
// If omitted, the entire sheet is exported.
worksheet.PageSetup.PrintArea = "B1:E6";
// Save this specific sheet/range to PDF
worksheet.SaveToPdf("Output/SpecificSheet.pdf");
}
}
}
}
Pro Tip: If you set PrintArea, only that cell range will appear in the PDF. If you skip it, the entire worksheet data is exported.
B. Fitting Worksheet to One Page
To force a worksheet to fit on a single PDF page (scaling content if necessary):
using Spire.Xls;
namespace ExcelToPdf.Advanced
{
class Program
{
static void Main(string[] args)
{
using (Workbook workbook = new Workbook())
{
workbook.LoadFromFile("LargeReport.xlsx");
// Enable fitting to one page
workbook.ConverterSetting.SheetFitToPage = true;
workbook.SaveToFile("Output/FitToOnePage.pdf", FileFormat.PDF);
}
}
}
}
C. Customizing Page Setup (Orientation, Margins, Paper Size)
Adjust page orientation, paper size, margins, and gridline visibility:
using Spire.Xls;
namespace ExcelToPdf.Advanced
{
class Program
{
static void Main(string[] args)
{
using (Workbook workbook = new Workbook())
{
workbook.LoadFromFile("WideData.xlsx");
Worksheet sheet = workbook.Worksheets[0];
// Set orientation to Landscape
sheet.PageSetup.Orientation = PageOrientationType.Landscape;
// Set paper size to A4
sheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
// Set margins in inches
sheet.PageSetup.TopMargin = 0.5;
sheet.PageSetup.BottomMargin = 0.5;
sheet.PageSetup.LeftMargin = 0.3;
sheet.PageSetup.RightMargin = 0.3;
// Show gridlines in the output PDF
sheet.PageSetup.IsPrintGridlines = true;
sheet.SaveToPdf("Output/CustomPageSetup.pdf");
}
}
}
}
D. Securing the PDF with a Password
To encrypt the output PDF so that a password is required to open it:
using Spire.Xls;
using Spire.Xls.Pdf.Security;
namespace ExcelToPdf.Advanced
{
class Program
{
static void Main(string[] args)
{
using (Workbook workbook = new Workbook())
{
workbook.LoadFromFile("Confidential.xlsx");
// Encrypt the PDF
// Parameters: openPassword, permissionPassword, permissions, keySize
workbook.ConverterSetting.PdfSecurity.Encrypt(
"user123", // Password to open
"owner456", // Password for permissions
PdfPermissionsFlags.FullAccess,
PdfEncryptionKeySize.Key128Bit
);
workbook.SaveToFile("Output/Secure.pdf", FileFormat.PDF);
}
}
}
}
Performance Notes
Use a using statement (or call Dispose()) to release memory, especially when processing large files.
The library can process multiple files in a loop; for server-side use, ensure proper resource management.
Conclusion
Converting Excel to PDF in C# is a routine but important task in many backend systems. The examples above demonstrate a straightforward workflow: loading a workbook, optionally adjusting page or sheet settings, and saving the result as a PDF. Whether you need to export a single range, fit a large sheet onto one page, or apply basic encryption, the same Workbook and Worksheet APIs can be extended to fit your use case. As always, test with your actual document layouts to ensure the output matches your expected pagination and formatting.