最近开发门店系统(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)
{
StringFormat fmt = new StringFormat();
fmt.LineAlignment = StringAlignment.Near;
fmt.FormatFlags = StringFormatFlags.LineLimit;
Graphics g = ev.Graphics;
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = new Font("微软雅黑", 9, FontStyle.Regular);
SolidBrush myBrush = new SolidBrush(Color.Black);
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(g);
int temp = 0;
while (count < linesPerPage && ((line = sr.ReadLine()) != null))
{
yPosition = topMargin + (temp * printFont.GetHeight(g));
line += Environment.NewLine;
try
{
if (TextRenderer.MeasureText(line, printFont).Width > ev.MarginBounds.Width * 2)
{
line = line.Replace("\r\n", "");
if (line.StartsWith("-------------------") || line.StartsWith("....................."))
{
ev.Graphics.DrawString(line, printFont, myBrush,
leftMargin, yPosition);
count++;
temp++;
}
else
{
//temp += TextRenderer.MeasureText(line, printFont).Width / ev.MarginBounds.Width;
g.DrawString(line, printFont, myBrush, new Rectangle((int)leftMargin,
(int)yPosition, ev.MarginBounds.Width * 2, ev.MarginBounds.Height), fmt);
count++;
temp++;
}
}
else
g.DrawString(line, printFont, myBrush, leftMargin, yPosition);
count++;
temp++;
}
catch (Exception ex)
{
MessageBox.Show("字符串格式错误:" + ex.ToString());
}
}
if (line != null)
{
ev.HasMorePages = true;
}
else
{
ev.HasMorePages = false;
}
if (!string.IsNullOrWhiteSpace(filename))
{
yPosition = topMargin + (temp * printFont.GetHeight(ev.Graphics));
//读取图片
// 1 根据路径获取
Bitmap image = new Bitmap(filename);
//在指定区域打印二维码
int a = Convert.ToInt32(yPosition);
Rectangle destRect = new Rectangle(10, Convert.ToInt32(yPosition), 150, 150);
ev.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}
}
结束