文章出處
文章列表
對于套打的情況,一般出現在以下兩種情況:
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、非套打,全部打印的效果。如下圖。
文章列表
全站熱搜
留言列表