Print Multiple Reports in a Batch
- 3 minutes to read
This example sends multiple reports to the printer. The Print dialog appears only once before the first report is printed. This dialog specifies print settings for all reports.
- Use the PrintTool.PrintDialog method for the first report to display the Print dialog and specify print settings.
- Handle the StartPrint event to capture these settings and apply them to each report before printing.
- Call the Print method to print subsequent reports with the same printer settings, without prompting the user again.
using System.Drawing.Printing;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// Stores the printer settings selected by the user in the print dialog.
private PrinterSettings prnSettings;
// Handles the button click event to start the batch printing process.
private void button1_Click(object sender, EventArgs e) {
XtraReport1 report1 = new XtraReport1();
XtraReport[] reports = new XtraReport[] { new XtraReport2(), new XtraReport3() };
// Creates a print tool for the first report and subscribe to the StartPrint event.
ReportPrintTool pt1 = new ReportPrintTool(report1);
pt1.PrintingSystem.StartPrint += new PrintDocumentEventHandler(PrintingSystem_StartPrint);
// Subscribes to the StartPrint event for each additional report.
foreach (XtraReport report in reports) {
ReportPrintTool pts = new ReportPrintTool(report);
pts.PrintingSystem.StartPrint += new PrintDocumentEventHandler(reportsStartPrintEventHandler);
}
// Shows the print dialog for the first report.
if(pt1.PrintDialog() == true) {
// If the user confirms, print all additional reports with the selected printer settings
foreach(XtraReport report in reports) {
ReportPrintTool pts = new ReportPrintTool(report);
pts.Print();
}
}
}
// Event handler to capture the printer settings from the print dialog.
void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e) {
prnSettings = e.PrintDocument.PrinterSettings;
}
// Event handler to apply the captured printer settings to each report before printing.
private void reportsStartPrintEventHandler(object sender, PrintDocumentEventArgs e) {
int pageCount = e.PrintDocument.PrinterSettings.ToPage;
e.PrintDocument.PrinterSettings = prnSettings;
// Ensures all pages are printed, even if reports have different page counts.
e.PrintDocument.PrinterSettings.ToPage = pageCount;
}
See Also