Excel to PDF C#

Microsoft Excel is a powerful tool for creating and managing spreadsheets. However, there are times when you need to convert your Excel sheets to PDF for sharing reports or preserving formatting. In this blog post, we’ll explore how to convert Excel sheets to PDF using C# and review options to customize the conversion.

C# Excel to PDF Conversion Library

Aspose.Cells for .NET API makes converting Excel spreadsheets to PDF a breeze. You can either download the API’s DLL or install it using NuGet.

PM> Install-Package Aspose.Cells

Steps to Convert an Excel XLS to PDF

Using Aspose.Cells for .NET, you can easily convert an Excel file to PDF in a few steps.

  • Load the Excel file from disk.
  • Save it as PDF to the desired location.

Now, let’s see how to perform Excel to PDF conversion with C# code.

Convert Excel XLSX to PDF in C#

Aspose.Cells for .NET provides an easy‑to‑use API to convert Excel files to PDF.

  1. Instantiate the Workbook class with the Excel document you want to convert.
  2. Save the document in PDF format by specifying SaveFormat.Pdf.

The following code snippet shows how to convert an Excel file to PDF in C#.

Workbook workbook = new Workbook("SampleExcel.xls");
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 as PDF/A ensures future compatibility.

The code below converts an Excel workbook to a PDF/A‑compliant PDF.

Workbook workbook = new Workbook();
workbook.Worksheets[0].Cells[0, 0].PutValue("Testing PDF/A");
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b;
workbook.Save(dataDir + "output.pdf", pdfSaveOptions);

Track Excel to PDF Conversion in C#

Aspose.Cells for .NET lets you track conversion progress via the IPageSavingCallback interface. Implement the interface and assign it to PdfSaveOptions.PageSavingCallback.

Workbook workbook = new Workbook("PagesBook1.xlsx");
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.PageSavingCallback = new TestPageSavingCallback();
workbook.Save("DocumentConversionProgress.pdf", pdfSaveOptions);

The custom class below implements IPageSavingCallback to monitor 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);
        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);
        if (args.PageIndex >= 8)
        {
            args.HasMorePages = false;
        }
    }
}

Online Excel to PDF Converter

Use our online Excel to PDF converter app, which is based on Aspose.Cells. This free app requires no sign‑up.

Image

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 many business and development scenarios. Aspose.Cells for .NET simplifies this task, allowing you to automate conversion within your C# applications. This post demonstrated how to convert an Excel sheet to PDF, customize the output, and track progress. If you have questions, feel free to ask in our forum.

See Also