日期:2014-05-18 浏览次数:21028 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Printing; namespace WindowsApplication1 { public partial class Form1 : Form { string passWord = "1234"; string fullnumber = "9999"; System.Drawing.Printing.PrintDocument pdBays = new System.Drawing.Printing.PrintDocument(); public Form1() { InitializeComponent(); pdBays.PrintPage += new PrintPageEventHandler(pbay_PrintPage); pdBays.DefaultPageSettings.PaperSize = new PaperSize("",600, 800); } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i <= 0; i++) { pdBays.Print(); } } public void pbay_PrintPage(object sender, PrintPageEventArgs ev) { Graphics dc = ev.Graphics; Pen pen = new Pen(Color.Black, 1); Font drawFont = new Font("仿宋_GB2312", 12); SolidBrush drawBrush = new SolidBrush(Color.Black); PointF drawPoint = new PointF(30, 200); //这句可以隐藏“正在打印”窗体 pdBays.PrintController = new System.Drawing.Printing.StandardPrintController(); dc.DrawString("\n密码:" + passWord + "\n充值券编号:" + fullnumber, drawFont, drawBrush, drawPoint); } ////网上例子 //private void btnPrint_Click(object sender, EventArgs e) //{ // //打印预览 // //PrintPreviewDialog ppd = new PrintPreviewDialog(); // PrintDocument pd = new PrintDocument(); // //设置边距 // Margins margin = new Margins(20, 20, 20, 20); // pd.DefaultPageSettings.Margins = margin; // ////纸张设置默认 // //PaperSize pageSize = new PaperSize("First custom size", 800, 600); // //pd.DefaultPageSettings.PaperSize = pageSize; // //打印事件设置 // pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); // //ppd.Document = pd; // //ppd.ShowDialog(); // try // { // pd.Print(); // } // catch (Exception ex) // { // } //} } }
------解决方案--------------------
打印完了你总知道吧,再打几个空行,再切
------解决方案--------------------
我之前写过 小票的打印,
直接是写文本文件的方式,只不是写的对象是打印机的端口,
希望对你的帮助:
#region Print_CashResult public static void Print_CashResult(DataTable dt) { SafeFileHandle sfh = null; string instruction = null; IntPtr ptr = CreateFile(PrintPort, FileAccess.Write, FileShare.Write, 0, FileMode.Open, 0, IntPtr.Zero); if (ptr.ToInt32() == -1) { MessageBox.Show(string.Format("连接打印机:{0} 失败,请检查连接端口是否正确。", PrintPort )); return; } sfh = new SafeFileHandle(ptr, true); StreamWriter sw = new StreamWriter( new FileStream( sfh, FileAccess.Write), System.Text.Encoding.Default); StringBuilder sb = new StringBuilder(); DataRow dr = dt.Rows[0]; //一次充值只有1行 sb.AppendLine(string.Format("单据编号:{0}",dr["BillNo"])); sb.AppendLine(string.Format("{0,-15}{1,23}",ClubName + "(客户联)","充值")); sb.AppendLine("===================