C# 利用PrintDocument定制打印单据
发布时间:2020-12-16 01:13:54 所属栏目:百科 来源:网络整理
导读:本文是利用PrintDocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正。 涉及知识点: PrintDocument :从 Windows 窗体应用程序打印时,定义一种可重用的可发送到打印机上的对象。 PrintPreviewControl :表示 Windows 窗体应用程序打印
本文是利用PrintDocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正。 涉及知识点:
?效果图如下: ? 核心代码 关键代码如下: 1 using System; 2 System.Collections.Generic; 3 System.ComponentModel; 4 System.Data; 5 System.Drawing; 6 System.Drawing.Printing; 7 System.Linq; 8 System.Text; 9 System.Threading.Tasks; 10 System.Windows.Forms; 11 12 namespace DemoPrint 13 { 14 public partial class MainForm : Form 15 { 16 private int width_p = 200;//单位是mm 17 18 int height_p = 70; 19 20 int margin_lr = 2;左右边距 21 22 int margin_tb = 上下边距 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 void MainForm_Load(object sender,EventArgs e) 37 38 InitInfo(); 39 InitDocument(); 40 41 42 void InitInfo() { 43 PrinterSettings printSetting = 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 = 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 = Margins(margin_lr_in,margin_lr_in,margin_tb_in,margin_tb_in); 55 this.pdControl.DefaultPageSettings = pageSetting; 56 57 58 InitDocument() { 59 List<PrintInfo> lstPrintInfos = new List<PrintInfo>(); 60 PrintInfo p0 = PrintInfo() 61 { 62 PrtType = PrintType.Table, 63 PrtColor = Color.Brown,1)"> 64 Row = int.Parse(this.txtRow.Text.Trim()),1)"> 65 Column = .txtColumn.Text.Trim()),1)"> 66 Start = new Point(this.txtStart.Text.Trim(new char[] { '(',)' }).Split(')[0]),1])),1)"> 67 End = this.txtEnd.Text.Trim(])) 68 69 }; 70 lstPrintInfos.Add(p0); 71 printHelper.PrintInfos = lstPrintInfos; 72 73 74 75 转换毫米到百分之一英寸 76 77 <param name="mm"></param> 78 <returns></returns> 79 int MM2Inch(int mm) { 80 return (int)(mm * 100.0f / 25.4f); 81 82 83 84 打印开始事件 85 86 <param name="sender"></param> 87 <param name="e"></param> 88 void pdControl_BeginPrint( 89 90 91 92 93 94 打印事件 95 96 97 98 void pdControl_PrintPage( 99 100 Font font = new Font(Arial101 Graphics g = e.Graphics; 102 g.PageScale = ; 103 g.PageUnit = GraphicsUnit.Millimeter; 104 先画一个矩形 105 Pen lineColor = new Pen(Color.Black,1)">0.2f106 g.FillRectangle(Brushes.Linen,1)">0,1)">0107 printHelper.Print(g); 108 109 110 111 打印结束事件 112 113 114 115 void pdControl_EndPrint(116 117 118 119 120 121 122 打印 123 124 125 126 void btnPrint_Click(127 128 打印对话框 129 if (this.ptDControl.ShowDialog() == DialogResult.OK) 130 131 .pdControl.Print(); 132 } 133 134 135 136 void lblColor_Click(137 138 ColorDialog f = ColorDialog(); 139 if (f.ShowDialog() ==140 141 142 this.lblColor.BackColor = f.Color; 143 144 145 146 147 刷新 148 149 150 151 void btnRefresh_Click(152 153 List<PrintInfo> lstPrintInfos = 154 表格配置 155 PrintInfo p0 = 156 157 PrtType =158 PrtColor =159 Row = 160 Column = 161 Start = 162 End = 163 164 165 166 标题配置 167 PrintInfo p1 = 168 169 PrtType = PrintType.Text,1)">170 PrtColor = .lblColor.BackColor,1)">171 Content = .txtTitle.Text.Trim(),1)">172 Size = .txtSize.Text.Trim()),1)">173 FontStyle = chkBold.Checked ? FontStyle.Bold : FontStyle.Regular,1)">174 Start = this.txtLocation.Text.Trim(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 = ] { L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12 }; 180 for (int i = 0; i < 12; i++) 181 182 PrintInfo p = 183 { 184 PrtType =185 PrtColor = Color.Black,1)">186 Content = T[i].Text.Trim(),1)">187 Size = 188 FontStyle = FontStyle.Regular,1)">189 Start = int.Parse(L[i].Text.Trim(190 }; 191 lstPrintInfos.Add(p); 192 193 打印时间 194 PrintInfo p2 = 195 196 PrtType =197 PrtColor = 198 Content = DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss),1)">199 Size =11200 FontStyle =FontStyle.Regular,1)">201 Start = new Point(145,1)">63202 203 lstPrintInfos.Add(p2); 204 205 printHelper.PrintInfos =206 this.ppVControl.InvalidatePreview();刷新文档的预览,重新调用PrintDocument的Print方法 207 208 } 209 }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |