Merge two or more Crystal Reports to a Single PDF

thevan picture thevan · Feb 4, 2011 · Viewed 19.6k times · Source

I have a CheckBoxList. If I select two or more values then the CheckBoxList SelectedValues would be passed as a Parameter one by one, and I want to generate the Crystal Report for each SelectedValue in PDF format and I want to merge all the Crystal Report PDF format into a Single PDF format. How to achieve this? So far I have generated only a single Crystal Report in PDF format. In below, I have given the Code about How I had generated the Crystal Report in PDF Format.

CrystalDecisions.CrystalReports.Engine.ReportDocument rpt =
    new CrystalDecisions.CrystalReports.Engine.ReportDocument();

string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
string[] str = conn.Split(';');
string server = str[0].Substring(str[0].IndexOf(" = ") + 3);
string database = str[1].Substring(str[1].IndexOf(" = ") + 3);
string userid = str[2].Substring(str[2].IndexOf(" = ") + 3);
string password = "Welc0me";//str[3].Substring(str[3].IndexOf(" = ") + 3);

rpt.Load(Server.MapPath(RepPath));

for (int i = 0; i < rpt.DataSourceConnections.Count; i++)
{
   rpt.DataSourceConnections[i].SetConnection(server, database, userid, password);
}
// Here ReleaseID will be replaced by the CheckBoxList's SelectedValue
rpt.SetParameterValue(0, ReleaseID);  
rpt.SetParameterValue(1, false);                         
rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,
    HttpContext.Current.Response, true, "Docs Report");

But the above code is only to generate a single pdf report at a time. But I want to pass each CheckBoxList selectedvalue as a Parameter. For Example, If I Selected three values means, then the Crystal Report should contain all the three Crystal Report for the Corresponding SelectedValue one by one. How do I merge all the PDF Crystal Report into a Single Crystal Report. I have to solve this problem. Please help me.

Answer

Evren Kuzucuoglu picture Evren Kuzucuoglu · Jul 4, 2011

Crystal Reports are meant to generate one page per record. You just have to set a datasource with several lines of data, set that data source for your report and export it to pdf directly (already "merged").

First of all you have to create a datasource with your data. For instance, you can create a DataSet using the Visual Studio designer. More info here.

Then you have to set the data source for your Crystal Report. You can do it with the Crystal designer.

Then at runtime, you just have to populate the rpt with the data related to all selected checkboxes, and export it to pdf. Inside the .rpt file create a group on the specific column related to the checkboxes with the "page break after" option set on the group. This will generate one Pdf file containing all your records, 1 per page.

Here's a sample code:

            using (var reportDocument = new ReportDocument())
        {
            var datasource = new Datasource { EnforceConstraints = false };

            var adapter = new adoTableAdapter { Connection = ConfigurationManager.ConnectionStrings["ConnectionString"]) };
            adapter.Fill(datasource.ado);

            reportDocument.Load(RptPath);
            reportDocument.SetDataSource(datasource);

            PageMargins myMargins = reportDocument.PrintOptions.PageMargins;
            myMargins.topMargin = Settings.Default.DefaultTopMargin;
            myMargins.leftMargin = Settings.Default.DefaultLeftMargin;
            reportDocument.PrintOptions.ApplyPageMargins(myMargins);
            reportDocument.PrintOptions.PaperSize = PaperSize.PaperA5;
            reportDocument.PrintOptions.PaperOrientation = PaperOrientation.Landscape;

            reportDocument.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            reportDocument.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            reportDocument.ExportOptions.DestinationOptions = new DiskFileDestinationOptions { DiskFileName = PdfFilename };
            reportDocument.ExportOptions.FormatOptions = new PdfRtfWordFormatOptions();

            reportDocument.Export();
        }

In this code, dsEtiqueta is the ADO datasource, RptPath the path to the *.rpt report file and the pdf filename is generated using a timeSpan. It's what I needed in my project but feel free to adapt it to your needs.

Edit: done with CR for VS 2010.