Convert XLS and XLSX to PDF in C#

Share on FacebookTweet about this on TwitterShare on LinkedIn
Aspose.Cells for .NET logo

This article demonstrates how to convert Excel (XLS, XLSX) files to PDF format. 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. Aspose.Cells for .NET API makes exporting Excel files to PDF a breeze.

Convert XLS, XLSX to PDF

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 the conversion process.

// 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 XLS, XLSX to PDF/A Compliant PDF

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 saving it in PDF/A Compliant PDF format using the PdfSaveOptions class.

// 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 XLS, XLSX to PDF conversion

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 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.

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

For more information on converting Excel files to PDF, head on over to our documentation, Convert Excel Workbook to PDF. To get started with the API, get the free version using the Nuget Package Manager or download directly from here.