1. Home
  2. /
  3. GroupDocs.Conversion.LowCode
  4. /
  5. Developer Guide
  6. /
  7. Using XLS to PDF Converter

Using XLS to PDF Converter

This topic shows how to use the XlsToPdfConverter with examples that demonstrate common use cases for converting Excel XLS files to PDF.

Prerequisites

Refer to the Developer Guide to learn how to set up your environment and run code examples in this section.

You can also check two related topics on Loading Source Documents and Saving Converted Documents to learn how to specify input and output documents.

Basic Example

The following example shows the most common use case for converting Excel XLS document to PDF. The source XLS file is loaded from a current folder. The converted file is saved to the same folder.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Create a converter for the XLS file
var converter = new XlsToPdfConverter("cost-analysis.xls");

// Convert XLS to PDF
converter.Convert("cost-analysis.pdf");
Imports System
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Create a converter from file path
        Dim converter As New XlsToPdfConverter("cost-analysis.xls")

        ' Convert XLS to PDF
        converter.Convert("cost-analysis.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Create a converter from file path
    let converter = new XlsToPdfConverter("cost-analysis.xls")

    // Convert XLS to PDF
    converter.Convert("cost-analysis.pdf")
    0

Sample input file: Download cost-analysis.xls

Spreadsheet Load Options

This section covers only the main scenarios. You can also refer to the API references for the SpreadsheetLoadOptions for a complete list of options that you can specify.

Convert Protected XLS to PDF

The following example shows how to convert protected XLS file and save it to unprotected PDF file.

In case you do not specify password for protected document PasswordRequiredException is going to be thrown.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Provide password through load options
var converter = new XlsToPdfConverter("protected.xls", options =>
{
    options.Password = "12345";
});

// Convert XLS to PDF
converter.Convert("unprotected.pdf");
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Provide password through load options
        Dim converter As New XlsToPdfConverter("protected.xls", Sub(options)
            options.Password = "12345"
        End Sub)

        ' Convert XLS to PDF
        converter.Convert("unprotected.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply license
    License.Set(publicKey, privateKey)

    // Provide password through load options
    let converter = new XlsToPdfConverter("protected.xls", fun options ->
        options.Password <- "12345"
    )
    
    // Convert XLS to PDF
    converter.Convert("unprotected.pdf")
    0

Sample input: Download protected.xls (Password: 12345)

Expected output: Download unprotected.pdf

Convert Specific Sheets from XLS to PDF

The following example shows how to convert only specific sheets from an XLS file to PDF using the SheetIndexes property.

using System;
using System.Collections.Generic;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Convert only specific sheets through load options
var converter = new XlsToPdfConverter("invoice-tracker.xls", options =>
{
    options.SheetIndexes = new List<int> { 0, 2 }; // Convert first and third sheets
});

// Convert XLS to PDF
converter.Convert("specific-sheets.pdf");
Imports System
Imports System.Collections.Generic
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Convert only specific sheets through load options
        Dim converter As New XlsToPdfConverter("invoice-tracker.xls", Sub(options)
            options.SheetIndexes = New List(Of Integer) From {0, 2} ' Convert first and third sheets
        End Sub)

        ' Convert XLS to PDF
        converter.Convert("specific-sheets.pdf")
    End Sub
End Module
open System
open System.Collections.Generic
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")
    
    // Apply license
    License.Set(publicKey, privateKey)

    // Convert only specific sheets through load options
    let converter = new XlsToPdfConverter("invoice-tracker.xls", fun options ->
        options.SheetIndexes <- List<int>([0; 2]) // Convert first and third sheets
    )
    
    // Convert XLS to PDF
    converter.Convert("specific-sheets.pdf")
    0

Sample input: Download invoice-tracker.xls (Contains multiple sheets)

Expected output: Download specific-sheets.pdf (Only selected sheets included)

Convert XLS with Hidden Sheets

By default hidden sheets are not added to the converted PDF document.

The following example shows how to include hidden sheets when converting XLS to PDF using the ShowHiddenSheets property.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Show hidden sheets through load options
var converter = new XlsToPdfConverter("hidden-sheets.xls", options =>
{
    options.ShowHiddenSheets = true;
});

// Convert XLS to PDF
converter.Convert("with-hidden-sheets.pdf");
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Show hidden sheets through load options
        Dim converter As New XlsToPdfConverter("hidden-sheets.xls", Sub(options)
            options.ShowHiddenSheets = True
        End Sub)

        ' Convert XLS to PDF
        converter.Convert("with-hidden-sheets.pdf")
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")
    
    // Apply license
    License.Set(publicKey, privateKey)

    // Show hidden sheets through load options
    let converter = new XlsToPdfConverter("hidden-sheets.xls", fun options ->
        options.ShowHiddenSheets <- true
    )
    
    // Convert XLS to PDF
    converter.Convert("with-hidden-sheets.pdf")
    0

Sample input: Download hidden-sheets.xls (Contains hidden sheets)

Expected output: Download with-hidden-sheets.pdf (Hidden sheets included)

PDF Convert Options

The examples in this section shows how you can adjust the output using PdfConvertOptions.

Convert XLS to PDF with Specific PDF Format

You can specify the PDF format for the output file using the PdfFormat property in PdfOptions class. This allows you to create PDF files that conform to specific standards like PDF/A for archiving or PDF/X for print production.

The following example shows how to convert an XLS file to PDF/A-1b format, which is commonly used for long-term archiving:

using System;
using GroupDocs.Conversion.LowCode;
using GroupDocs.Conversion.Options.Convert;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Create the converter
var converter = new XlsToPdfConverter("cost-analysis.xls");

// Convert to PDF/A-1b format for archiving
converter.Convert("converted.pdf", convertOptions =>
{
    convertOptions.PdfOptions.PdfFormat = PdfFormats.PdfA_1B;
});
Imports GroupDocs.Conversion.LowCode
Imports GroupDocs.Conversion.Options.Convert

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey As String = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey As String = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Create the converter
        Dim converter As New XlsToPdfConverter("cost-analysis.xls")

        ' Convert to PDF/A-1b format for archiving
        converter.Convert("converted.pdf", Sub(convertOptions)
            convertOptions.PdfOptions.PdfFormat = PdfFormats.PdfA_1B
        End Sub)
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode
open GroupDocs.Conversion.Options.Convert

[<EntryPoint>]
let main argv =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply license
    License.Set(publicKey, privateKey)

    // Create the converter
    let converter = new XlsToPdfConverter("cost-analysis.xls")

    // Convert to PDF/A-1b format for archiving
    converter.Convert("converted.pdf", fun convertOptions ->
        convertOptions.PdfOptions.PdfFormat <- PdfFormats.PdfA_1B
    )

    0 // return exit code

Expected output: Download converted.pdf (PDF/A-1b format)

Convert XLS to Password-Protected PDF

You can protect the output PDF with a password by setting the Password property in PdfConvertOptions class.

using System;
using GroupDocs.Conversion.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply license
License.Set(publicKey, privateKey);

// Create the converter
var converter = new XlsToPdfConverter("cost-analysis.xls");

// Convert to password-protected PDF
converter.Convert("protected.pdf", convertOptions =>
{
    convertOptions.Password = "12345";
});
Imports GroupDocs.Conversion.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply license
        License.Set(publicKey, privateKey)

        ' Create the converter
        Dim converter As New XlsToPdfConverter("cost-analysis.xls")

        ' Convert to password-protected PDF
        converter.Convert("protected.pdf", Sub(convertOptions)
            convertOptions.Password = "12345"
        End Sub)
    End Sub
End Module
open System
open GroupDocs.Conversion.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply license
    License.Set(publicKey, privateKey)

    // Create the converter
    let converter = new XlsToPdfConverter("cost-analysis.xls")

    // Convert to password-protected PDF
    converter.Convert("protected.pdf", fun convertOptions ->
        convertOptions.Password <- "12345"
    )

    0

Expected output: Download protected.pdf (Password: 12345)