This topic describes how to generate two reports and merge them on the page-by-page basis.
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.
Imports DevExpress.XtraReports.UI
' ...
' Create the first report and generate its document.
Private report1 As New XtraReport1()
report1.CreateDocument()
' Create the second report and generate its document.
Dim report2 As New XtraReport2()
report2.CreateDocument()
' Add all pages of the second report to the end of the first report.
report1.ModifyDocument(Sub(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));
}
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports System
Imports System.Drawing
' ...
Private Function CreateReport() As XtraReport
Dim report1 As New XtraReport1()
AddHandler report1.AfterPrint, AddressOf Report1_AfterPrint
Return report1
End Function
Private Sub Report1_AfterPrint(ByVal sender As Object, ByVal e As EventArgs)
Dim report2 As New XtraReport2()
report2.CreateDocument()
Dim report1 As XtraReport = DirectCast(sender, XtraReport)
report1.ModifyDocument(Function(x) x.AddPages(report2.Pages))
End Sub
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.
Imports DevExpress.XtraPrinting.Caching
Imports DevExpress.XtraReports.UI
' ...
' Create the first report and generate its document.
Private storage1 = New MemoryDocumentStorage()
Private report1 = New XtraReport1()
Private cachedReportSource1 = New CachedReportSource(report1, storage1)
cachedReportSource1.CreateDocument()
' Create the second report and generate its document.
Dim storage2 = New MemoryDocumentStorage()
Dim report2 = New XtraReport2()
Dim 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(Sub(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.
Imports DevExpress.XtraPrinting.Caching
Imports DevExpress.XtraReports.UI
' ...
' Create the first report and generate its document.
Private storage1 = New MemoryDocumentStorage()
Private report1 = New XtraReport1()
Private cachedReportSource1 = New CachedReportSource(report1, storage1)
cachedReportSource1.CreateDocument()
' Create the second report and generate its document.
Dim storage2 = New MemoryDocumentStorage()
Dim report2 = New XtraReport2()
Dim 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(Sub(x) x.AddPages(cachedReportSource2.PrintingSystem.Pages))
' Preview the first report using the cachedReportSource1 object.
See Also