最近开发门店系统(wpf)用USB模式调用热敏打印机打印,查了许多办法,现在整理一下
引用 PrintDocument 类
首先组织数据。。。。。。字符串拼接 stringbuilder sb=new stringbuilder();
使劲往sb里面append();就行。 完了进行一下tostring();
public void PrintOrder(PrintResponse data, out string msg, string qrCodeText = null)
{
msg = "";
StringBuilder sb = new StringBuilder();
foreach (var item in data.Details)
{
if (item.Type == 1)//内容
{
var val = item.Text.Replace("\r\n", StringPrinter.CTS(10));
sb.Append(val);
}
else if (item.Type == 2)//标题打印
{
var val = item.Text.Replace("\r\n", StringPrinter.CTS(10));
try
{
sr = new StringReader(val.Substring(2, val.Length - 2));
PrintDocument pd = new PrintDocument();
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.DefaultPageSettings.Margins.Top = 2;
pd.DefaultPageSettings.Margins.Left = 0;
pd.PrinterSettings.PrinterName = "ABS小票打印机";//pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage1);
pd.Print();
}
catch (Exception ex)
{
msg = "连接打印机异常,查询一下打印机名称是否修改了哦!";
}
finally
{
if (sr != null)
sr.Close();
}
}
}
string filename = string.Empty;
//打印二维码
if (!string.IsNullOrEmpty(qrCodeText))
{
sb.Append("------------------------\r\n");
sb.Append("如需增值税普通发票,请扫描\r\n");
sb.Append("以下二维码自助开票。如需增\r\n");
sb.Append("值税专业发票,请联系门店内\r\n");
sb.Append("家居顾问\r\n");
//获取图片
//Bitmap bmp = new Bitmap(filename);//filename 图片地址
}
if (Print(sb.ToString(), filename) == false)
{
msg = "连接打印机异常,查询一下打印机名称是否修改了哦!";
}
}
开始 print(sb.tostring());
//开始打印
private StringReader sr;
public bool Print(string str, string _filename = null)//str要打印的数据
{
bool result = true;
try
{
filename = _filename;
sr = new StringReader(str.ToString());
PrintDocument pd = new PrintDocument();
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.PrinterSettings.PrinterName = "小票打印机";//pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机 可使用已连接的打印机名字,或者使用默认打印机 控制面板->查看打印机设备
pd.DefaultPageSettings.Margins.Top = 2;上边距
pd.DefaultPageSettings.Margins.Left = 0;左边距
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex)
{
result = false;
}
finally
{
if (sr != null)
sr.Close();
}
return result;
}
通过委托进行打印
string filename = string.Empty;
public void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font printFont = new Font("黑体", 8);//打印字体设置
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line = "";
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
while (count < linesPerPage && ((line = sr.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
if (line.Length > 19)
{
int y = line.Length / 19;
for (int i = 0; i <= line.Length / 19; i++)//字符过长按照19个字符分割(边界自动换行没有查询到相关解决办法,欢迎大佬留言)
{
if (i < y)
{
ev.Graphics.DrawString(line.Substring(i * 19, 19), printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
else
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line.Substring(i * 19), printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
}
else
{
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
}
}
if (!string.IsNullOrWhiteSpace(filename))//filename 图片地址由上面的传过来
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
//读取图片
// 1 根据路径获取
Bitmap image = new Bitmap(filename);
//在指定区域打印二维码
int a = Convert.ToInt32(yPos);
Rectangle destRect = new Rectangle(10, Convert.ToInt32(yPos), 150, 150); 打印二维码的位置,大小
ev.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}
}
结束