Back to Devexpress

Print a Report

xtrareports-400016-desktop-reporting-common-features-printing-print-reports-in-wpf-print-a-report.md

latest6.5 KB
Original Source

Print a Report

  • Aug 18, 2023
  • 4 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.

  • 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.

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:

csharp
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");
}
vb
Imports DevExpress.Xpf.Printing
' ...

Private Sub Window_Loaded(sender As Object, e As System.Windows.RoutedEventArgs)
    Dim report As XtraReport1 = 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")
End Sub

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.

Use the CachedReportSource component to preview a large report that contains over 10,000 pages. The CachedReportSource component stores generated pages in the file system, database, or compressed memory streams. The dedicated storage allows you to overcome memory allocation limitations and avoid the OutOfMemory exception. The size of a document is limited only by the available storage space. A substantial increase in the number of pages does not lead to a significant increase in memory consumption.

The CachedReportSource may decrease overall performance because of the time required to store a page and retrieve it from the storage. You should test your application and decide whether it can benefit from the CachedReportSource component.

The following code creates a CachedReportSource instance that uses the MemoryDocumentStorage storage type for the specified report.

csharp
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
//...
    var storage = new MemoryDocumentStorage();
    var report = new XtraReport1();
    var cachedReportSource = new CachedReportSource(report, storage);
vb
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Caching
'...
    Private storage = New MemoryDocumentStorage()
    Private report = New XtraReport1()
    Private cachedReportSource = New CachedReportSource(report, storage)

Instead of memory storage, you can use the FileDocumentStorage, or DbDocumentStorage storage types.

Call one of the PrintHelper ‘s methods and pass the CachedReportSource instance as a parameter.

csharp
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");
}
vb
Imports DevExpress.Xpf.Printing
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Caching
' ...

Private Sub Window_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim storage = New MemoryDocumentStorage()
    Dim report = New XtraReport1()
    Dim 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")
End Sub

The PrintHelper object automatically starts the document creation process. You should not call the CachedReportSource.CreateDocumentAsync method before creating the PrintHelper instance.

See Also

Create a Report in Visual Studio

Merge Reports