Convert Excel files to PDF

The following example shows how to convert an Excel file to PDF in C# and VB.NET using the GemBox.Spreadsheet library.

Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the 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 the 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
Converted Excel workbook to PDF format in C# and VB.NET
Screenshot of input Excel and converted output PDF

Note that 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, as shown in the example below.

Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the 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 the 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
Excel cell range converted to PDF file in C# and VB.NET
Screenshot of converted Excel cell range to PDF file

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.

Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the 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 the 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
Converted Excel workbook to PDF/A format in C# and VB.NET
Screenshot of converted output PDF/A

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy