如何在一个PDF中组合多个Crystal报告

本文关键字:组合 报告 Crystal PDF 一个 | 更新日期: 2023-09-27 18:14:45

我需要生成5个Crystal报告并将它们合并到一个PDF文件中。最初我想用这5个报告创建一个报告作为子报告,然后将其输出为PDF。但是,这不起作用,因为这些报告中的每一个都已经有子报告,并且不支持子报告中的报告。

我想我需要在幕后将每个报告输出为PDF,然后使用像iTextSharp这样的东西来组合这些报告。我找到了一些例子,但没有一个真正适用于我的情况。我将感谢任何代码示例的这项任务。

谢谢。

如何在一个PDF中组合多个Crystal报告

如果您要手动将它们合并为一个PDF,我会使用PDFSam免费版本。

但是,如果你想用Crystal Reports来做这件事,为什么不使用子报表,并以那种方式将它们组合成一个呢?

https://youtu.be/xxwWFAtGSIM?t = 1 m06s

您可以编写自己的应用程序将这两个报告导出为PDF,然后使用一个开源项目将它们合并为一个PDF。例如:https://sourceforge.net/projects/pdfmerge/

您也可以得到一个现有的应用程序,可以为您做这些。这个是免费的http://www.r-tag.com/Pages/CommunityEdition.aspx

工作示例[c#,使用vs 2015 cm]-[根据您的观点.....]

1) crystalreports添加到名为"CrystalReport1"的示例项目中。crystalreport报道。[简单的设计]

2)所需文件:(合并流)itext lib: https://www.nuget.org/packages/iTextSharp(命令::Install-Package iTextSharp -Version 5.5.11)

3)需要的命名空间

using System;
using System.IO;
using System.Collections.Generic;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using iTextSharp.text;
using iTextSharp.text.pdf;
Step  1:  Reference code 
        //reploace this event to your related event .......
        protected void Page_Load(object sender, EventArgs e)
        {
            //hold the more than one report outputs [bytes]
            List<byte[]> files = new List<byte[]>();
            //for testing purpose LOOP used,
            //u can change as per your requirement and your report name
            for (int i = 1; i <= 2; i++) {
                ReportDocument crdReport1 = new ReportDocument();
                //put your related report names.....
                crdReport1.Load(Server.MapPath(string.Format("CrystalReport{0}.rpt", i)));
                Stream stream1 = crdReport1.ExportToStream(ExportFormatType.PortableDocFormat);
                //prepare the "bytes" from "stream"               
                files.Add(PrepareBytes(stream1));
                //finally the result added to LIST
            }
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";     
            //merge the all reports & show the reports            
            Response.BinaryWrite(MergeReports(files).ToArray());
            Response.End();        
        }

步骤2:参考代码(credit:: https://stackoverflow.com/a/221941)

//准备报告字节

 private byte[] PrepareBytes(Stream stream)
    {          
        using (MemoryStream ms = new MemoryStream())
        {
            byte[] buffer = new byte[stream.Length];
            int read;
            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
           return ms.ToArray();
        }           
    }

步骤3:参考代码(credit:: https://stackoverflow.com/a/6780582)

//合并报表

private MemoryStream MergeReports(List<byte[]> files)
    {
        if (files.Count > 1)
        {
            PdfReader pdfFile;
            Document doc;
            PdfWriter pCopy;
            MemoryStream msOutput = new MemoryStream();
            pdfFile = new PdfReader(files[0]);
            doc = new Document();
            pCopy = new PdfSmartCopy(doc, msOutput);
            doc.Open();
            for (int k = 0; k < files.Count; k++)
            {
                pdfFile = new PdfReader(files[k]);
                for (int i = 1; i < pdfFile.NumberOfPages + 1; i++)
                {
                    ((PdfSmartCopy)pCopy).AddPage(pCopy.GetImportedPage(pdfFile, i));
                }
                pCopy.FreeReader(pdfFile);
            }
            pdfFile.Close();
            pCopy.Close();
            doc.Close();
            return msOutput;
        }
        else if (files.Count == 1)
        {
            return new MemoryStream(files[0]);
        }
        return null;
    }

因此,您可以在步骤1中更改逻辑(根据您的报告)