
Excel files are widely used to keep and share the tabular data. On the other hand, PDF format has been among the ruling digital document formats. In certain cases, the Excel files are to be converted into PDF format programmatically. To achieve that, this article demonstrates how to convert Excel XLS XLSX to PDF in C#.
- C# API for Excel to PDF Conversion - Free Download
- Steps to Convert an Excel File to PDF in C#
- Convert Excel to PDF in C#
- C# Set PDF Compliance in Excel to PDF
- Track Excel to PDF Conversion in C#
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
How to Convert an Excel File to PDF in C#
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 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.
- Instantiate the Workbook class with the Excel document that you want to convert.
- 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);
C# Excel to PDF/A Compliant PDF Conversion
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);
C# Excel XLS XLSX to PDF - Track 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 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;
}
}
}
C# Excel to PDF Converter - Get a Free License
You can use Aspose.Cells for .NET without evaluation limitations using a temporary license.
Conclusion
In this article, you have learned how to convert Excel XLSX or XLS files to PDF in 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.