xtrareports-5191-desktop-reporting-common-features-printing-winforms-reporting-print-api-print-a-report.md
This topic describes how to print a report in a WinForms application.
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.
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:
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");
}
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
' ...
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim report As New XtraReport1()
Dim printTool As 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")
End Sub
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);
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Caching
'...
Private storage = New MemoryDocumentStorage()
Private report = New XtraReport1()
Private 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();
}
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Caching
' ...
Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim storage = New MemoryDocumentStorage()
Dim report = New XtraReport1()
Dim cachedReportSource = New CachedReportSource(report, storage)
Dim printTool = New ReportPrintTool(cachedReportSource)
' Invoke the Print dialog.
printTool.PrintDialog()
End Sub
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.
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();
Dim report As XtraReport1 = New XtraReport1()
report.CreateDocument()
report.PrintingSystem.Document.Name = "Application Name - Report Name"
report.PrintDialog()
To print a DevExpress report on a dot matrix printer, use the following workaround: