本文是利用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 }
源码链接
时间: 2024-10-14 20:48:52