Print a Report
- 3 minutes to read
This topic describes how to print a report in a WPF application.
Essential Methods
You can print a report in a WPF application using the following PrintHelper class’s methods:
The PrintHelper.PrintDirect method sends a report to a printer with the default print settings. This method provides overloads allowing you to use the system’s default or a specific printer.
In the Document Preview, the Quick Print command corresponds to this method.
Note
An exception is thrown when a printer with the specified name is not found.
The PrintHelper.Print method opens the Print dialog where end-users can select a printer and specify other printing options before printing the report document.
In the Document Preview, the Print command corresponds to this method.
Print Light-Weight Reports
Call one of the PrintHelper‘s methods listed above and pass a report instance as a parameter.
The following example illustrates how use the PrintHelper.PrintDirect and PrintHelper.Print methods to print a report:
using DevExpress.Xpf.Printing;
// ...
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) {
XtraReport1 report = new XtraReport1();
// Invoke the Print dialog.
PrintHelper.Print(report);
// Send the report to the default printer.
PrintHelper.PrintDirect(report);
// Send the report to the specified printer.
PrintHelper.PrintDirect(report, "myPrinter");
}
Tip
Calling any of these methods generates a document by calling the XtraReport.CreateDocument method internally if it is not generated yet.
Note
If you want to print multiple reports in a single batch (as a single print job), merge these reports into one document before sending it to a printer.
Print Large Reports
Use the CachedReportSource component to print reports that include a large amount of data. This component allows you to avoid memory consumption-related issues by storing pages in a file system or database during document generation.
Create a CachedReportSource instance specifying a report to be printed and a storage to cache the report document.
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
//...
var storage = new MemoryDocumentStorage();
var report = new XtraReport1();
var cachedReportSource = new CachedReportSource(report, storage);
The MemoryDocumentStorage object used in this code compactly stores a document in memory. You can use the FileDocumentStorage, DbDocumentStorage or implement a custom storage instead.
Call one of the PrintHelper‘s methods detailed above passing a CachedReportSource instance as a parameter.
using DevExpress.Xpf.Printing;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
// ...
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) {
var storage = new MemoryDocumentStorage();
var report = new XtraReport1();
var cachedReportSource = new CachedReportSource(report, storage);
// Invoke the Print dialog.
PrintHelper.Print(cachedReportSource);
// Send the report to the default printer.
PrintHelper.PrintDirect(cachedReportSource);
// Send the report to the specified printer.
PrintHelper.PrintDirect(cachedReportSource, "myPrinter");
}
Tip
Calling any of the printing methods generates a report document by calling the CachedReportSource.CreateDocumentAsync method internally if it not generated yet.