最近一周竟然有2位以前的同事問我在winfor應用程序里面打印怎么搞,所以才有了寫這篇文章的打算,索性現在沒事就寫出來
在窗體上簡單的布局設置一下如圖
定義一個Model 我在里面放了屬性之外還從寫了ToString方法和返回模型的方法。
代碼如下
1 #region Model 2 public DateTime CreateDate = DateTime.Today; 3 public string DocNumber; 4 public string Title { get; set; } 5 public string Consignee { get; set; } 6 public string ProductName { get; set; } 7 public string ProductLot { get; set; } 8 public string Specification { get; set; } 9 public string Factory { get; set; } 10 public string RegNo { get; set; } 11 public string PermitNo { get; set; } 12 public DateTime ExpirationDate { get; set; } 13 public DateTime DeliveryDate { get; set; } 14 public string Unit { get; set; } 15 public int Count { get; set; } 16 public float Price { get; set; } 17 public float Money { get; set; } 18 #endregion 19 20 21 private const char Spliter = '|';//分割txt里面的值 22 23 24 public override string ToString() 25 { 26 StringBuilder sb = new StringBuilder(); 27 sb.Append(CreateDate); 28 sb.Append(Spliter); 29 sb.Append(DocNumber); 30 sb.Append(Spliter); 31 sb.Append(Title); 32 sb.Append(Spliter); 33 sb.Append(Consignee); 34 sb.Append(Spliter); 35 sb.Append(ProductName); 36 sb.Append(Spliter); 37 sb.Append(ProductLot); 38 sb.Append(Spliter); 39 sb.Append(Specification); 40 sb.Append(Spliter); 41 sb.Append(Factory); 42 sb.Append(Spliter); 43 sb.Append(RegNo); 44 sb.Append(Spliter); 45 sb.Append(PermitNo); 46 sb.Append(Spliter); 47 sb.Append(ExpirationDate); 48 sb.Append(Spliter); 49 sb.Append(DeliveryDate); 50 sb.Append(Spliter); 51 sb.Append(Unit); 52 sb.Append(Spliter); 53 sb.Append(Count); 54 sb.Append(Spliter); 55 sb.Append(Price); 56 sb.Append(Spliter); 57 sb.Append(Money); 58 sb.Append(Spliter); 59 return sb.ToString(); 60 } 61 62 public static OutshipDoc FromString(string s) 63 { 64 try 65 { 66 int i = 0; 67 68 OutshipDoc doc = new OutshipDoc(); 69 string[] pars = s.Split(new char[] { Spliter }); 70 doc.CreateDate = DateTime.Parse(pars[i++]); 71 doc.DocNumber = pars[i++]; 72 doc.Title = pars[i++]; 73 doc.Consignee = pars[i++]; 74 doc.ProductName = pars[i++]; 75 doc.ProductLot = pars[i++]; 76 doc.Specification = pars[i++]; 77 doc.Factory = pars[i++]; 78 doc.RegNo = pars[i++]; 79 doc.PermitNo = pars[i++]; 80 doc.ExpirationDate = DateTime.Parse(pars[i++]); 81 doc.DeliveryDate = DateTime.Parse(pars[i++]); 82 doc.Unit = pars[i++]; 83 doc.Count = int.Parse(pars[i++]); 84 doc.Price = float.Parse(pars[i++]); 85 doc.Money = float.Parse(pars[i++]); 86 return doc; 87 } 88 catch (System.Exception ex) 89 { 90 return null; 91 } 92 }
Model里面的東西已經准備完了,現在就開始前台的代碼了
OutshipDoc doc; internal OutshipDoc Doc { get { return doc; } set { doc = value; } }
以上是model,接下來就是定義公用的
PrintDocument
private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();//創建一個PrintDocument的實例
定義好了就用構造函數初始化Model
public PrintPreview() { InitializeComponent(); doc = new OutshipDoc(); doc.Title = "廣州市xx醫療器械有限公司送貨清單"; doc.Consignee = "陽江人民醫院"; doc.ProductName = "銀爾通活性銀離子抗菌液III型"; doc.DocNumber = "X2012040201"; doc.DeliveryDate = DateTime.Parse("2012/4/2"); doc.Factory = "西安康旺抗菌科技股份有限公司"; doc.Specification = "1200ml/盒"; doc.RegNo = "陝食葯監械(准)字2008第2640064號"; doc.PermitNo = "陝食葯監械生產許20052286號"; doc.ProductLot = "20110402"; doc.ExpirationDate = DateTime.Parse("2014/04/01"); doc.Unit = "盒"; doc.Count = 960; doc.Price = 39.00F; doc.Money = 37441.18F; docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage); }
//畫打印格式 void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { int margin = 20; int width = 750; int height = e.PageBounds.Height; int left = (e.PageBounds.Width - width) / 2; int top = 20; renderDocument(doc, e.Graphics, left, top, width, height); }
public static void renderDocument(OutshipDoc d, Graphics g, int left, int top, int width, int height) { width = 750; Brush br = Brushes.Black; // 打印抬頭 Font ft; StringFormat sf; ft = new Font("宋體", 14); sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.FormatFlags = StringFormatFlags.NoClip; g.DrawString(d.Title, ft, br , new RectangleF(new PointF(left, top), new SizeF(width , height )) , sf); string info1 = "收貨單位:" + d.Consignee + " " + "品名:" + d.ProductName; ft.Dispose(); ft = new Font("宋體", 12); g.DrawString(info1, ft, br, new PointF(left, top + 50)); info1 = "編號:" + d.DocNumber; g.DrawString(info1, ft, br, new PointF(left + width - 200, top + 50)); ft.Dispose(); ft = new Font("宋體", 10); info1 = "生產廠家:" + d.Factory; g.DrawString(info1, ft, br, new PointF(left, top + 80)); info1 = "送貨日期:" + d.DeliveryDate.Year + "年" + d.DeliveryDate.Month + "月" + d.DeliveryDate.Day + "日"; g.DrawString(info1, ft, br, new PointF(left + width - 200, top + 80)); int x = left, y = top + 100; Pen pn = Pens.Black; g.DrawLine(pn, new Point(left, y), new Point(left + width, y)); y += 50; g.DrawLine(pn, new Point(left, y), new Point(left + width, y)); y += 50; g.DrawLine(pn, new Point(left, y), new Point(left + 80, y)); g.DrawLine(pn, new Point(left + 80 + 240, y), new Point(left + width, y)); y += 50; g.DrawLine(pn, new Point(left, y), new Point(left + width, y)); y += 50; g.DrawLine(pn, new Point(left, y), new Point(left + width, y)); int y1 = top + 100; int y2 = top + 250; int y3 = top + 300; g.DrawLine(pn, new Point(x, y1), new Point(left, y3)); x += 80; g.DrawLine(pn, new Point(x, y1), new Point(x, y3)); x += 120; g.DrawLine(pn, new Point(x, y1), new Point(x, y2)); x += 120; g.DrawLine(pn, new Point(x, y1), new Point(x, y2)); x += 80; g.DrawLine(pn, new Point(x, y1), new Point(x, y2)); x += 80; g.DrawLine(pn, new Point(x, y1), new Point(x, y2)); x += 60; g.DrawLine(pn, new Point(x, y1), new Point(x, y2)); x += 60; g.DrawLine(pn, new Point(x, y1), new Point(x, y2)); x += 60; g.DrawLine(pn, new Point(x, y1), new Point(x, y3)); x = left + width; g.DrawLine(pn, new Point(x, y1), new Point(x, y3)); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; ft.Dispose(); ft = new Font("宋體", 11); x = left; g.DrawString("規格", ft, br, new RectangleF(x, y1, 80, 50), sf); x += 80; g.DrawString("產品注冊證號", ft, br, new RectangleF(x, y1, 120, 50), sf); x += 120; g.DrawString("醫療器械生產企業許可證號", ft, br, new RectangleF(x, y1, 120, 50), sf); x += 120; g.DrawString("產品批號", ft, br, new RectangleF(x, y1, 80, 50), sf); x += 80; g.DrawString("失效日期", ft, br, new RectangleF(x, y1, 80, 50), sf); x += 80; g.DrawString("單位", ft, br, new RectangleF(x, y1, 60, 50), sf); x += 60; g.DrawString("數量", ft, br, new RectangleF(x, y1, 60, 50), sf); x += 60; g.DrawString("單價", ft, br, new RectangleF(x, y1, 60, 50), sf); x += 60; g.DrawString("金額", ft, br, new RectangleF(x, y1, 100, 50), sf); x += 150; x = left; g.DrawString(d.Specification, ft, br, new RectangleF(x, y1 + 50, 80, 50), sf); x += 80; g.DrawString(d.RegNo, ft, br, new RectangleF(x, y1 + 50, 120, 80), sf); x += 120; g.DrawString(d.PermitNo, ft, br, new RectangleF(x, y1 + 50, 120, 80), sf); x += 120; g.DrawString(d.ProductLot, ft, br, new RectangleF(x, y1 + 50, 80, 50), sf); x += 80; g.DrawString(d.ExpirationDate.ToString("yyyyMMdd"), ft, br, new RectangleF(x, y1 + 50, 80, 50), sf); x += 80; g.DrawString(d.Unit, ft, br, new RectangleF(x, y1 + 50, 60, 50), sf); x += 60; g.DrawString(d.Count.ToString(), ft, br, new RectangleF(x, y1 + 50, 60, 50), sf); x += 60; g.DrawString(d.Price.ToString("#.00"), ft, br, new RectangleF(x, y1 + 50, 60, 50), sf); x += 60; g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y1 + 50, 100, 50), sf); x += 100; x = left; sf.Alignment = StringAlignment.Near; g.DrawString("合計:", ft, br, new RectangleF(x, y2, 100, 50), sf); x += 80; g.DrawString("人民幣(大寫)" + moneyToString(d.Money), ft, br, new RectangleF(x, y2, 500, 50), sf); x += 120; x += 120; x += 80; x += 80; x += 60; x += 60; x += 60; sf.Alignment = StringAlignment.Center; g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y2, 100, 50), sf); x += 100; x = left; sf.Alignment = StringAlignment.Near; g.DrawString("收貨單位:", ft, br, new RectangleF(x, y3, 400, 50), sf); x += 400; g.DrawString("送貨單位:", ft, br, new RectangleF(x, y3, 400, 50), sf); }
//貨幣大寫轉換 private static string moneyToString(double p) { string[] numbers = new string[] { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "镹"}; string[] unit = new string[] { "元", "拾", "佰", "仟", "萬", "拾萬", "佰萬", "仟萬" }; string[] xunit = new string[] { "角", "分" }; int x = (int)p; string s = ""; int i = 0; for (i = 0; i < 8 && x > 0; i++) { if (x % 10 != 0) s = numbers[x % 10] + unit[i] + s; x /= 10; } if (s == "") s = "零"; if (!s.EndsWith("元")) s += "元"; double y = p - (int)p; if (y != 0.0f) { y *= 10; if ((int)y != 0) s += numbers[(int)y] + "角"; y = y - (int)y; y *= 10; if ((int)y != 0) s += numbers[(int)y] + "分"; } if (s.EndsWith("元")) s += "整"; return s; }
輸出的格式瀏覽就已經寫完最好就差真正的打印了
private void btnPrint_Click(object sender, EventArgs e) { PrintDialog pd = new PrintDialog(); pd.Document = docToPrint; if (pd.ShowDialog() == DialogResult.OK) { docToPrint.Print(); } }
到此就已經寫完了看看效果了,如圖下面就是最后的效果