水晶报表的多个报表生成一个pdf文件

用户提出点击一个打印按钮就可以生成多个报表,并且是生成为一个pdf文件。
开始实现的是点击打印生成N个pdf文件,每个Pdf文件弹出一次,用户感觉很复杂。
我查了很多的资料,也没有一个很好的解决方案。
开始我想要用流来实现合并多个pdf。
  Byte[] byteStreamOutput = objReport.ConvertReportToByteArray(objRPT);
(1)生成文件
          FileInfo objFileInfo = new FileInfo(@"d:/" + ArrayPDFName[i]);
          using (FileStream objFileStream = objFileInfo.OpenWrite())
          {
            objFileStream.Write(byteStreamOutput, 0, byteStreamOutput.Length);
            objFileStream.Close();
            objFileStream.Dispose();
          }
(2)在内存中存储
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.IO.BinaryWriter BWriter = new BinaryWriter(stream);
BWriter.Write(PDFstr, 0, PDFstr.Length);
stream.Seek(0, SeekOrigin.Current);
BWriter.Write(byteStreamOutput, 0, byteStreamOutput.Length);
stream.Close();
PDFstr = stream.GetBuffer();
stream.Dispose();
总是显示的只是最后的一个报表文件的pdf。
后来我发现文件的大小一个多个文件合并的大小,就是不能显示。
突然,我想应该是读取格式的问题。每个Pdf文件它有自己的独特的存储格式,读取的时候也是一样。它在显示时也是特殊的,它是首先从文件尾开始读,读到文件头就表示一个完整的pdf已经得到。我用流合并时只是简单的把流相加,没有处理掉文件的头和尾的问题。

昨天晚上,回家总是想着这个问题,打开电脑上网在查查有什么解决办法。发现了一个开源的项目iTextSharp。它可以很好的处理pdf文件。我又查了一些其他的文章,终于可以把问题解决了。
今天来到公司把我找到的类dll加载到我的项目中,写好类,我想要的结果终于可以了。
[解决方案]
1、下载iTextSharp.dll并且加到项目中
http://blog.rubypdf.com/itextsharp/tutorial01/index.html 网址中有例子
可以到csdn中下载。
2、写合并函数。
fileList :为要合并的pdf文件数组(要是绝对地址)
outMergeFile: 是最终合并成的文件的名字。
public static string MergeMultiplePDFsToPDF(string[] fileList, string outMergeFile)
    {
      string returnStr = "";
      try
      {
        int f = 0;
        // we create a reader for a certain document
        PdfReader reader = new PdfReader(fileList[f]);
        // we retrieve the total number of pages
        int n = reader.NumberOfPages; //There are " + n + " pages in the original file.
        // step 1: creation of a document-object
        Document document = new Document(reader.GetPageSizeWithRotation(1));
        // step 2: we create a writer that listens to the document
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
        // step 3: we open the document
        document.Open();
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage page;
        int rotation;
        // step 4: we add content
        while (f < fileList.Length)
        {
          int i = 0;
          while (i < n)
          {
            i++;
            document.SetPageSize(reader.GetPageSizeWithRotation(i));
            document.NewPage();
            page = writer.GetImportedPage(reader, i);
            rotation = reader.GetPageRotation(i);
            if (rotation == 90 || rotation == 270)
            {
              cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
            }
            else
            {
              cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
            }
            //Processed page i
          }
          f++;
          if (f < fileList.Length)
          {
            reader = new PdfReader(fileList[f]);
            // we retrieve the total number of pages
            n = reader.NumberOfPages; //There are " + n + " pages in the original file.
          }
        }
        // step 5: we close the document
        document.Close();
        returnStr = "Succeed!";
      }
      catch (Exception e)
      {
        returnStr += e.Message + "<br />";
        returnStr += e.StackTrace;
      }
      return returnStr;
    }
3、调用函数
string str = MergeMultiplePDFsToPDF(ArrayPDFName, SaveReportsFile)
这样所要的功能就实现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值