Merge Multiple C1Reports in WPF and WinForms

C1Report offers the flexibility to create and save multiple reports in just one Report Definition File (.xml). However, only one report can be previewed at a time. Now there have been scenarios where customers wanted to merge these multiple reports (either from same or different report definition file(s)) and preview them as one document. Since, there isn't any direct way to achieve the same, I write this blog to share a workaround for it. This is a simple approach which requires to load all the reports that are to be merged. And then each page of the loaded reports is saved as an image in an array. These images are then retrieved one by one and added to the C1PrintDocument as RenderImage objects. Once all the images are added, the C1PrintDocument is displayed in the Preview control (Winforms/WPF). The code implementation for the same is :

private void Form1_Load(object sender, EventArgs e)  
      // load the reports  
      c1Report1.Load("Report1.xml", "ReportName");  
      c1Report2.Load("Report2.xml", "ReportName");  

      // the margins are set to zero to compensate for the margins already present in the reports  
      c1PrintDocument1.PageLayout.PageSettings.LeftMargin = "0.0in";  
      c1PrintDocument1.PageLayout.PageSettings.TopMargin = "0.0in";  
      int i;  
      int count=c1Report1.GetPageCount() + c1Report2.GetPageCount();  
      Pages = new System.Drawing.Imaging.Metafile[count];  

      // get pageimages of report1  
      foreach (System.Drawing.Imaging.Metafile Page in this.c1Report1.GetPageImages())  
           Pages.SetValue(Page, index);  

      // get pageimages of report2  
      foreach (System.Drawing.Imaging.Metafile Page in this.c1Report2.GetPageImages())  
           Pages.SetValue(Page, index);  

      // add images from array to PrintDocument as RenderImages  
      for (i = 0; i <= (Pages.Length - 1); i++)  
           RenderImage PageImage = new RenderImage(Pages[ i ]);  

      // Preview the C1PrintDocument  

      // for winform Application  
      c1PrintPreviewControl1.Document = c1PrintDocument1;  
      // for WPF Application  
      c1DocumentViewer1.Document = c1PrintDocument1.FixedDocumentSequence;  

You may refer to the following samples : Download Winform Sample_C# Download WPF Sample_VB Note : Since, the images are retrieved page by page for each of the available reports, there could be certain delay or performance issues with large documents.


GrapeCity Developer Tools
comments powered by Disqus