Convert Excel to PDF in C#

2024-01-26 03:31:33

PDF is a widely accepted and platform-independent file format. Converting Excel to PDF ensures that the document can be accessed and viewed by anyone with a PDF reader, thereby eliminating compatibility issues and guaranteeing consistent presentation of the data.

Furthermore, PDF files are usually read-only. In regulated industries, such as finance, healthcare, or legal sectors, compliance requirements may dictate that documents should be in non-editable formats. Converting Excel to PDF helps fulfill these regulatory obligations, ensuring data integrity and authenticity.

This article will give three examples to demonstrate how to use C# to convert Excel to PDF.

C# Excel to PDF Converter Library

To use C# for Excel to PDF conversion, Spire.XLS for .NET library is required. It is a powerful library that provides full support for working with Excel files in .NET platforms. It gives developers the flexibility to convert Excel to a variety of file formats such as Excel to PDF, Excel to HTML, Excel to image, Excel to CSV, etc., enabling seamless integration with different applications and systems.

Installation:

Before getting started, download the C# Excel to PDF converter to manually add the DLL files as references in your .NET project. Or install it directly via NuGet.

PM> Install-Package Spire.XLS

Classes and Properties:

Once installed, you can use the following classes, properties and methods provided by the Spire.XLS for .NET library to programmatically convert Excel to PDF in C#.

Item Description
Workbook class Represents an Excel workbook.
Worksheet class Represents a worksheet in a workbook.
ConverterSetting class Represents the Excel to PDF conversion options
PageSetup Class Represents the page setup of a worksheet.
Workbook.LoadFromFile() method Loads an Excel workbook.
Workbook.Worksheets[] property Gets a specific worksheet by index.
ConverterSetting.SheetFitToPage property Renders the worksheet on a single PDF page.
Worksheet.PageSetup property Returns a PageSetup object that contains all the page setup attributes such as margins, paper size, etc.
Workbook.SaveToFile(string fileName, FileFormat.PDF) method Save the Excel workbook as a PDF file.
Worksheet.SaveToPdf() method Save the Excel worksheet as a PDF file.

Convert Excel XLS XLSX to PDF in C#

To save Excel as PDF in C#, simply load a .xls or .xlsx file, and then call the SaveToFile method to save it in PDF format. During the process, you can control the conversion settings through the ConverterSetting class.

The following code example shows how to convert an Excel (XLS/XLSX) file to PDF.

  • Python
using Spire.Xls;

namespace ConvertExcelToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel file 
            workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Planner.xlsx");

            //Set worksheets to fit to page when converting
            workbook.ConverterSetting.SheetFitToPage = true;

            //Save Excel XLSX as PDF
            workbook.SaveToFile("XlsxToPdf.pdf", FileFormat.PDF);
        }
    }
}

Convert Excel to PDF in C#

Convert a Specific Worksheet to PDF in C#

The Worksheet.SaveToPdf() method is used to save a specific worksheet as PDF. If you want to set the page margins, sizes, or other attributes of the worksheet to achieve a desired rendering effect, you can use the corresponding properties of the PageSetup Class.

The following code example shows how to convert a specific worksheet to PDF.

  • Python
using Spire.Xls;

namespace ConvertWorksheetToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel file
            workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Planner.xlsx");


            //Get the third worksheet
            Worksheet sheet = workbook.Worksheets[2];

            //Set page margins
            sheet.PageSetup.TopMargin = 0.6;
            sheet.PageSetup.BottomMargin = 0.6;
            sheet.PageSetup.LeftMargin = 0.6;
            sheet.PageSetup.RightMargin = 0.6;

            //Set worksheet to fit to page when converting
            workbook.ConverterSetting.SheetFitToPage = true;

            //Convert the worksheet to PDF
            sheet.SaveToPdf("WorksheetToPdf.pdf");
        }
    }
}

Convert Excel to PDF in C#

Convert Each Worksheet to a Separate PDF File in C#

The first code example converts each worksheet into a single page in a PDF file. With Spire.XLS for .NET library, it is also allowed to iterate through the worksheets and save each as a separate PDF file.

The following code example shows how to convert each worksheet in an Excel file to a separate PDF file.

  • Python
using Spire.Xls;

namespace WorksheetToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel file
            workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Planner.xlsx");

            //Set worksheets to fit to page when converting
            workbook.ConverterSetting.SheetFitToPage = true;

            //Iterate through the worksheets in the Excel file 
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                //Convert each worksheet to a different PDF file
                string FileName = sheet.Name + ".pdf";
                sheet.SaveToPdf("Sheets\\" + FileName);
            }
        }
    }
}

Convert Excel to PDF in C#

Free Excel to PDF Online Converter

Try this online converter to convert your Excel XLS or XLSX files to PDF format for free. But please be aware that the free converter only supports simple conversion without additional settings. You can upload an Excel file and wait a few seconds to get the Excel to PDF conversion done.

Conclusion

In this post, you have gained knowledge on how to use C# to convert Excel to PDF programmatically. The main classes, properties, and methods involved in the Excel to PDF conversion process have been thoroughly explained, and complete sample code for converting an Excel workbook or worksheet to PDF have also been provided.

To explore more about the Spire.XLS for .NET library, refer to the documentation, where you can find other Excel generation, processing, and conversion features along with the code examples.

See Also