本文是利用Printdocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正。
涉及知识点:
Printdocument :从 windows 窗体应用程序打印时,定义一种可重用的可发送到打印机上的对象。PrintPrevIEwControl :表示 windows 窗体应用程序打印预览的原始预览部分,没有任何对话框或按钮。Graphics :GDI+绘图对象PrinterSettings:设置打印机属性,如:设置属性copIEs,可以设置打印份数,默认为1,PageSettings:指定应用于单页打印的设置DefaultPageSettings:Printdocument的属性PrintPage事件:Printdocument的事件,通过此事件来绘制需要打印的内容PaperSize:指定纸张大小毫米和英寸的换算:打印机是以英寸为单位的,单据设置是以毫米为单位的,所以需要转换效果图如下:
核心代码
关键代码如下:
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 }VIEw Code
源码链接
以上是内存溢出为你收集整理的C# 利用PrintDocument定制打印单据全部内容,希望文章能够帮你解决C# 利用PrintDocument定制打印单据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)