将两个或多个Crystal Reports合并为单个PDF

我有一个CheckBoxList。 如果我选择两个或多个值,则CheckBoxList SelectedValues将作为参数逐个传递,我想为PDF格式的每个SelectedValue生成Crystal Report,并且我想将所有Crystal Report PDF格式合并为单个PDF格式。 怎么做到这一点? 到目前为止,我只生成了PDF格式的单个Crystal报表。 在下面,我已经提供了有关如何以PDF格式生成Crystal报表的代码。

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"); 

但上面的代码只是一次生成一个pdf报告。 但我想将每个CheckBoxList selectedvalue作为参数传递。 例如,如果我选择了三个值,则Crystal Report应该逐个包含对应SelectedValue的所有三个Crystal Report。 如何将所有PDF Crystal Report合并到单个Crystal Report中。 我必须解决这个问题。 请帮我。

Crystal Reports 旨在为每条记录生成一个页面。 您只需设置包含多行数据的数据源,为报表设置该数据源并直接将其导出为pdf(已“合并”)。

首先,您必须使用数据创建数据源。 例如,您可以使用Visual Studio设计器创建DataSet。 更多信息在这里 。

然后,您必须为Crystal Report设置数据源。 您可以使用Crystal设计师来完成。

然后在运行时,您只需使用与所有选中复选框相关的数据填充rpt,然后将其导出为pdf。 在.rpt文件中,在与复选框相关的特定列上创建一个组,并在组上设置“分页后”选项。 这将生成一个包含所有记录的Pdf文件,每页1个。

这是一个示例代码:

  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(); } 

在此代码中,dsEtiqueta是ADO数据源,RptPath是* .rpt报告文件的路径,pdf文件名是使用timeSpan生成的。 这是我在项目中所需要的,但可以随意根据您的需求进行调整。

编辑:使用CR for VS 2010完成。

坏消息:.Net和Crystal都不支持合并PDF。

好消息:使用ExportToStream而不是ExportToHttpResponse将每个报告写入自己的内存流。 然后使用第三方库将两个PDF合并为一个。

以下是一些开源库: http : //csharp-source.net/open-source/pdf-libraries 。 还有许多商业图书馆,一个是DynamicPDF

水晶不能做到这一点。 但是,您可以将Crystal导出到内存流,然后您可以使用此站点来了解如何合并它们: http : //www.codeproject.com/KB/files/SimplePdfMerger.aspx

您可以使用Atalasoft dotImage(免责声明 – 我为Atalasoft工作并编写大部分PDF工具,包括此工具)。 事实上,它实际上是一个单行:

 PdfDocument.Combine(outputFile, intputFile1, intputFile2, ...); // the signature uses params string[] 

有一些适用于Streams或路径的风格,如果需要,您可以加密输出。