C#小票打印POS
源码地址:https://download.****.net/download/horseroll/10788400
打印效果图:
我把打印的参数基本都封装到一个类里面,直接调用即可,可打印文字也可打印图片
首先安装打印机驱动,然后在设备和打印机中将要用的打印机设为默认打印机,然后调用该方法即可
Pulic Void Print()
{
PrintService ps = new PrintService();
//ps.StartPrint("33333","txt");//打印文字
ps.StartPrint(WriteTxt(),"txt");
ps.StartPrint(Image.FromFile(Application.StartupPath+"\\2.jpeg"), "image");//打印图片
}
WriteTxt方法就是拼接要打印的字符串
public string WriteTxt()
{
StringBuilder sb = new StringBuilder();
string tou = "伊尹餐饮公司";
string address = "深圳市罗湖区东门老街29号";
string saleID = "2010930233330";
string item = "项目";
decimal price = 25.00M;
int count = 5;
decimal total = 0.00M;
decimal fukuan = 500.00M;
sb.Append(" " + tou + " \r\n");
sb.Append("-----------------------------------------------------------------\r\n");
sb.Append("日期:" + DateTime.Now.ToShortDateString() + " " + "单号:" + saleID + "\r\n");
sb.Append("-----------------------------------------------------------------\r\n");
sb.Append("项目" + "\t\t" + "数量" + "\t" + "单价" + "\t" + "小计" + "\r\n");
for (int i = 0; i < count; i++)
{
decimal xiaoji = (i + 1) * price;
sb.Append(item + (i + 1) + "\t\t" + (i + 1) + "\t" + price + "\t" + xiaoji);
total += xiaoji;
if (i != (count))
sb.Append("\r\n");
}
sb.Append("-----------------------------------------------------------------\r\n");
sb.Append("数量: " + count + " 合计: " + total + "\r\n");
sb.Append("付款: 现金" + " " + fukuan);
sb.Append(" 现金找零:" + " " + (fukuan - total) + "\r\n");
sb.Append("-----------------------------------------------------------------\r\n");
sb.Append("地址:" + address + "\r\n");
sb.Append("电话:123456789 123456789\r\n");
sb.Append(" 谢谢惠顾欢迎下次光临 ");
return sb.ToString();
}
PrintService类源码,在这个类中可以改变是否打印弹窗确认,字体类型,大小,打印位置设置
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 小票打印
{
public class PrintService
{
public PrintService()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
}//将事件处理函数添加到PrintDocument的PrintPage中
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例
private string streamType;
private string streamtxt;
private Image streamima;
// This method will set properties on the PrintDialog object and
// then display the dialog.
public void StartPrint(string txt, string streamType)
{
this.streamType = streamType;
this.streamtxt = txt;
// Allow the user to choose the page range he or she would
// like to print.
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。
PrintDialog1.AllowSomePages = true;
// Show the help button.
PrintDialog1.ShowHelp = true;
// Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
//DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框,如果不要注释即可,直接调用docToPrint.Print()
//// If the result is OK then print the document.
//if (result == DialogResult.OK)
//{
// docToPrint.Print();//开始打印
//}
docToPrint.Print();//开始打印
}
public void StartPrint(Image ima, string streamType)
{
this.streamType = streamType;
this.streamima = ima;
// Allow the user to choose the page range he or she would
// like to print.
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。
PrintDialog1.AllowSomePages = true;
// Show the help button.
PrintDialog1.ShowHelp = true;
PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框,如果不要注释即可,直接调用docToPrint.Print()
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print();//开始打印
}
//docToPrint.Print();//开始打印
}
// The PrintDialog will print the document
// by handling the document's PrintPage event.
private void docToPrint_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
{
// Insert code to render the page here.
// This code will be called when the control is drawn.
// The following code will render a simple
// message on the printed document
switch (this.streamType)
{
case "txt":
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 7, System.Drawing.FontStyle.Regular);//在这里设置打印字体以及大小
// Draw the content.
text = streamtxt;
//e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 0, 10);//设置打印初始位置
break;
case "image":
System.Drawing.Image image = streamima;
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;
}
}
}
}