Merge two or more Crystal Reports to a Single PDF

白昼怎懂夜的黑 提交于 2019-11-30 13:21:50
Evren Kuzucuoglu

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.

Bad news: Neither .Net nor Crystal has support to merge PDFs.

Good News: Use ExportToStream instead ExportToHttpResponse to write each report to its own memory stream. Then use a 3rd party library to merge the two PDFs into a single one.

Here are some open source libraries: http://csharp-source.net/open-source/pdf-libraries. There are many commercial libraries available too, one is DynamicPDF

Crystal cannot do this. You can however make Crystal export to a memory stream, then you can use this site to learn how to merge them: http://www.codeproject.com/KB/files/SimplePdfMerger.aspx

You can do this with Atalasoft dotImage (disclaimer - I work for Atalasoft and wrote most of the PDF tools, including this one). In fact, it literally is a one-liner:

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

There are flavors that work with Streams or paths, and you can encrypt the output if you need to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!