打印代码 ,中间是小票的格式 path自己定义一个路径,有些变量你可以根据需要设置
方法和打印事件外面声明
代码
// 小票打印
StreamWriter sw = new StreamWriter(path, true);
strRiqi = DateTime.Now.ToString("yyyyMMdd");
strShijian = DateTime.Now.ToString("HHmmss");
sw.Write(" 冲正凭证\t\t");
sw.WriteLine(" \t");
sw.WriteLine(" \t");
sw.Write("卡号 \t");
sw.WriteLine(strKaID);
sw.Write("卡类型 \t");
sw.WriteLine(strKaLeixing);
sw.Write("卡有效期 \t");
sw.WriteLine(strKaYouxiaoqi);
sw.Write("网点编号 \t");
sw.WriteLine(strWangdianBianhao);
sw.Write("冲正前金额 \t");
sw.WriteLine(strChongzhiqianJine);
sw.Write("冲正金额 \t");
sw.WriteLine(strChongzhiJine);
sw.Write("冲正后金额 \t");
sw.WriteLine(strChongzhiHouJine);
sw.Write("日期 \t");
sw.WriteLine(strRiqi);
sw.Write("时间 \t");
sw.WriteLine(strShijian);
sw.Write("操作员 \t");
sw.WriteLine(ShareMemory.StrBianhao);
sw.Write("客服电话 \t");
sw.WriteLine(strKefuDianhua);
sw.WriteLine(" ");
sw.Write("XXXX科技有限公司");
sw.Close();
//创建一个PrintDocument的实例
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
System.IO.Stream streamToPrint;
string streamType = "txt";
//打印按钮里面
streamToPrint = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
// 创建一个PrintDialog的实例。
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.AllowSomePages = true;
PrintDialog1.ShowHelp = true;
// 把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
PrintDialog1.Document = docToPrint;
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
// 调用PrintDialog的ShowDialog函数显示打印对话框
DialogResult result = PrintDialog1.ShowDialog();
if (result == DialogResult.OK)
{
// 开始打印
docToPrint.Print();
}
//设置打印机开始打印的事件处理函数
private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
switch (this.streamType)
{
case "txt":
string text = null;
// 信息头
string strTou = string.Empty;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 8, System.Drawing.FontStyle.Regular);
System.Drawing.Font printFont1 = new System.Drawing.Font
("Arial", 11, System.Drawing.FontStyle.Regular);
System.IO.StreamReader streamReader = new StreamReader(this.streamToPrint);
text = streamReader.ReadToEnd();
// 获取信息头
strTou = text.Substring(0, 20);
//信息其他部分
text = text.Substring(20, (text.Length - 20));
// 设置信息打印格式
e.Graphics.DrawString(strTou, printFont1, System.Drawing.Brushes.Black, 5, 5);
e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 10, 5);
break;
case "image":
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
break;
default:
break;
}
}