This example demonstrates how to implement an Action that prints a specific report without displaying its preview. The printed report uses custom sorting and includes only the items selected in the List View. The complete description is available in the How to: Print a Report Without Displaying a Preview (in Reports V2) topic.
You can also use the code from this example to access an XtraReport object and then export or email report content according to tutorials listed in the Export Reports topic.
Files to Review:
- InstantPrintReportController.cs
- BlazorPrintContactsController.cs
- _Host.cshtml
- WinInstantPrintReportController.cs
Documentation
- Reports V2 Module
- How to: Print a Report Without Displaying a Preview (in Reports V2)
- How to: Access Objects Selected in the Current View
- Data Sorting in Reports V2
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using MySolution.Module.BusinessObjects;
namespace InstantPrintReportsV2Example.Module.Controllers {
public abstract class PrintContactsController : ObjectViewController<ListView, Contact> {
public PrintContactsController() {
SimpleAction printAction = new SimpleAction(this, "PrintContacts", PredefinedCategory.Reports);
printAction.ImageName = "Action_Printing_Print";
printAction.Execute += delegate (object sender, SimpleActionExecuteEventArgs e) {
PrintReport("ContactReport", e.SelectedObjects);
};
}
protected abstract void PrintReport(string reportDisplayName, System.Collections.IList selectedObjects);
}
}
C#using DevExpress.Data.Filtering;
using DevExpress.Data.Helpers;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ReportsV2;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Xpo;
using DevExpress.XtraReports.UI;
using InstantPrintReportsV2Example.Module.Controllers;
using Microsoft.JSInterop;
using DevExpress.Xpo.DB;
using DevExpress.ReportServer.ServiceModel.DataContracts;
namespace InstantReport.Blazor.Server.Controllers {
public class BlazorPrintContactsController : PrintContactsController {
readonly IReportExportService reportExportService;
readonly IJSRuntime jsRuntime;
public BlazorPrintContactsController() : base() { }
[ActivatorUtilitiesConstructor]
public BlazorPrintContactsController(IServiceProvider serviceProvider) : base() {
reportExportService = serviceProvider.GetService<IReportExportService>();
jsRuntime = serviceProvider.GetService<IJSRuntime>();
}
protected override async void PrintReport(string reportDisplayName, System.Collections.IList selectedObjects) {
using XtraReport report = reportExportService.LoadReport<ReportDataV2>(r => r.DisplayName == reportDisplayName);
// Filter and sort report data
CriteriaOperator objectsCriteria = ((BaseObjectSpace)ObjectSpace).GetObjectsCriteria(((ObjectView)View).ObjectTypeInfo, selectedObjects);
SortProperty[] sortProperties = { new SortProperty("Age", SortingDirection.Descending) };
reportExportService.SetupReport(report, objectsCriteria.ToString(), sortProperties);
using Stream s = reportExportService.ExportReport(report, DevExpress.XtraPrinting.ExportTarget.Pdf);
using var streamRef = new DotNetStreamReference(s);
var fileName = reportDisplayName + ".pdf";
await jsRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
}
}
}
Razor@page "/"
@namespace InstantReportEF.Blazor.Server
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using DevExpress.ExpressApp.Blazor.Components
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, shrink-to-fit=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<!-- meta name="theme-color" content="#000000" -->
<title>InstantReportEF</title>
<base href="~/" />
<component type="typeof(BootstrapThemeLink)" render-mode="Static" />
</head>
<body>
@{
string userAgent = Request.Headers["User-Agent"];
bool isIE = userAgent.Contains("MSIE") || userAgent.Contains("Trident");
}
@if(isIE) {
<link href="css/site.css" rel="stylesheet" />
<div class="d-flex flex-column justify-content-center align-items-center h-100">
<div class="d-flex">
<img class="mt-2 mr-4" src="_content/DevExpress.ExpressApp.Blazor/images/Sad.svg" width="60" height="60" />
<div>
<div class="h1">Internet Explorer is not supported.</div>
<p style="font-size: 1rem; opacity: 0.75;" class="m-0">InstantReportEF cannot be loaded in Internet Explorer.<br>Please use a different browser.</p>
</div>
</div>
</div>
}
else {
<component type="typeof(SplashScreen)" render-mode="Static" param-Caption='"InstantReportEF"' param-ImagePath='"images/SplashScreen.svg"' />
<link href="_content/DevExpress.ExpressApp.Blazor/styles.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<script src="_content/DevExpress.ExpressApp.Blazor/scripts.js"></script>
<app class="d-none">
<component type="typeof(App)" render-mode="Server" />
</app>
<component type="typeof(AlertsHandler)" render-mode="Server" />
<div id="blazor-error-ui">
<component type="typeof(BlazorError)" render-mode="Static" />
</div>
<script src="_framework/blazor.server.js"></script>
}
<script>
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
</script>
</body>
</html>
C#using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ReportsV2;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.XtraReports.UI;
using InstantPrintReportsV2Example.Module.Controllers;
using Microsoft.Extensions.DependencyInjection;
namespace InstantPrintReportsV2Example.Module.Win {
public class WinInstantPrintReportController : PrintContactsController {
readonly IReportExportService reportExportService;
public WinInstantPrintReportController() : base() { }
[ActivatorUtilitiesConstructor]
public WinInstantPrintReportController(IServiceProvider serviceProvider) : base() {
reportExportService = serviceProvider.GetService<IReportExportService>();
}
protected override void PrintReport(string reportDisplayName, System.Collections.IList selectedObjects) {
using XtraReport report = reportExportService.LoadReport<ReportDataV2>(r => r.DisplayName == reportDisplayName);
// Filter and sort report data.
CriteriaOperator objectsCriteria = ((BaseObjectSpace)ObjectSpace).GetObjectsCriteria(((ObjectView)View).ObjectTypeInfo, selectedObjects);
SortProperty[] sortProperties = { new SortProperty("Age", SortingDirection.Descending) };
reportExportService.SetupReport(report, objectsCriteria.ToString(), sortProperties);
report.PrintDialog();
}
}
}