How to: Export a Workbook to PDF

  • 3 min to read
Important

The Workbook class is defined in the DevExpress.Docs.v19.2.dll assembly. Add this assembly to your project to use the workbook functionality. You require 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 The Entire 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 exporting the specific worksheet(s).

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 code example below 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.
          }
      }
  }
}

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