水晶报表 一次打印多张,利用pdf导出

项目需求,一组数据根据类型区分打印多张报表,经过研究利用pdf导出实现。

利用插件  iTextSharp.dll.

核心代码如下

//报表名            
private string strReportFile = " CalibrationFlowOrderRpt.rpt";

            List listStream = new List();
            List test = (List)Session["TestType"];//分组
            arry_Id = (string[])Session["PrinterArrId"];
            for (int i = 0; i < arry_Id.Length; i++)
            {
                strIds += "'" + arry_Id[i] + "'" + ",";
            }
            //全部数据 id 主键,代码的写法很low,不要在意这些
            strIds = strIds.Trim(new char[] { ',' });
            //报表对象 初始化
            rpt = new ReportDocument();           
            rpt.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;
            rpt.Load(Server.MapPath(strReportFile));
          
            List meters = dispMana.GetModelListWithBarcode(strIds);
            for (int i = 0; i < test.Count; i++)
            {
               //数据源 这里使用的是 .xsd格式数据
                CalibrationOrderForSHData data = new CalibrationOrderForSHData();
                List caliTypes = objCaliCateGoryHelper.GetCateGorySubItems(test[i]);
                List selectMeters = meters.FindAll(p => caliTypes.Contains(p.MeterTestType)).ToList();
                //数据填入data数据源中
                SetData(data, selectMeters, test.Count, i + 1);
               //为报表设置数据源
                rpt.SetDataSource(data);
//将报表导出为pdf格式
                Stream sr = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                listStream.Add(sr);
                data.Dispose();
            }
//循环时,即是将不同组数据导入不同的报表,转为stream流最后一次性导出
            rpt.Close();
            rpt.Dispose();
            string filename ="流转单"+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
            mergePDFFiles(listStream, filename);


//导出pdf方法
  private void mergePDFFiles(List listStream, string fileName)
        {
            string pathReportOutputUrl = @"../../FileLoad/";
            iTextSharp.text.Document document = new iTextSharp.text.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(PageSize.A4.Rotate());//A4纸横向打印
            // document.SetPageSize(PageSize.A4);//A4纵向打印
            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();
        }

 

你可能感兴趣的:(学习)