c#呼叫熱敏印表機列印小票(通用)
最近開發門店系統(wpf)用USB模式呼叫熱敏印表機列印,查了許多辦法,現在整理一下
引用PrintDocument 類
首先組織資料。。。。。。字串拼接 stringbuilder sb=newstringbuilder();
使勁往sb裡面append();就行。 完了進行一下tostring();
public void PrintOrder(PrintResponse data,out string msg,string qrCodeText = null)
{
msg = "";
StringBuilder sb = new StringBuilder();
{
if (item.Type == 1)//內容
{
var val = item.Text.Replace("\r\n",StringPrinter.CTS(10));
sb.Append(val);
}
else if (item.Type == 2)//標題列印
{
try
{
sr = new StringReader(val.Substring(2,val.Length - 2));
PrintDocument pd = new PrintDocument();
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
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),new StringFormat());
count++;
}
}
else
{
ev.Graphics.DrawString(line,
leftMargin,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,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel);
}
}
結束