//定义一个字符串流,用来接收所要打印的数据
private StringReader sr;
public bool Print(string str)//str要打印的数据
{
bool result = true;
try
{
sr = new StringReader(str.ToString());
PrintDocument pd = new PrintDocument();
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.DefaultPageSettings.Margins.Top = 2;
pd.DefaultPageSettings.Margins.Left = 0;
pd.DefaultPageSettings.PaperSize.Width = 320;
pd.DefaultPageSettings.PaperSize.Height = 5150;
pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex)
{
result = false;
}
finally
{
if (sr != null)
sr.Close();
}
return result;
}
public void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font printFont = new Font("Arial", 7);//打印字体
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));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
public void PrintOrder(string order)
{
var r = JsonConvert.DeserializeObject<Result>(order);
StringBuilder sb = new StringBuilder();
int TotalPrice = 0;
//int num = 32 - int.Parse(r.StoreName.toString().Length);
//string str = "";
//for (int i = 0; i < num; i++)
//{
// str += " ";
//}
//sb.Append(""+str+""+ r.StoreName + "\n");
sb.Append(" " + r.StoreName + "\n");
sb.Append("-----------------------------------------------------------------\n");
sb.Append("收银员:" + r.OperatorName + " " + "流水号:" + r.PurchaseNo + "\n");
sb.Append("时间:" + DateTime.Now + " " + " " + " " + "\n");
sb.Append("-----------------------------------------------------------------\n");
sb.Append("序号" + " " + "商品名" + " " + "数量" + " " + "单价(元)" + " " + "金额(元)" + "\n");
foreach (var item in r.Details)
{
sb.Append(@" " + r.Details.IndexOf(item) + " " + "" + item.Title + "\n "
+ item.Number + " " + item.Price + " " + item.Total + " " + "\n");
TotalPrice += int.Parse(item.Total);
}
sb.Append("-----------------------------------------------------------------\n");
//sb.Append("实收:" + (TotalPrice).ToString() + " \n");
sb.Append("总价:" + r.OriginalTotal + " 实收:" + r.PayTotal + " \n");
sb.Append("-----------------------------------------------------------------\n");
sb.Append(" " + "谢谢光临,欢迎下次光顾!\n");
//sb.Append(" " + r.PurchaseNo);
// updateData(int.Parse(jp.Result[0].Status));
//}
Print(sb.ToString());
}
}
public class Info
{
public string Code { get; set; }
public string Msg { get; set; }
public List<Result> Result { get; set; }
public string IsAmortize { get; set; }
public string Point { get; set; }
public string OriginalTotal { get; set; }
public string PointTotal { get; set; }
}
public class Result
{
public string Id { get; set; }
public string PurchaseNo { get; set; }
public string CreateTime { get; set; }
public string StoreId { get; set; }
public string StoreName { get; set; }
public string OperatorName { get; set; }
public string Creator { get; set; }
public string CustomerId { get; set; }
public string Status { get; set; }
public string IsPrint { get; set; }
public string DiscountMoney { get; set; }
public string Type { get; set; }
public string Updateor { get; set; }
public string UpdateTime { get; set; }
public string Total { get; set; }
public string PayTotal { get; set; }
public string OriginalTotal { get; set; }
public List<Details> Details { get; set; }
}
public class Details
{
public string Id { get; set; }
public string ProductId { get; set; }
public string Number { get; set; }
public string Price { get; set; }
public string Total { get; set; }
public string WarehouseId { get; set; }
public string AccountId { get; set; }
public string PurchaseNo { get; set; }
public string Price1 { get; set; }
public string Encode { get; set; }
public string Title { get; set; }
}
public class ProductInfo
{
public string Name { get; set; }
public string Encode { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}