先将pos机设置为默认
控制面板->打印机和传真->右键->服务器属性
首先创建 ClassPrint 对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Text; namespace PrintWinform { class ClassPrint { //定义一个字符串流,用来接收所要打印的数据 private static StringReader sr; //str要打印的数据 public static bool Print(StringBuilder str) { bool result = true; try { sr = new StringReader(str.ToString()); PrintDocument pd = new PrintDocument(); pd.PrintController = new System.Drawing.Printing.StandardPrintController(); //pd.DefaultPageSettings.Margins.Top = 2; //pd.DefaultPageSettings.Margins.Left = 0; //pd.DefaultPageSettings.PaperSize.Width = 320; pd.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20); //pd.DefaultPageSettings.PaperSize.Height = 5150; pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机 pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); pd.Print(); } catch (Exception ex) { result = false; } finally { if (sr != null) sr.Close(); } return result; } private static void pd_PrintPage(object sender, PrintPageEventArgs ev) { Font printFont = new Font("Arial", 9);//打印字体 float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; String line = string.Empty; linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); while (count < linesPerPage && ((line = sr.ReadLine()) != null)) { yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } // If more lines exist, print another page. if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } } }
然后UI form
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void button1_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append(" 心理测评一体机 "); sb.Append(" 职业倾向人格临床量表 "); sb.Append("*********************************************** "); sb.Append("E量表得分为:32分,您的性格内向 "); sb.Append("N量表得分为:28分,您的情绪稳定性高 "); sb.Append("P量表得分为:56分,您的倔强性一般 "); sb.Append("L量表得分为:51分,您的掩饰性一般。 "); sb.Append("*********************************************** "); sb.Append(" 北京XXXX科技有限公司 "); sb.Append(" 测评结果仅供参考,最终结果请以心理咨询师为准 "); ClassPrint.Print(sb); }