Skip to main content

Print a Report

  • 4 minutes to read

This topic describes how to print a report in a WinForms application.

Essential Methods

Create the ReportPrintTool class’ instance and use any of the following methods to print a report:

  • The PrintToolBase.Print method sends a document to a printer (the system’s default or a specific printer) using default printing settings.

    Note

    An exception is thrown when a printer with the specified name is not found.

    In Print Preview, this method corresponds to the Quick Print preview-icon-vector-quick-print command.

  • The PrintTool.PrintDialog method raises the Print dialog, where an end-user can select a printer and specify other print options before printing the document:

    PrintDialog

    In Print Preview, this method corresponds to the Print preview-icon-vector-print command.

Pass the report’s instance to the ReportPrintTool constructor as a parameter. Use one of the ReportPrintTool‘s methods listed above to print the associated report.

The following code illustrates how to use these methods to print a report in a Windows Forms application:

using System;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
// ...

private void button1_Click(object sender, EventArgs e) {
    XtraReport1 report = new XtraReport1();
    ReportPrintTool printTool = new ReportPrintTool(report);
    // Invoke the Print dialog.
    printTool.PrintDialog();
    // Send the report to the default printer.
    printTool.Print();
    // Send the report to the specified printer.
    printTool.Print("myPrinter");
}

Tip

Calling any of these methods generates a report document by calling the XtraReport.CreateDocument method internally if the document is not created yet.

Use the CachedReportSource component to print reports that include a huge 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 stores a document in memory in a compact way. You can use the FileDocumentStorage, DbDocumentStorage or implement a custom storage instead.

Pass a CachedReportSource instance to the ReportPrintTool constructor as a parameter. Use one of the ReportPrintTool‘s methods listed above to print the report associated with the CachedReportSource object.

using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting.Caching;
// ...

private void button_Click(object sender, EventArgs e) {
    var storage = new MemoryDocumentStorage();
    var report = new XtraReport1();
    var cachedReportSource = new CachedReportSource(report, storage);
    var printTool = new ReportPrintTool(cachedReportSource);
    // Invoke the Print dialog.
    printTool.PrintDialog();
}

Tip

Calling any of the printing methods generates a report document by calling the CachedReportSource.CreateDocumentAsync method internally if the document is not created yet.

Set the Document Name of a Print Job

Specify the Document.Name property of a report’s PrintingSystem. Call CreateDocument before changing this property’s value to initialize this report’s Printing System.

Note that `Document.Name`` is displayed in the Save File dialog window and is used as a file name on document export.

XtraReport1 report = new XtraReport1();  
report.CreateDocument();  
report.PrintingSystem.Document.Name = "Application Name - Report Name";  
report.PrintDialog();