Merge Reports in Web Applications
- 4 minutes to read
This topic describes how to merge reports and display them in the Web Viewer. The topic discusses methods that work and warns about methods that can cause problems.
Use Subreports
In this technique, the main report includes the XRSubreport control, which creates a report document independent of the main report document. Document pages created for a subreport are combined with the main report’s document pages.
The technique requires that a report referenced by the XRSubreport control is available at the time the main report creates a document. In a distributed application, implement the IReportProvider service that translates the report ID (report URL) specified in the XRSubreport.ReportSourceUrl property to a report instance.
Review the following topics for examples of use:
Use the ModifyDocument Method to Add/Remove Document Pages
In this technique, a report document is created for each report. Then, the ModifyDocument method allows you to delete certain pages from the document page collection, or add pages from another document.
This method requires that all report documents are available when the Document Viewer loads a merged document.
In a reporting application, you can use the XtraReport.CreateDocument or CachedReportSourceWeb.CreateDocument method to create a document and obtain a collection of document pages. Both methods have advantages and disadvantages.
Review the following help topic for examples of use:: Merge Reports: Specify Page Sequence Manually.
Create a Document with XtraReport
The XtraReport.CreateDocument method creates a document in memory, and that can result in high memory consumption for large reports. Once the document is created, it can be saved and/or serialized in storage for later use.
If you do not save a document generated with XtraReport, you should call the XtraReport.ModifyDocument method after calling the XtraReport.CreateDocument methods for reports being merged.
The following code uses the XtraReport.ModifyDocument method to merge two reports:
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 report1 instance that has the merged document stored in memory.
Note
Do not use the CachedReportSourceWeb for documents created and merged with the XtraReport instances.
Create a Document with CachedReportSourceWeb
The CachedReportSourceWeb.CreateDocument method consumes less memory than the XtraReport.CreateDocument method because it serializes the document while the document is being built.
The CachedReportSourceWeb
needs a document storage before starting document generation. You can assign a DocumentStorage descendant when the CachedReportSourceWeb instance is created.
In a distributed application, use the Document Cache Storage. For more information, review the following help topics:
The following code uses CachedReportSourceWeb objects and MemoryDocumentStorage to merge two reports:
using System.Collections.Generic;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting.Caching;
using DevExpress.XtraReports.Web;
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller {
// ...
public IActionResult Viewer() {
// Create the first report and generate its document.
var storage1 = new MemoryDocumentStorage();
var report1 = new MainReport();
var cachedReportSource1 = new CachedReportSourceWeb(report1, storage1);
cachedReportSource1.CreateDocument();
// Create the second report and generate its document.
var storage2 = new MemoryDocumentStorage();
var report2 = new NextReport();
var cachedReportSource2 = new CachedReportSourceWeb(report2, storage2);
cachedReportSource2.CreateDocument();
// Replace the second page of the MainReport with the first page of the NextReport.
cachedReportSource1.ModifyDocument(x => {
x.RemovePageAt(1);
x.InsertPage(1, cachedReportSource2.PrintingSystem.Pages[0]);
});
// Use the cachedReportSource1 object to preview the first report.
return View(cachedReportSource1);
}
}
Conclusion
In web applications, subreports are the recommended way to merge reports. In a distributed application, use report resolution services such as IReportProvider to return a report instance by ID (or by report URL).
You can use CachedReportSourceWeb in a distributed application with the IDocumentStorageProvider service.
Use either XtraReport objects or CachedReportSourceWeb objects to create and modify the document. Do not add document pages from one type of source object to another.