善待自己,珍惜今天,恩泽他人,享受生活

不放弃任何解决困难的机会,人的一生就是解决困难的过程。 当我们走完一生才能说没有问题要解决了。 面对工作、生活上的压力,面对来自家庭、朋友、同事、上司等的困惑。 要排除万难,否则我们就会被万难排除!

博客园 首页 新随笔 联系 订阅 管理

#region 报表打印
       private void PrintReorpt(string OrderNumber,string ReportPath)
       {
           //初始化报表文件数据源
           CrystalDecisions.Web.CrystalReportSource reportSource = new CrystalDecisions.Web.CrystalReportSource();
           //指定报表来源
           reportSource.Report.FileName = this.MapPath(ReportPath);
           //初始化数据来源
           DataTable dt = new DataTable();
           try
           {
               dt = _head.GetDT_Doc(OrderNumber, Page.User.Identity.Name);
           }
           catch (Exception ex)
           {
               EA.Common.WebMessageBox.Show(this.Page, "单据打印数据异常.\\r\\n异常原因:" + EA.Common.UIHelper.JsEncode(ex.Message));
               return;
           }
           if (dt == null || dt.Rows.Count == 0)
           {
               EA.Common.WebMessageBox.Show(this.Page, "该用户未被授权查看该单据或单据不存在或已删除!");
               return;
           }
           reportSource.ReportDocument.SetDataSource(dt);
           Stream sr = reportSource.ReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
           Stream sw = Response.OutputStream;
           Response.Clear();
           Response.ContentType = "application/octet-stream";
           string filename = OrderNumber + ".pdf";
           Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
           byte[] buf = new byte[256];
           int cnt = sr.Read(buf, 0, 256);
           while (cnt > 0)
           {
               sw.Write(buf, 0, cnt);
               Response.Flush();
               cnt = sr.Read(buf, 0, 256);
           }

           //以下代码是必须的,除了释放内存外,另一个作用是当将再次调用该方法时,容易造成上一次的内容没被清楚,而导致第二次查看的PDF和第一次是一样的。
           Response.Close();
           reportSource.ReportDocument.Close();
           reportSource.Dispose();
           Response.End();
       }
       #endregion

 

多个报表合并为1个PDF文件的方法:

#region 报表打印

       private void PrintReorpt(List<System.Data.DataTable> listTable, string ReportPath)
       {
           List<Stream> listStream = new List<Stream>();
           CrystalDecisions.Web.CrystalReportSource reportSource=null;
           foreach (System.Data.DataTable dt in listTable)
           {
               if (dt != null && dt.Rows.Count > 0)
               {
                   //初始化报表文件数据源
                   reportSource = new CrystalDecisions.Web.CrystalReportSource();
                   //指定报表来源
                   reportSource.Report.FileName = this.MapPath(ReportPath);
                   reportSource.ReportDocument.SetDataSource(dt);
                   Stream sr = reportSource.ReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                   listStream.Add(sr);
                   reportSource.ReportDocument.Close();
                   reportSource.Dispose();
               }
           }
           string fileName = DateTime.Now.ToString("yyyyMMddHHmmss_") + Guid.NewGuid().ToString() + ".pdf";
           mergePDFFiles(listStream, fileName);
       }


       /// <summary> 合併PDF檔(集合) </summary>  
       /// <param name="fileList">欲合併PDF檔之集合(一筆以上)</param> 
       /// <param name="outMergeFile">合併後的檔名</param>  

//我们需要引入:

using iTextSharp.text;
using iTextSharp.text.pdf;

//document.NewPage(); 这个是必须的,否则PDF没办法自动的分页。


       private void mergePDFFiles(List<Stream> listStream, string fileName)
       {
           Document document = new Document();
           int i = 0;
           PdfReader reader;
           string filePath = this.MapPath(pathReportOutputUrl + fileName);
           PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
           PdfImportedPage newPage;
           document.Open();
           PdfContentByte cb = writer.DirectContent;
           Rectangle re = new Rectangle(595, 850);
           document.SetPageSize(re);
           foreach (Stream sr in listStream)
           {
               if (i != 0)
               {
                   document.NewPage();
               }
               reader = new PdfReader(sr);

               int iPageNum = reader.NumberOfPages;
               for (int j = 1; j <= iPageNum; j++)
               {
                   document.NewPage();
                   newPage = writer.GetImportedPage(reader, j);
                   cb.AddTemplate(newPage, 0, 10);
               }
               i++;
           }
           document.Close();
           Stream sr1 = new FileStream(filePath, FileMode.Open);

           Stream sw = Response.OutputStream;
           Response.Clear();
           Response.ContentType = "application/octet-stream";
           Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
           byte[] buf = new byte[256];
           int cnt = sr1.Read(buf, 0, 256);
           while (cnt > 0)
           {
               sw.Write(buf, 0, cnt);
               Response.Flush();
               cnt = sr1.Read(buf, 0, 256);
           }
           sr1.Close();
           try
           {
               File.Delete(filePath);
           }
           catch { }
           Response.End();
       }

       #endregion

posted on 2012-07-19 22:32  笨笨丁  阅读(1735)  评论(1编辑  收藏  举报