在較早期的報表套打的時候,我傾向於使用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報表工具實現信封套打功能的軟件,這樣整合後,軟件體驗性就更加完美了。