对于套打的情况,一般出现在以下两种情况:
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 { ///// 生成报表对象 /// ///public object Report() { return new rptAllocateBLMTHD(); } /// /// 打印预览, 设置报表数据的方法: /// /// 报表 /// 需要打印的数据 /// 是否打印背景图片 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); } //////生成报表所需要的DataSet /// ///由界面中传过来的数据 ///private ProtionOutHeadReport GetDataSet(object reportData) { ProtionOutHeadReport outBillDs = new ProtionOutHeadReport(); Easipay.WGQ.Entitys.ProtionOutHead head = reportData as Easipay.WGQ.Entitys.ProtionOutHead; if (head != null) { FillDataTable (GetHeadList(head), outBillDs.ProtionOutHead); } if (head.ProtionOutODetail != null) { FillDataTable (GetDetailList(head), outBillDs.ProtionOutODetail); } if (head.ProtionOutMDetail != null) { FillDataTable (GetMDetailList(head), outBillDs.ProtionOutMDetail); } return outBillDs; } /// /// 获取报表头 /// /// 进区凭单表头信息 ///private List GetHeadList(Easipay.WGQ.Entitys.ProtionOutHead head) { List list = new List (); list.Add(head); return list; } /// /// 获取报表体 /// /// 进区凭单带表体的表头信息类 ///private List GetDetailList(Easipay.WGQ.Entitys.ProtionOutHead head) { List list = new List (); foreach (Easipay.WGQ.Entitys.ProtionOutODetail item in head.ProtionOutODetail) { list.Add(item); } return list; } /// /// 获取报表体 /// /// 进区凭单带表体的表头信息类 ///private List GetMDetailList(Easipay.WGQ.Entitys.ProtionOutHead head) { string pid = string.Empty; List list = new List (); 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; /// /// 报表打印类型 /// public string PrintTypeBarName { get { if (string.IsNullOrEmpty(m_PrintTypeBarName)) { return "分拨货物提货单"; } return m_PrintTypeBarName; } set { m_PrintTypeBarName = value; } } ////// 按需要隐藏报表上的某些控件,这里隐藏的都是图片控件,其他的可以自行添加: /// 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、打印方法:////// 打印报表/// /// 打印机名称/// 打印份数///true成功,false失败 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、非套打,全部打印的效果。如下图。