ActiveReports 13
Merge Multiple Reports
ActiveReports 13 > ActiveReports User Guide > How To > Page Report/RDL Report How To > Merge Multiple Reports

You can merge or combine multiple Page and RDL reports into one report. Merging is performed by using ReportCombiner class which adds the reports as subreports. The reports are merged one after the another, in the order in which they are added. Having access to BuildReport method, user can use full PageReport class functionality. You can add a page break or specify the gap between two reports. By default, the gap of 1 inch is added between the reports.

The following code illustrates how to merge three reports. You need to add GrapeCity.ActiveReports.Core.Rendering assembly to your project. The code also exports the merged report in PDF format, so you need to add GrapeCity.ActiveReports.Export.Pdf assembly.

To write the code in Visual Basic.NET

Visual Basic.NET
Copy Code
Viewer1.LoadDocument(Application.StartupPath & "\..\..\PageReport1.rdlx")
Dim combiner = New GrapeCity.ActiveReports.ReportsCore.Tools.ReportCombiner()

Dim r1 = New GrapeCity.ActiveReports.PageReport()
r1.Load(New System.IO.FileInfo("c:\temp\Report1.rdlx"))

Dim r2 = New GrapeCity.ActiveReports.PageReport()
r2.Load(New System.IO.FileInfo("c:\temp\Report2.rdlx"))

Dim r3 = New GrapeCity.ActiveReports.PageReport()
r3.Load(New System.IO.FileInfo("c:\temp\Report3.rdlx"))

combiner.AddReport(r1)

Dim options = New GrapeCity.ActiveReports.ReportsCore.Tools.LocationOptions
options.Gap = "5in" 'adds a 5 inch gap from the first report. By default this gap is 1 inch.
options.PageBreakBefore = True 'adds a page break.

combiner.AddReport(r2, options)
combiner.AddReport(r3)

'PDF Rendering extension
Dim pdfRe = New GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension()
Dim provider = New GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(New System.IO.DirectoryInfo("c:\temp\"), "CombinedReport")

combiner.BuildReport().Document.Render(pdfRe, provider)

To write the code in C#

C# code
Copy Code
viewer1.LoadDocument(Application.StartupPath + @"\..\..\PageReport1.rdlx");
var combiner = new GrapeCity.ActiveReports.ReportsCore.Tools.ReportCombiner();

var r1 = new GrapeCity.ActiveReports.PageReport();
r1.Load(new System.IO.FileInfo(@"c:\temp\Report1.rdlx"));

var r2 = new GrapeCity.ActiveReports.PageReport();
r2.Load(new System.IO.FileInfo(@"c:\temp\Report2.rdlx"));

var r3 = new GrapeCity.ActiveReports.PageReport();
r3.Load(new System.IO.FileInfo(@"c:\temp\Report3.rdlx"));

combiner.AddReport(r1);
combiner.AddReport(r2, new LocationOptions() { PageBreakBefore = true, Gap = "5in" }); //adds second report after a page break and a 5 inch gap from the first report. By default this gap is 1 inch.
combiner.AddReport(r3);    

//PDF Rendering extension
var pdfRe = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
var provider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(new System.IO.DirectoryInfo(@"c:\temp\"), "CombinedReport");

combiner.BuildReport().Document.Render(pdfRe, provider);

The merged reports can be modified in many ways as described below.

To add reports at particular index

If you want to add a report r4 after the first report r1, use the following code with index '1':

C#
Copy Code
combiner.Insert(1, r4, new LocationOptions());
report = combiner.BuildReport();
VB.NET
Copy Code
combiner.Insert(1, r4, New GrapeCity.ActiveReports.ReportsCore.Tools.LocationOptions())
report = combiner.BuildReport()

To add a list of reports

C#
Copy Code
combiner.AddRange(new PageReport[] {r1, r2, r3, r4 }, new LocationOptions())
report = combiner.BuildReport();
VB.NET
Copy Code
Dim reports As IEnumerable(Of GrapeCity.ActiveReports.PageReport) = {r1, r2, r3, r4}
combiner.AddRange(reports, New GrapeCity.ActiveReports.ReportsCore.Tools.LocationOptions())
report = combiner.BuildReport()

To delete report(s)

If you want to delete a report in first position, use the following code with index '1':

C#
Copy Code
combiner.RemoveAt(1);
report = combiner.BuildReport();
VB.NET
Copy Code
combiner.RemoveAt(1)
report = combiner.BuildReport()

If you want to delete all instances of report r2, use the following code:

C#
Copy Code
combiner.RemoveAll(r2);
report = combiner.BuildReport();
VB.NET
Copy Code
combiner.RemoveAll(r2)
report = combiner.BuildReport()