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 various purposes, such as sharing reports or preserving the formatting. In this blog post, we’ll explore how to convert Excel sheets to PDF using C#. Also, we’ll go through different options to customize Excel to PDF 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 within a couple of steps. This is how you can save an Excel file as using the API.

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

And that’s it. Now, let’s have a look at how to perform Excel to PDF conversion via C# code.

Convert Excel 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 Format

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 how to convert an Excel workbook to a PDF/A compliant PDF format in C#.

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

Online Excel to PDF Converter

Use our online Excel to PDF converter app, which is based on Aspose.Cells. This is a free app and you do not have to sign up for this.

Get Free C# Excel to PDF Converter

You can use Aspose.Cells for .NET without evaluation limitations using a free temporary license.

Conclusion

Converting Excel sheets to PDF is a common requirement in various business and development scenarios. Aspose.Cells for .NET makes this task easy and efficient, allowing you to automate the process within your C# applications. In this blog post, we’ve demonstrated an example of how to convert an Excel sheet to PDF in C#. Also, we have covered how to customized Excel to PDF conversion with different options. In case you would have any queries, feel free to let us know via our forum.

See Also