c#調用熱敏打印機打印小票(usb 自動換行)


最近開發門店系統(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);
}
}

 

結束


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM