The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Export a Workbook to PDF

  • 2 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");
}

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.
          }
      }
  }
}
See Also