How to: Export a Workbook to PDF

  • 3 minutes to read
IMPORTANT

The Workbook class is defined in the DevExpress.Docs.v20.2.dll assembly. Add this assembly to your project to use the Workbook API. You need a license to the DevExpress Office File API or DevExpress Universal Subscription to use this assembly in production code. Refer to the DevExpress Subscription page for pricing information.

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's call may occur in a different thread than the target operation.

  • The operation is not thread safe (documents should not be accessed simultaneously by different threads). Wait until the operation is completed before working with the document, i.e., 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