Merging the Pages of Different Documents
- 4 minutes to read
You can merge different documents’ pages to produce a single composite document in code.
This example demonstrates how to access and customize a collection of document pages (XtraReport.Pages).
Tip
Do not dispose of the merged documents before publishing the resulting document (the resulting document does not create a “deep copy” of merged documents).
The following code appends an entire document to the end of another document:
using DevExpress.XtraReports.UI;
// ...
private void CombineTwoReports() {
// Create the 1st report and generate its document.
XtraReport1 report1 = new XtraReport1();
report1.CreateDocument();
// Create the 2nd report and generate its document.
XtraReport2 report2 = new XtraReport2();
report2.CreateDocument();
// Add all pages of the 2nd report to the end of the 1st report.
report1.Pages.AddRange(report2.Pages);
// Reset all page numbers in the resulting document.
report1.PrintingSystem.ContinuousPageNumbering = true;
// Show the Print Preview form (in a WinForms application).
ReportPrintTool printTool = new ReportPrintTool(report1);
printTool.ShowPreviewDialog();
}
Tip
Composite report documents created from multiple merged documents do not support continuous (single file) export. An attempt to export a composite document to a single file only exports its first merged document.
As a workaround, you can use subreports to display multiple reports in a single document, or you can export individual documents to separate files and then manually combine them into a single file.
The following code combines two documents into one document:
using DevExpress.XtraReports.UI;
// ...
private void MergeTwoReports() {
// Create the 1st report and generate its document.
XtraReport1 report1 = new XtraReport1();
report1.CreateDocument();
// Create the 2nd report and generate its document.
XtraReport2 report2 = new XtraReport2();
report2.CreateDocument();
// Merge the pages of two reports.
int minPageCount = Math.Min(report1.Pages.Count, report2.Pages.Count);
for (int i = 0; i < minPageCount; i++) {
report1.Pages.Insert(i * 2 + 1, report2.Pages[i]);
}
if (report2.Pages.Count != minPageCount) {
for (int i = minPageCount; i < report2.Pages.Count; i++) {
report1.Pages.Add(report2.Pages[i]);
}
}
// Reset all page numbers in the resulting document.
report1.PrintingSystem.ContinuousPageNumbering = true;
// Show the Print Preview form (in a WinForms application).
ReportPrintTool printTool = new ReportPrintTool(report1);
printTool.ShowPreviewDialog();
}
The following code reorders document pages to produce a booklet (first page, last page, second page, last but one, third page, etc.):
using DevExpress.XtraReports.UI;
// ...
private void CreateBooklet() {
// Create the 1st report and generate its document.
XtraReport1 report1 = new XtraReport1();
report1.CreateDocument();
// Preserve the original page numbers on all pages.
report1.PrintingSystem.ContinuousPageNumbering = false;
// Create a booklet.
int centerPageIndex = Convert.ToInt32((report1.Pages.Count - 1) / 2);
for (int i = 0; i < centerPageIndex; i++) {
report1.Pages.Insert(i * 2 + 1, report1.Pages[report1.Pages.Count - 1]);
}
// Show the Print Preview form (in a WinForms application).
ReportPrintTool printTool = new ReportPrintTool(report1);
printTool.ShowPreviewDialog();
}
Tip
See Printing Reports to learn how to print your reports in applications created for different platforms.