水晶报表实现套打
对于套打的情况,一般出现在以下两种情况:
1:清单类打印,一般可能是已经是在已经印刷好的纸张上打印
2:单据发票类,一般单据格式固定,如每张打印5行数据,而且这种格式是已经印刷好的,当然机打发票除外。
我们以第2种情况为例进行说明,因为第2种会了第1种也自然就明白了
在这里我们使用程序代码+报表中设置背景图片的方式来实现。
1、纸张尺寸类型很难控制客户端的,那就用A4纸,计算设置较大右边距和下边距,以适合你的单据尺寸;
2、页面设置,精确计算左边距与上边距,放好你需打印的组件与不需打印的组件(Top与Left);
3、窗体上,三个控件:CrystalReport控件、CrystalReportViewer控件和PrintDialog控件,让CrystalReportViewer控件的ShowPrintButton=false,以免客户从它启动打印;
4、在制作报表的时候,把你不需要打印的部分使用图片代替。
如下图。
使用了图片来代替固定数据。如上图中的“仓储企业名称”等标题。
5、报表打印类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Easipay.Controls.Printer; using System.Drawing.Printing; namespace Warehouse.Report { public class ReportAllocateBLM : ReportBase, IPrintReport { // <summary> /// 生成报表对象 /// </summary> /// <returns></returns> public object Report() { return new rptAllocateBLMTHD(); } /// <summary> /// 打印预览, 设置报表数据的方法: /// </summary> /// <param name="rpt">报表</param> /// <param name="reportData">需要打印的数据</param> /// <param name="boolBackgroupPic">是否打印背景图片</param> public void PrePrint(ref CrystalDecisions.CrystalReports.Engine.ReportDocument rpt, object reportData, bool boolBackgroupPic) { ProtionOutHeadReport info = GetDataSet(reportData); AddBarNo(ref rpt); rpt.SetDataSource(info); System.Drawing.Printing.PrintDocument pd = new PrintDocument(); System.Drawing.Printing.PaperSize paperSize = new System.Drawing.Printing.PaperSize("ProtionInHeadReport", 827, 1200); } ///<summary> ///生成报表所需要的DataSet ///</summary> ///<param name="reportData">由界面中传过来的数据</param> ///<returns></returns> private ProtionOutHeadReport GetDataSet(object reportData) { ProtionOutHeadReport outBillDs = new ProtionOutHeadReport(); Easipay.WGQ.Entitys.ProtionOutHead head = reportData as Easipay.WGQ.Entitys.ProtionOutHead; if (head != null) { FillDataTable<Easipay.WGQ.Entitys.ProtionOutHead>(GetHeadList(head), outBillDs.ProtionOutHead); } if (head.ProtionOutODetail != null) { FillDataTable<Easipay.WGQ.Entitys.ProtionOutODetail>(GetDetailList(head), outBillDs.ProtionOutODetail); } if (head.ProtionOutMDetail != null) { FillDataTable<Easipay.WGQ.Entitys.ProtionOutMDetail>(GetMDetailList(head), outBillDs.ProtionOutMDetail); } return outBillDs; } /// <summary> /// 获取报表头 /// </summary> /// <param name="head">进区凭单表头信息</param> /// <returns></returns> private List<Easipay.WGQ.Entitys.ProtionOutHead> GetHeadList(Easipay.WGQ.Entitys.ProtionOutHead head) { List<Easipay.WGQ.Entitys.ProtionOutHead> list = new List<Easipay.WGQ.Entitys.ProtionOutHead>(); list.Add(head); return list; } /// <summary> /// 获取报表体 /// </summary> /// <param name="head">进区凭单带表体的表头信息类</param> /// <returns></returns> private List<Easipay.WGQ.Entitys.ProtionOutODetail> GetDetailList(Easipay.WGQ.Entitys.ProtionOutHead head) { List<Easipay.WGQ.Entitys.ProtionOutODetail> list = new List<Easipay.WGQ.Entitys.ProtionOutODetail>(); foreach (Easipay.WGQ.Entitys.ProtionOutODetail item in head.ProtionOutODetail) { list.Add(item); } return list; } /// <summary> /// 获取报表体 /// </summary> /// <param name="head">进区凭单带表体的表头信息类</param> /// <returns></returns> private List<Easipay.WGQ.Entitys.ProtionOutMDetail> GetMDetailList(Easipay.WGQ.Entitys.ProtionOutHead head) { string pid = string.Empty; List<Easipay.WGQ.Entitys.ProtionOutMDetail> list = new List<Easipay.WGQ.Entitys.ProtionOutMDetail>(); foreach (Easipay.WGQ.Entitys.ProtionOutMDetail item in head.ProtionOutMDetail) { pid = item.pid; if (pid=="2") { list.Add(item); } } //每页要打印八条记录,如果不足8条,则补空行 while (list.Count < 8) { Easipay.WGQ.Entitys.ProtionOutMDetail detail = new Easipay.WGQ.Entitys.ProtionOutMDetail(); detail.pid = pid; System.Threading.Thread.Sleep(1); detail.id = (DateTime.UtcNow.Ticks % 100000000000).ToString(); list.Add(detail); } return list; } string m_PrintTypeBarName; /// <summary> /// 报表打印类型 /// </summary> public string PrintTypeBarName { get { if (string.IsNullOrEmpty(m_PrintTypeBarName)) { return "分拨货物提货单"; } return m_PrintTypeBarName; } set { m_PrintTypeBarName = value; } } /// <summary> /// 按需要隐藏报表上的某些控件,这里隐藏的都是图片控件,其他的可以自行添加: /// </summary> public void HidePicture(ref CrystalDecisions.CrystalReports.Engine.ReportDocument rpt) { try { foreach (CrystalDecisions.CrystalReports.Engine.Section section in rpt.ReportDefinition.Sections) { foreach (CrystalDecisions.CrystalReports.Engine.ReportObject rptObject in section.ReportObjects) { if (rptObject is CrystalDecisions.CrystalReports.Engine.PictureObject) { //图片背景 (rptObject as CrystalDecisions.CrystalReports.Engine.PictureObject).ObjectFormat.EnableSuppress = true; } } } } catch { } } } } 6、打印方法: /// <summary> /// 打印报表 /// </summary> /// <param name="strPrintName">打印机名称</param> /// <param name="shInt">打印份数</param> /// <returns>true成功,false失败</returns> private bool blnPrint(string strPrintName,string shInt) { bool blnPrintScuss=false; int intCopies=0; try { intCopies=int.Parse(shInt); } catch { intCopies=1; } try { if (ipr == null) { MessageBox.Show("打印报表出错"); blnPrintScuss = false; return blnPrintScuss; } // m_BillNewRpt.PrintOptions.PaperSize=CrystalDecisions.Shared.PaperSize.PaperA4; ipr.HidePicture(ref m_BillNewRpt); m_BillNewRpt.PrintOptions.PrinterName = strPrintName; m_BillNewRpt.PrintToPrinter(intCopies,false,1,m_intMaxPage); blnPrintScuss=true; } catch (Exception ex) { blnPrintScuss=false; MessageBox.Show("打印报表出错:"+ex.Message); } return blnPrintScuss; }
7、套打,只打印数据的效果。如下图。
8、非套打,全部打印的效果。如下图。