8

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

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 作为参数传递。例如,如果我选择了三个值的意思,那么水晶报表应该包含所有三个水晶报表对应的SelectedValue一一。如何将所有 PDF Crystal Report 合并为单个 Crystal Report。我必须解决这个问题。请帮我。

4

4 回答 4

8

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.

于 2011-07-04T10:41:42.543 回答
0

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

于 2011-07-01T15:49:07.697 回答
0

您可以使用 Atalasoft dotImage 执行此操作(免责声明 - 我为 Atalasoft 工作并编写了大多数 PDF 工具,包括这个工具)。事实上,它实际上是一个单行:

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

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

于 2011-07-01T15:54:04.860 回答
0

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

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

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

于 2011-07-01T15:36:47.137 回答