Search code examples
devexpressxtrareportdevexpress-mvc

Can't merge two XtraReport Pages in Third XtraReport


I have odd even pages for each record that I have to prepare report. when I run the MVC application i can see the XtraReport Viewer but there are no pages in it.

Controller/Action Logic

var xtraReport1 = XtraReport.FromStream(GenerateStreamFromString(layout1), true);
var xtraReport2 = XtraReport.FromStream(GenerateStreamFromString(layout2), true);

var finalExtraReport = new XtraReport();

foreach(var row in dataTable.Rows)
{
    xtraReport1 = XtraReport.FromStream(GenerateStreamFromString(layout1), true);
    xtraReport1.DataSource = dataTable.Select("id = '"+ row[id] +"'")CopyToDataTable();
    xtraReport1.CreateDocument();
    finalExtraReport.Pages.AddRange(xtraReport1.Pages);

    xtraReport2 = XtraReport.FromStream(GenerateStreamFromString(layout2), true);   
    xtraReport2.DataSource = dataTable.Select("id = '"+ row[id] +"'")CopyToDataTable();
    xtraReport2.CreateDocument();
    finalExtraReport.Pages.AddRange(xtraReport2.Pages);
}

return View(finalExtraReport);

View

@model XtraReport
@{
    ViewBag.Title = "Home Page";
}

@Html.DevExpress().WebDocumentViewer(settings =>
{
    settings.Name = "webDocumentViewer";
}).Bind(Model).GetHtml()

Solution

  • Found the solution.

    This behavior is caused by the fact that the WebDocumentViewer extension recreates the report document to preview it. So, the report pages added to your report dynamically will be cleared.

    Currently, when using the HTML5 Document Viewer control to preview a report, it is necessary to call the XtraReport.AfterPrint event handler to merge report pages. So, handle your "r2" report's AfterPrint event and use this event handler to add the "r1" report's pages to the "r2" report.

    public ActionResult ShowReport()
    {
         //...
         XtraReport mainReport = //get a report;
         mainReport.AfterPrint += Report_AfterPrint;
         //...
    }
    void Report_AfterPrint(object sender, EventArgs e) {
         //merge reports
        XtraReport sourceReport = sender as XtraReport;
    
        XtraReport additionalReport = new XtraReport1();
        additionalReport.CreateDocument();
    
        sourceReport.Pages.AddRange(additionalReport.Pages);
    }
    

    Note : starting with version 15.2.5 WebDocumentViewer supports displaying merged reports out of the box.