1. 程式人生 > >C# 利用PrintDocument定制打印單據

C# 利用PrintDocument定制打印單據

tasks 打印預覽 文檔 forms == event box code val

本文是利用PrintDocument定制打印單據的小例子。

涉及知識點:

  • PrintDocument :從 Windows 窗體應用程序打印時,定義一種可重用的可發送到打印機上的對象。
  • PrintPreviewControl :表示 Windows 窗體應用程序打印預覽的原始預覽部分,沒有任何對話框或按鈕。
  • Graphics :GDI+繪圖對象
  • PageSettings:指定應用於單頁打印的設置
  • DefaultPageSettings:PrintDocument的屬性
  • PrintPage事件:PrintDocument的事件,通過此事件來繪制需要打印的內容
  • PaperSize:指定紙張大小
  • 毫米和英寸的換算:打印機是以英寸為單位的,單據設置是以毫米為單位的,所以需要轉換

效果圖如下:

技術分享

關鍵代碼如下:

技術分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Drawing.Printing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
11 12 namespace DemoPrint 13 { 14 public partial class MainForm : Form 15 { 16 private int width_p = 200;//單位是mm 17 18 private int height_p = 70;//單位是mm 19 20 private int margin_lr = 2;//左右邊距 21 22 private int margin_tb = 2;//上下邊距 23 24 /// <summary>
25 /// 需要打印的內容 26 /// </summary> 27 public List<PrintInfo> PrintInfos { get; set; } 28 29 private PrintHelper printHelper = new PrintHelper(); 30 31 public MainForm() 32 { 33 InitializeComponent(); 34 } 35 36 private void MainForm_Load(object sender, EventArgs e) 37 { 38 InitInfo(); 39 InitDocument(); 40 } 41 42 private void InitInfo() { 43 PrinterSettings printSetting = new PrinterSettings(); 44 printSetting.PrintRange = PrintRange.AllPages; 45 46 47 int width_in = MM2Inch(width_p); 48 int height_in = MM2Inch(height_p); 49 PageSettings pageSetting = new PageSettings(printSetting); 50 pageSetting.PaperSize = new PaperSize("customer",width_in, height_in); 51 52 int margin_lr_in = MM2Inch(margin_lr); 53 int margin_tb_in = MM2Inch(margin_tb); 54 pageSetting.Margins = new Margins(margin_lr_in, margin_lr_in, margin_tb_in, margin_tb_in); 55 this.pdControl.DefaultPageSettings = pageSetting; 56 } 57 58 private void InitDocument() { 59 List<PrintInfo> lstPrintInfos = new List<PrintInfo>(); 60 PrintInfo p0 = new PrintInfo() 61 { 62 PrtType = PrintType.Table, 63 PrtColor = Color.Brown, 64 Row = int.Parse(this.txtRow.Text.Trim()), 65 Column = int.Parse(this.txtColumn.Text.Trim()), 66 Start = new Point(int.Parse(this.txtStart.Text.Trim(new char[] { (, ) }).Split(,)[0]), int.Parse(this.txtStart.Text.Trim(new char[] { (, ) }).Split(,)[1])), 67 End = new Point(int.Parse(this.txtEnd.Text.Trim(new char[] { (, ) }).Split(,)[0]), int.Parse(this.txtEnd.Text.Trim(new char[] { (, ) }).Split(,)[1])) 68 69 }; 70 lstPrintInfos.Add(p0); 71 printHelper.PrintInfos = lstPrintInfos; 72 } 73 74 /// <summary> 75 /// 轉換毫米到百分之一英寸 76 /// </summary> 77 /// <param name="mm"></param> 78 /// <returns></returns> 79 private int MM2Inch(int mm) { 80 return (int)(mm * 100.0f / 25.4f); 81 } 82 83 /// <summary> 84 /// 打印開始事件 85 /// </summary> 86 /// <param name="sender"></param> 87 /// <param name="e"></param> 88 private void pdControl_BeginPrint(object sender, PrintEventArgs e) 89 { 90 91 } 92 93 /// <summary> 94 /// 打印事件 95 /// </summary> 96 /// <param name="sender"></param> 97 /// <param name="e"></param> 98 private void pdControl_PrintPage(object sender, PrintPageEventArgs e) 99 { 100 Font font = new Font("Arial", 14f, FontStyle.Regular); 101 Graphics g = e.Graphics; 102 g.PageScale = 1; 103 g.PageUnit = GraphicsUnit.Millimeter; 104 //先畫一個矩形 105 Pen lineColor = new Pen(Color.Black, 0.2f); 106 g.FillRectangle(Brushes.Linen,0,0,width_p,height_p); 107 printHelper.Print(g); 108 } 109 110 /// <summary> 111 /// 打印結束事件 112 /// </summary> 113 /// <param name="sender"></param> 114 /// <param name="e"></param> 115 private void pdControl_EndPrint(object sender, PrintEventArgs e) 116 { 117 118 } 119 120 121 /// <summary> 122 /// 打印 123 /// </summary> 124 /// <param name="sender"></param> 125 /// <param name="e"></param> 126 private void btnPrint_Click(object sender, EventArgs e) 127 { 128 //打印對話框 129 if (this.ptDControl.ShowDialog() == DialogResult.OK) 130 { 131 this.pdControl.Print(); 132 } 133 134 } 135 136 private void lblColor_Click(object sender, EventArgs e) 137 { 138 ColorDialog f = new ColorDialog(); 139 if (f.ShowDialog() == DialogResult.OK) 140 { 141 142 this.lblColor.BackColor = f.Color; 143 } 144 } 145 146 /// <summary> 147 /// 刷新 148 /// </summary> 149 /// <param name="sender"></param> 150 /// <param name="e"></param> 151 private void btnRefresh_Click(object sender, EventArgs e) 152 { 153 List<PrintInfo> lstPrintInfos = new List<PrintInfo>(); 154 //表格配置 155 PrintInfo p0 = new PrintInfo() 156 { 157 PrtType = PrintType.Table, 158 PrtColor = Color.Brown, 159 Row = int.Parse(this.txtRow.Text.Trim()), 160 Column = int.Parse(this.txtColumn.Text.Trim()), 161 Start = new Point(int.Parse(this.txtStart.Text.Trim(new char[] { (, ) }).Split(,)[0]), int.Parse(this.txtStart.Text.Trim(new char[] { (, ) }).Split(,)[1])), 162 End = new Point(int.Parse(this.txtEnd.Text.Trim(new char[] { (, ) }).Split(,)[0]), int.Parse(this.txtEnd.Text.Trim(new char[] { (, ) }).Split(,)[1])) 163 164 }; 165 lstPrintInfos.Add(p0); 166 //標題配置 167 PrintInfo p1 = new PrintInfo() 168 { 169 PrtType = PrintType.Text, 170 PrtColor = this.lblColor.BackColor, 171 Content = this.txtTitle.Text.Trim(), 172 Size = int.Parse(this.txtSize.Text.Trim()), 173 FontStyle = chkBold.Checked ? FontStyle.Bold : FontStyle.Regular, 174 Start = new Point(int.Parse(this.txtLocation.Text.Trim(new char[] { (, ) }).Split(,)[0]), int.Parse(this.txtLocation.Text.Trim(new char[] { (, ) }).Split(,)[1])) 175 }; 176 lstPrintInfos.Add(p1); 177 //內容 178 TextBox[] T = new TextBox[12] { T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 }; 179 TextBox[] L = new TextBox[12] { L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12 }; 180 for (int i = 0; i < 12; i++) 181 { 182 PrintInfo p = new PrintInfo() 183 { 184 PrtType = PrintType.Text, 185 PrtColor = Color.Black, 186 Content = T[i].Text.Trim(), 187 Size = 12, 188 FontStyle = FontStyle.Regular, 189 Start = new Point(int.Parse(L[i].Text.Trim(new char[] { (, ) }).Split(,)[0]), int.Parse(L[i].Text.Trim(new char[] { (, ) }).Split(,)[1])) 190 }; 191 lstPrintInfos.Add(p); 192 } 193 //打印時間 194 PrintInfo p2 = new PrintInfo() 195 { 196 PrtType = PrintType.Text, 197 PrtColor = this.lblColor.BackColor, 198 Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 199 Size =11, 200 FontStyle =FontStyle.Regular, 201 Start = new Point(145,63) 202 }; 203 lstPrintInfos.Add(p2); 204 205 printHelper.PrintInfos = lstPrintInfos; 206 this.ppVControl.InvalidatePreview();//刷新文檔的預覽,重新調用PrintDocument的Print方法 207 } 208 } 209 }
View Code


源碼鏈接

C# 利用PrintDocument定制打印單據