Merge Reports: Specify Page Sequence Manually

  • 5 min to read

This topic describes how to generate two reports and merge them on the page-by-page basis.

Tip

When you merge reports in eXpressApp Framework applications, follow the instructions given in the How to: Merge the Pages of Two Reports topic.

using DevExpress.XtraReports.UI;
// ...
// Create the first report and generate its document.
XtraReport1 report1 = new XtraReport1();
report1.CreateDocument();

// Create the second report and generate its document.
XtraReport2 report2 = new XtraReport2();
report2.CreateDocument();

// Add all pages of the second report to the end of the first report.
report1.ModifyDocument(x => {
    x.AddPages(report2.Pages);
});

// Preview the modified report.

To add pages to a report when it is generated, handle the report's XRControl.AfterPrint event.

using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System;
using System.Drawing;
// ...
XtraReport CreateReport() {
    XtraReport1 report1 = new XtraReport1();
    report1.AfterPrint += Report1_AfterPrint;
    return report1;
}

void Report1_AfterPrint(object sender, EventArgs e) {
    XtraReport2 report2 = new XtraReport2();
    report2.CreateDocument();

    XtraReport report1 = (XtraReport)sender;
    report1.ModifyDocument(x => x.AddPages(report2.Pages));
}
Note

The XtraReport.ModifyDocument method was implemented in version 18.1.8.

Limitations

  • The Preview does not support document export in continuous (single file) mode. As a workaround, you can use either of the following:

    • Use subreports to display multiple reports in a single document.
    • Export individual documents to different files and then combine them into a single file.
  • Interactive features are not available (for instance, the drill-down functionality, sorting, report parameters input).

  • Page setup is not available in Preview mode.
  • The combined reports do not share bookmarks and tables of contents. These items remain specific to the original reports.
Tip

Do not destroy the source documents before you publish the resulting document (the resulting document does not create a "deep copy" of the source documents).

Merge Large Reports

When you display, print or export documents that contain thousands of pages, use the CachedReportSource/CachedReportSourceWeb components to avoid memory consumption-related issues. These component generate a report document and cache each generated page in the specified storage - memory, file or database.

The code sample below illustrates how to add a title page to a large report.

using DevExpress.XtraPrinting.Caching;
using DevExpress.XtraReports.UI;
// ...
// Create the first report and generate its document.
var storage1 = new MemoryDocumentStorage();
var report1 = new XtraReport1();
var cachedReportSource1 = new CachedReportSource(report1, storage1);
cachedReportSource1.CreateDocument();
// Create the second report and generate its document.
var storage2 = new MemoryDocumentStorage();
var report2 = new XtraReport2();
var cachedReportSource2 = new CachedReportSource(report2, storage2);
cachedReportSource2.CreateDocument();

// Add all pages of the second report to the end of the first report.
// Use either XtraReport.ModifyDocument() or CachedReportSource.ModifyDocument() to modify the report document.
report1.ModifyDocument(x => {
    x.InsertPage(1, cachedReportSource2.PrintingSystem.Pages[0]);
});

// Preview the first report using the cachedReportSource1 object.

The code sample below illustrates how to use the ModifyDocument(Action<IDocumentModifier>) method to append a report's pages to a large report.

using DevExpress.XtraPrinting.Caching;
using DevExpress.XtraReports.UI;
// ...
// Create the first report and generate its document.
var storage1 = new MemoryDocumentStorage();
var report1 = new XtraReport1();
var cachedReportSource1 = new CachedReportSource(report1, storage1);
cachedReportSource1.CreateDocument();
// Create the second report and generate its document.
var storage2 = new MemoryDocumentStorage();
var report2 = new XtraReport2();
var cachedReportSource2 = new CachedReportSource(report2, storage2);
cachedReportSource2.CreateDocument();

// Add all pages of the second report to the end of the first report.
// Use either XtraReport.ModifyDocument() or CachedReportSource.ModifyDocument() to modify the report document.
report1.ModifyDocument(x => {
    x.AddPages(cachedReportSource2.PrintingSystem.Pages);
});

// Preview the first report using the cachedReportSource1 object.
See Also