Excel to PDF C#

Microsoft Excel is an essential tool for managing spreadsheets, but there are times when converting these sheets to PDF becomes crucial—whether for sharing reports or preserving formatting. In this blog post, we’ll guide you through the process of efficiently converting Excel sheets to PDF using C#. We’ll also explore various customization options available during the conversion process, including methods for converting Excel to PDF free of charge.

Table of Contents

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:

  1. Load the Excel file from disk.
  2. 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:

  1. Instantiate the Workbook class with the Excel document you want to convert.
  2. 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, 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 Excel XLSX to PDF in .NET 6 and convert Excel XLS to PDF in .NET 7, while exploring 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 .NET. 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.

For those looking to convert large Excel files to PDF in .NET, the Aspose .NET Excel to PDF conversion library is your best choice. It supports C# .NET Excel to PDF asynchronous conversion and ensures that you can convert Excel to searchable PDF in C# .NET and convert Excel to secure PDF in C# .NET with ease.

Explore the .NET MAUI Excel to PDF Conversion Library for the latest features in document processing, and take advantage of the Excel to PDF conversion C# .NET Core: code example to get started quickly. Whether you’re looking for the .NET Framework Excel to PDF: best library or need to handle images during conversion, Aspose provides all the tools you need for efficient Excel to PDF conversion.