Skip to main content
.NET 8.0+

How to: Customize the Report Export Options

  • 2 minutes to read

This example demonstrates how to access the ExportOptions object, which stores document export options for different formats. These options are applied when you export a report from a Report Viewer.

In this topic, it is assumed that you have an XAF application that uses the Reports V2 Module, and you have created one or more reports (see Reports V2 Module Overview).

1. Implement an ExportConfigurator Helper Class

In the platform-agnostic module, declare the following helper class:

using DevExpress.XtraPrinting;
// ...
public static class ExportConfigurator {
    public static void Setup(ExportOptions exportOptions) {
        SetHtmlOptions(exportOptions.Html);
        SetPdfOptions(exportOptions.Pdf);
        SetXlsOptions(exportOptions.Xls);
    }
    private static void SetXlsOptions(XlsExportOptions xlsExportOptions) {
        // XLS-specific options: 
        xlsExportOptions.SheetName = "CustomXlsSheetTitle";
        xlsExportOptions.ShowGridLines = true;
    }
    private static void SetPdfOptions(PdfExportOptions pdfExportOptions) {
        // PDF-specific options: 
        pdfExportOptions.DocumentOptions.Title = "CustomPdfTitle";
        pdfExportOptions.ImageQuality = PdfJpegImageQuality.Medium;
    }
    private static void SetHtmlOptions(HtmlExportOptions htmlExportOptions) {
        // HTML-specific options: 
        htmlExportOptions.Title = "CustomHtmlTitle";
        htmlExportOptions.ExportMode = HtmlExportMode.SingleFilePageByPage;
        htmlExportOptions.PageBorderColor = System.Drawing.Color.Gray;
        htmlExportOptions.EmbedImagesInHTML = true;
    }
}

2. Handle the BeforeShowPreview Event

In .NET Applications

In the application’s Startup.cs file, add the OnBeforeShowPreview event handler to the builder.Modules.AddReports method call. In this event handler, call the ExportConfigurator.Setup static method as shown below:

File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs

// ...
builder.Modules
    .AddReports(options => {
        // ...
        options.Events.OnBeforeShowPreview = context => {
            ExportConfigurator.Setup(context.Report.ExportOptions);
        };
    })
// ...

In .NET Framework Applications

In the platform-agnostic module’s Module.cs file, override the ModuleBase.Setup method, find the ReportsModuleV2 instance using the static ReportsModuleV2.FindReportsModule method and subscribe to the ReportDataSourceHelper.BeforeShowPreview event.

File: MySolution.Module/Module.cs

using DevExpress.ExpressApp.ReportsV2;
// ...
public override void Setup(ApplicationModulesManager moduleManager) {
    base.Setup(moduleManager);
    ReportsModuleV2 reportsModule = ReportsModuleV2.FindReportsModule(moduleManager.Modules);
    if(reportsModule != null) {
        reportsModule.ReportsDataSourceHelper.BeforeShowPreview += ReportsDataSourceHelper_BeforeShowPreview;
    }
}

private void ReportsDataSourceHelper_BeforeShowPreview(object sender, BeforeShowPreviewEventArgs e) {
    // Execute the `ExportConfigurator.Setup` static helper method implemented in the first step.
    ExportConfigurator.Setup(e.Report.ExportOptions);
}
See Also