在较早期的报表套打的时候,我倾向于使用LODOP的ActiveX进行报表的打印或者套打,BS效果仍是很不错的。以前利用它在Winform程序里面实现信封套打功能,详细参考《基于信封套打以及批量打印的实现过程》,虽然功能可以完美实现,不过因为还须要附带一个不是百分百整合一块儿的插件,仍是有点另类的,虽然只是第一次使用的时候,须要安装一次插件便可。本篇随笔介绍一下如何旧瓶装新酒,使用FastReport报表工具来实现信封的套打及批量打印的功能。html
首先咱们要有一些特制的信封或者普通讯封,这样才能基于这个基础上进行套打,把邮政编码、地址和联系人等信息打印上去。工具
而后你须要有一个打印设备,我这里采用了一个佳能的喷墨打印机(固然其余的也没问题)。post
其次咱们开发一个工具来快速实现数据的导入和批量打印,以下界面所示。测试
最后固然可以知足要求的打印大量的信封出来,减小咱们人工干预的麻烦了。this
首先咱们模仿上面的工具界面来作一个新的Winform程序,此次使用DevExpress界面来作,获得界面以下所示。编码
功能和前面软件的基本同样,只是界面有所变化差别而已。url
如今咱们来聊聊如何FastReport报表工具来实现套打的处理,这样咱们就可使用它来进行信封的打印了。spa
首先,和前面随笔《使用FastReport报表工具生成报表PDF文档》/《使用FastReport报表工具生成标签打印文档》等文章介绍的同样,咱们套打同样须要准备好一个报表模板,而后基于这个基础上进行套打处理。插件
套打的原理,就是预设一个图片和报表的大小,以及放置一些须要打印的内容,预设的图片通常不须要打印出来,这样其余内容打印在特定的纸张上就实现了证书、信封、发票、快递单等的打印了。设计
而使用FastReport报表工具,咱们能够对报表模板里面的元素的位置、大小、样式等进行必定的调整,以适应咱们具体的报表须要,基于这个模式咱们先设计一个FastReport报表模板,以下所示。
以上模板的设置,主要就是注意定义好几个参数,并将参数和具体的展现控件进行绑定,并加入一个图片做为不打印的元素便可。
报表在运行时刻能够进行模板的调整,以下是报表的【打印设计】界面。
咱们能够利用FastReport提供的报表设计工具进行元素位置、大小、样式等方面的调整。这样就能够给客户很大的灵活性进行处理。
报表打印的操做以下代码所示。
/// <summary> /// 报表打印测试 /// </summary> private void btnPrintTest_Click(object sender, EventArgs e) { if(this.txtAddress.Text.Length == 0) { MessageDxUtil.ShowTips("请输入地址"); this.txtAddress.Focus(); return; } else if (this.txtReceiver.Text.Length == 0) { MessageDxUtil.ShowTips("请输入收件人"); this.txtReceiver.Focus(); return; } FrmReportPreview dlg = new FrmReportPreview(); var report = dlg.Report; //加载报表 var reportFile = Path.Combine(baseDir, "Report/信封报表.frx"); report.Load(reportFile); //绑定数据源 //定义参数和数据格式 var dict = new Dictionary<string, object>(); var zipCode = txtPostCode.Text.Trim().PadRight(6, ' ').ToCharArray(); dict.Add("C1", zipCode[0]); dict.Add("C2", zipCode[1]); dict.Add("C3", zipCode[2]); dict.Add("C4", zipCode[3]); dict.Add("C5", zipCode[4]); dict.Add("C6", zipCode[5]); dict.Add("Address", this.txtAddress.Text.Trim()); var Recipient = this.txtReceiver.Text.Trim(); if(!Recipient.EndsWith("收")) { Recipient += "收"; } dict.Add("Recipient", Recipient); //刷新数据源 foreach (string key in dict.Keys) { report.SetParameterValue(key, dict[key]); } dlg.ShowDialog(); }
以上打印处理的时候,会调用打印预览界面展现数据,以下界面所示。
报表打印设计处理,和打印测试差很少,也须要绑定数据,方便预览,代码以下所示。
/// <summary> /// 报表打印设计 /// </summary> private void btnPrintDesign_Click(object sender, EventArgs e) { FrmReportDesign dlg = new FrmReportDesign(); var report = dlg.Report; //加载报表文件 var reportFile = Path.Combine(baseDir, "Report/信封报表.frx"); report.Load(reportFile); //绑定数据源 //定义参数和数据格式 var dict = new Dictionary<string, object>(); var zipCode = txtPostCode.Text.Trim().PadRight(6, ' ').ToCharArray(); dict.Add("C1", zipCode[0]); dict.Add("C2", zipCode[1]); dict.Add("C3", zipCode[2]); dict.Add("C4", zipCode[3]); dict.Add("C5", zipCode[4]); dict.Add("C6", zipCode[5]); dict.Add("Address", this.txtAddress.Text.Trim()); var Recipient = this.txtReceiver.Text.Trim(); if (!Recipient.EndsWith("收")) { Recipient += "收"; } dict.Add("Recipient", Recipient); //刷新数据源 foreach (string key in dict.Keys) { report.SetParameterValue(key, dict[key]); } dlg.ShowDialog(); }
信封打印,咱们不须要一个个确认打印对话框,指定那个PrintDialog为False便可。信封的批量打印代码以下所示。
/// <summary> /// 信封批量打印操做 /// </summary> private void btnBatchPrint_Click(object sender, EventArgs e) { if(dtImport == null || dtImport.Rows.Count == 0) { MessageDxUtil.ShowTips("没有打印数据"); return; } FastReport.Report report = new FastReport.Report(); FastReport.Utils.Res.LocaleFolder = Path.Combine(baseDir, "L18N"); var file = FastReport.Utils.Res.LocaleFolder + @"Chinese (Simplified).frl"; FastReport.Utils.Res.LoadLocale(file); //加载报表 var reportFile = Path.Combine(baseDir, "Report/信封报表.frx"); report.Load(reportFile); SplashScreenHelper.Show(typeof(FrmWaitForm)); SplashScreenHelper.SetCaption("正在打印..."); int total = dtImport.Rows.Count; int i = 0; foreach(DataRow dr in dtImport.Rows) { SplashScreenHelper.SetDescription(string.Format("正在打印...任务 {0} / {1}", i++, total)); //绑定数据源 //定义参数和数据格式 var dict = new Dictionary<string, object>(); var zipCode = dr["邮编"].ToString().Trim().PadRight(6, ' ').ToCharArray(); dict.Add("C1", zipCode[0]); dict.Add("C2", zipCode[1]); dict.Add("C3", zipCode[2]); dict.Add("C4", zipCode[3]); dict.Add("C5", zipCode[4]); dict.Add("C6", zipCode[5]); dict.Add("Address", dr["地址"].ToString().Trim()); var Recipient = dr["收件人"].ToString().Trim(); if (!Recipient.EndsWith("收")) { Recipient += "收"; } dict.Add("Recipient", Recipient); //刷新数据源 foreach (string key in dict.Keys) { report.SetParameterValue(key, dict[key]); } report.PrintSettings.ShowDialog = false; report.Print(); Thread.Sleep(100); } SplashScreenHelper.SetCaption("打印完成!"); SplashScreenHelper.SetDescription(string.Format("完成所有打印,共打印【{0}】份!", total)); Thread.Sleep(500); SplashScreenHelper.Close(); }
咱们使用该批量模式测试打印几个信封,效果以下所示。
若是导入数据不少,那么同样和前面的软件打印效果同样,中间不须要人工接入,喝茶等着完成便可。
这个就是整合了FastReport报表工具实现信封套打功能的软件,这样整合后,软件体验性就更加完美了。