Skip to main content

Print Multiple Reports in a Batch

  • 2 minutes to read

This example illustrates how to print multiple reports in a single batch instead of sending one report to the printer at a time. This invokes the Print dialog only for the first report that is being printed (all the other reports are printed using the same settings).

Handle the PrintingSystemBase.StartPrint event of the ReportPrintTool‘s printing system and use the PrintTool.PrintDialog and PrintToolBase.Print methods to solve this task.

using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...

private PrinterSettings prnSettings;

private void button1_Click(object sender, EventArgs e) {
    XtraReport1 report1 = new XtraReport1();
    XtraReport[] reports = new XtraReport[] { new XtraReport2(), new XtraReport3() };

    ReportPrintTool pt1 = new ReportPrintTool(report1);
    pt1.PrintingSystem.StartPrint += new PrintDocumentEventHandler(PrintingSystem_StartPrint);

    foreach (XtraReport report in reports) {
        ReportPrintTool pts = new ReportPrintTool(report);
        pts.PrintingSystem.StartPrint += 
            new PrintDocumentEventHandler(reportsStartPrintEventHandler);
    }

    pt1.PrintDialog();
    foreach (XtraReport report in reports) {
        ReportPrintTool pts = new ReportPrintTool(report);
        pts.Print();
    }
}

void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e) {
    prnSettings = e.PrintDocument.PrinterSettings;
}

private void reportsStartPrintEventHandler(object sender, PrintDocumentEventArgs e) {
    int pageCount = e.PrintDocument.PrinterSettings.ToPage;
    e.PrintDocument.PrinterSettings = prnSettings;

    // The following line is required if the number of pages for each report varies, 
    // and you consistently need to print all pages.
    e.PrintDocument.PrinterSettings.ToPage = pageCount;
}
See Also