XtraReport.ExportToPdfAsync(Stream, PdfExportOptions, CancellationToken) Method
Asynchronously exports a report to the specified stream in PDF format.
Namespace: DevExpress.XtraReports.UI
Assembly: DevExpress.XtraReports.v26.1.dll
Declaration
public Task ExportToPdfAsync(
Stream stream,
PdfExportOptions options = null,
CancellationToken token = default(CancellationToken)
)
Parameters
| Name | Type | Description |
|---|---|---|
| stream | Stream | A Stream for output data. |
Optional Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| options | PdfExportOptions | null | The PDF export options. You can omit this parameter to use the current report export options. |
| token | CancellationToken | null | A cancellation token that the task observes. |
Returns
| Type | Description |
|---|---|
| Task | A task that exports the report. |
Remarks
This method is equivalent to the ExportToPdf(Stream, PdfExportOptions) method but does not lock other actions performed concurrently. For instance, the user interface remains operational while the application exports a report.
Call ExportToPdfAsync from an async method. Prefix the call with the await operator, as shown in the code sample below.
The optional CancellationToken parameter provides a way to send the cancellation signal to the task. The task monitors the token and stops when it receives the signal. Create a CancellationTokenSource class instance and pass its Token property to the ExportToPdfAsync method call. Call the CancellationTokenSource.Cancel method to stop the task.
Example
The code sample below exports a report to PDF asynchronously. A CancellationTokenSource class instance is used to allow users to interrupt the report export if it takes too long.
using DevExpress.XtraReports.UI;
using System;
using System.Threading;
// ...
using System.IO;
// ...
// Use cancellationTokenSource to allow users to stop the document creation process.
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
// ...
// The cancelButton_Click event handler uses cancellationTokenSource to stop the document creation process.
private void cancelButton_Click(object sender, EventArgs e)
{
cancellationTokenSource.Cancel();
}
// ...
async public void ExportReportToPdfStreamAsync()
{
// Create a simple report.
XtraReport report = new XtraReport()
{
Name = "SimpleReport",
Bands = {
new DetailBand() {
Controls = {
new XRLabel() {
Text = "Simple Report"
}
}
}
}
};
// Create the export file in the user's Downloads folder.
FileStream stream = File.Open(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".pdf",
FileMode.Create);
// Export the report to the created file.
await report.ExportToPdfAsync(
stream,
null,
cancellationTokenSource.Token);
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ExportToPdfAsync(Stream, PdfExportOptions, CancellationToken) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.