Skip to main content

Print a Report

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

    In the Document Preview, the Quick Print preview-icon-vector-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 preview-icon-vector-quick-print command corresponds to this method.

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.

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.

using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
//...
    var storage = new MemoryDocumentStorage();
    var report = new XtraReport1();
    var 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.

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");
}

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

See Also