Convert Excel files to PDF in C# and VB.NET

With GemBox.Spreadsheet you can easily convert Excel files from one format to another, using just C# or VB.NET code. You can load or read any supported input file format and save or write it as any supported output file format. The complete list of formats is available on the Supported File Formats help page.

For both reading and writing, you can either provide the file's path or a stream by using one of the ExcelFile.Load or ExcelFile.Save methods.

The following example shows how you can convert an Excel file to PDF using the default XlsxLoadOptions and specified PdfSaveOptions.

Converted Excel workbook to PDF format in C# and VB.NET
Screenshot of input Excel and converted output PDF
Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // In order to convert Excel to PDF, we just need to:
        //   1. Load XLS or XLSX file into ExcelFile object.
        //   2. Save ExcelFile object to PDF file.
        ExcelFile workbook = ExcelFile.Load("%InputFileName%");
        workbook.Save("Convert.pdf", new PdfSaveOptions() { SelectionType = SelectionType.EntireFile });
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' In order to convert Excel to PDF, we just need to
        '   1. Load XLS Or XLSX file into ExcelFile object.
        '   2. Save ExcelFile object to PDF file.
        Dim workbook As ExcelFile = ExcelFile.Load("%InputFileName%")
        workbook.Save("Convert.pdf", New PdfSaveOptions() With {.SelectionType = SelectionType.EntireFile})
    End Sub
End Module

Note, when converting an Excel workbook to PDF, the machine that's executing the code should have the fonts that are used in the workbook installed on it. If not, you can provide them as private fonts or as embedded fonts.

Convert Excel range to PDF

In addition to saving the whole Excel workbook as a single PDF file, you can also convert each sheet into a separate PDF file.

The following example shows how you can specify a single sheet and the cell range within the sheet that you want to export to PDF.

Excel cell range converted to PDF file in C# and VB.NET
Screenshots of converted Excel cell range to PDF file
Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Load Excel file.
        ExcelFile workbook = ExcelFile.Load("%InputFileName%");

        // Get Excel sheet you want to export.
        ExcelWorksheet worksheet = workbook.Worksheets[0];

        // Set targeted sheet as active.
        workbook.Worksheets.ActiveWorksheet = worksheet;

        // Get cell range that you want to export.
        CellRange range = worksheet.Cells.GetSubrange("A5:I14");

        // Set targeted range as print area.
        worksheet.NamedRanges.SetPrintArea(range);

        // Save to PDF file.
        // By default, the SelectionType.ActiveSheet is used.
        workbook.Save("Convert.pdf");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load Excel file.
        Dim workbook As ExcelFile = ExcelFile.Load("%InputFileName%")

        ' Get Excel sheet you want to export.
        Dim worksheet As ExcelWorksheet = workbook.Worksheets(0)

        ' Set targeted sheet as active.
        workbook.Worksheets.ActiveWorksheet = worksheet

        ' Get cell range that you want to export.
        Dim range As CellRange = worksheet.Cells.GetSubrange("A5:I14")

        ' Set targeted range as print area.
        worksheet.NamedRanges.SetPrintArea(range)

        ' Save to PDF file.
        ' By default, the SelectionType.ActiveSheet is used.
        workbook.Save("Convert.pdf")
    End Sub
End Module

Convert Excel files to PDF/A

GemBox.Spreadsheet also supports saving the Excel file to PDF/A, an ISO-standardized version of the Portable Document Format (PDF) specialized for use in the archiving and long-term preservation of electronic documents.

The following example shows how you can convert an Excel file to PDF/A using PdfConformanceLevel to specify the conformance level and version.

Converted Excel workbook to PDF/A format in C# and VB.NET
Screenshot of converted output PDF/A
Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        PdfConformanceLevel conformanceLevel = %PdfConformanceLevel%;

        // Load Excel file.
        var workbook = ExcelFile.Load("%InputFileName%");

        // Create PDF save options.
        var options = new PdfSaveOptions()
        {
            ConformanceLevel = conformanceLevel
        };

        // Save to PDF file.
        workbook.Save("Output.pdf", options);
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim conformanceLevel As PdfConformanceLevel = %PdfConformanceLevel%

        ' Load Excel file.
        Dim workbook = ExcelFile.Load("%InputFileName%")

        ' Create PDF save options.
        Dim options As New PdfSaveOptions() With
        {
            .ConformanceLevel = conformanceLevel
        }

        ' Save to PDF file.
        workbook.Save("Output.pdf", options)

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.