Skip to main content
A newer version of this page is available. .

How to: Export a Workbook to PDF

  • 3 minutes to read

Important

The Workbook class is defined in the DevExpress.Docs.v21.1.dll assembly. Add this assembly to your project to use the Workbook API. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.

Use the Workbook.ExportToPdf method overloads to save a workbook in PDF format.

Export a Document

using (FileStream pdfFileStream = new FileStream("Documents\\Document_PDF.pdf", FileMode.Create))
{
    workbook.ExportToPdf(pdfFileStream);
}

Export Individual Worksheets

// Specify export options.
PdfExportOptions options = new PdfExportOptions();
options.DocumentOptions.Author = "John Smith";
options.ImageQuality = PdfJpegImageQuality.Medium;

using (FileStream pdfFileStream = new FileStream("Document_PDF.pdf", FileMode.Create))
{
    workbook.ExportToPdf(pdfFileStream, options, "Sheet1", "Sheet2");
}

Tip

Handle the Workbook.BeforePrintSheet event to cancel export for specific worksheets.

Asynchronous Export

Use the Workbook.ExportToPdfAsync methods to asynchronously export a workbook or individual worksheets to PDF format.

Important

Take into account the following when you call this method:

  • The events fired by this method call may occur in a different thread than the target operation.

  • The operation is not thread safe (the document should not be accessed simultaneously by different threads). Wait until the operation is completed before you continue to work with the document (for example, use the await operator).

The following example shows how to asynchronously convert an XLSX file to PDF format:

private async void ConvertXlsx2PdfWithCancellation()
{
  using (CancellationTokenSource source = new CancellationTokenSource(10000))
  {
    using (Workbook workbook = new Workbook())
      {
          try
          {
              await workbook.LoadDocumentAsync("Document.xlsx", source.Token);
              await workbook.ExportToPdfAsync("Result.pdf", source.Token);
          }
          catch (OperationCanceledException)
          {
              // Your code to handle cancellation.
          }
      }
  }
}

Calculate Formulas Before Export

The default calculation mode for a Workbook is Manual. This mode implies that the Spreadsheet component does not recalculate formulas before it generates a PDF document. Call the Workbook.Calculate or Workbook.CalculateFull method to calculate all formulas in the workbook before you export it to PDF.

using (Workbook workbook = new Workbook())
{
    // Load a document.
    // ...
    // Modify the document.
    // ...
    // Calculate formulas in the document.
    workbook.Calculate();
    // Export the document to PDF.
    workbook.ExportToPdf("Document_PDF.pdf");
}

Export Workbooks with 3-D Charts to PDF in .NET Core Apps on Linux

If you run your .NET Core app on a Linux server distribution or Docker Linux Container, use the following terminal command to install additional libraries to export spreadsheets with 3-D charts to PDF:

sudo apt install libosmesa6 libglu1-mesa

If you use Linux with a window system (for example, X Window System), install one of the following libraries:

  • EGL (Embedded-System Graphics Library)

    sudo apt install libegl1
    
  • GLX (OpenGL extension to the X Window System)

    sudo apt install libx11-dev
    
See Also