private void printDocument_PrintPage(object sender, PrintPageEventArgs ev)
{測試
Font titleFont = new Font("宋體", 9, FontStyle.Bold);//標題字體 字體
Font fntTxt = new Font("宋體", 9, FontStyle.Regular);//正文文字 ui
Brush brush = new SolidBrush(Color.Black);//畫刷 this
Pen pen = new Pen(Color.Black); //線條顏色 orm
Point po = new Point(10, 10);事件
try
{ci
ev.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po); //DrawString方式進行打印。 string
}it
catch (Exception ex)
{io
MessageBox.Show(this, "打印出錯!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
///GetPrintSw方法用來構造打印文本,內部StringBuilder.AppendLine在Drawstring時單獨佔有一行。
public StringBuilder GetPrintSW()
{
StringBuilder sb = new StringBuilder();
string tou = "測試管理公司名稱";
string address = "河南洛陽";
string saleID = "2010930233330"; //單號
string item = "項目";
decimal price = 25.00M;
int count = 5;
decimal total = 0.00M;
decimal fukuan = 500.00M;
sb.AppendLine(" " + tou + " \n");
sb.AppendLine("-----------------------------------------");
sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "單號:" + saleID);
sb.AppendLine("-----------------------------------------");
sb.AppendLine("項目" + " " + "數量" + " " + "單價" + " " + "小計");
for (int i = 0; i < count; i++)
{
decimal xiaoji = (i + 1) * price;
sb.AppendLine(item + (i + 1) + " " + (i + 1) + " " + price + " " + xiaoji);
total += xiaoji;
}
sb.AppendLine("-----------------------------------------");
sb.AppendLine("數量:" + count + " 合計: " + total);
sb.AppendLine("付款:" + fukuan);
sb.AppendLine("現金找零:" + (fukuan - total));
sb.AppendLine("-----------------------------------------");
sb.AppendLine("地址:" + address + "");
sb.AppendLine("電話:123456789 123456789");
sb.AppendLine("謝謝惠顧歡迎下次光臨 ");
sb.AppendLine("-----------------------------------------");
return sb;
}
觸發事件
private void btnPrint_Click(object sender, EventArgs e)
{
pd.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); //打印頁面需指定相應的PrintDocument_PrintPrintPage事件委託
pd.Print();
}