
Microsoft Excel is an essential tool for managing spreadsheets. However, there are scenarios when converting Excel sheets to PDF becomes crucial—be it for sharing reports or preserving formatting. In this blog post, we will discuss how to efficiently convert Excel sheets to PDF using C# and explore various customization options available during the conversion process.
Table of Contents
- C# API for Excel to PDF Conversion - Free Download
- Steps to Convert an Excel File to PDF in C#
- Convert Excel to PDF in C#
- Convert Excel to PDF/A Format
- Track Excel to PDF Conversion in C#
- Online Excel to PDF Converter
C# Excel to PDF Conversion Library
The Aspose.Cells for .NET API simplifies the process of converting Excel spreadsheets to PDF. You can either download the API’s DLL or install it directly via NuGet:
PM> Install-Package Aspose.Cells
Steps to Convert an Excel XLS to PDF
Using Aspose.Cells for .NET, converting an Excel file to PDF is straightforward. Here’s how to do it:
- Load the Excel file from disk.
- Save it as a PDF to your desired location.
Let’s look at how to implement this in C# code.
Convert Excel XLSX to PDF in C#
With Aspose.Cells for .NET, you can easily convert Excel files to PDF by following these steps:
- Instantiate the Workbook class with the Excel document you want to convert.
- Save the document in PDF format by specifying the save format using the SaveFormat enumeration.
Here’s a code snippet demonstrating how to convert an Excel XLS file to PDF in C#:
// Instantiate the Workbook object with the Excel file
Workbook workbook = new Workbook("SampleExcel.xls");
// Save the document in PDF format
workbook.Save("outputPDF.pdf", SaveFormat.Pdf);
Convert Excel to PDF/A Format
PDF/A is an ISO-standardized version of PDF designed for long-term archiving. Saving files in this format ensures that they remain accessible over time.
Here’s how to convert an Excel workbook to a PDF/A compliant format in C#:
// Instantiate new workbook
Workbook workbook = new Workbook();
// Insert a value into cell A1
workbook.Worksheets[0].Cells[0, 0].PutValue("Testing PDF/A");
// Define PdfSaveOptions
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// Set the compliance type
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b;
// Save the file
workbook.Save(dataDir + "output.pdf", pdfSaveOptions);
Track Excel to PDF Conversion in C#
Aspose.Cells for .NET allows you to track the conversion progress using the IPageSavingCallback interface. You can create a custom class that implements this interface and assign its instance to the PdfSaveOptions.PageSavingCallback property.
Here’s a code snippet demonstrating how to track the Excel to PDF conversion progress:
// Load the workbook
Workbook workbook = new Workbook("PagesBook1.xlsx");
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// Assign the custom class that implements IPageSavingCallback interface
pdfSaveOptions.PageSavingCallback = new TestPageSavingCallback();
workbook.Save("DocumentConversionProgress.pdf", pdfSaveOptions);
The following is the custom class that implements the IPageSavingCallback interface for tracking the conversion process:
public class TestPageSavingCallback : IPageSavingCallback
{
public void PageStartSaving(PageStartSavingArgs args)
{
Console.WriteLine("Start saving page index {0} of pages {1}", args.PageIndex, args.PageCount);
// Pages before page index 2 are not rendered.
if (args.PageIndex < 2)
{
args.IsToOutput = false;
}
}
public void PageEndSaving(PageEndSavingArgs args)
{
Console.WriteLine("End saving page index {0} of pages {1}", args.PageIndex, args.PageCount);
// Pages after page index 8 are not rendered.
if (args.PageIndex >= 8)
{
args.HasMorePages = false;
}
}
}
Online Excel to PDF Converter
You can also utilize our online Excel to PDF converter app, which is powered by Aspose.Cells. This free app requires no sign-up, making it a convenient option for quick conversions.
Get Free C# Excel to PDF Converter
You can use Aspose.Cells for .NET without evaluation limitations by obtaining a free temporary license.
Conclusion
Converting Excel sheets to PDF is a common requirement in various business and development scenarios. The Aspose.Cells for .NET API streamlines this process, enabling you to automate conversions within your C# applications. Throughout this blog post, we’ve demonstrated how to convert an Excel sheet to PDF in C# and explored various customization options. If you have any questions, feel free to reach out via our forum.
By leveraging the $99 Aspose Plugin, you can achieve high-performance Excel to PDF conversion in C#. Whether you’re working with .NET Core or ASP.NET, this powerful library allows you to convert XLS and XLSX files to PDF efficiently, ensuring layout preservation and offering extensive customization options for all your document conversion needs.