Convert Excel XLS and XLSX to PDF in C#

Excel to PDF C#

This article demonstrates how to convert Excel (XLS, XLSX) files to PDF format using C#. The Portable Document Format (PDF) format developed by Adobe is used as a standard by many organizations to share and store documents. Exporting documents to PDF is a common scenario for many applications.

C# API for Excel to PDF Conversion

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

Convert Excel XLS or XLSX to PDF in C#

Aspose.Cells for .NET provides easy to use API with which you can convert Excel files to PDF with these simple steps.

  1. Instantiate the Workbook class with the Excel document that you want to convert.
  2. Save the document in PDF format by specifying the save format as PDF by using the SaveFormat Enumeration

The following code snippet demonstrates how to convert Excel XLS 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 Compliant PDF using C#

PDF/A is an ISO-standardized version of PDF that prohibits features that are not suitable for long term archiving. Saving PDF like this ensures that nothing will break in the long run.

The following code snippet demonstrates this feature by creating a new workbook and convert it to a PDF/A compliant PDF format:

// Instantiate new workbook
Workbook workbook = new Workbook();

// Insert a value into the 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 provides the ability to track the conversion progress by providing the IPageSavingCallback interface. You can create a custom class that implements this interface and assign its instance to PdfSaveOptions.PageSavingCallback property.

The following code snippet demonstrates how to track the Excel to PDF conversion progress using C#.

//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;
        }
    }
}

Conclusion

In this article, you have learned how to convert Excel XLSX or XLS files to PDF using C#. For more information on converting Excel files to PDF, head on over to our documentation, Convert Excel Workbook to PDF. In case you would have any queries, feel free to let us know via our forum.

See Also