上面代碼中 ,核心的是下面這個事件,調用pd_PrintPage方法:
//打印
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
printDocument.Print ();
pd_PrintPage方法:
//打印A4紙用的變量#region //打印A4紙用的變量
DataSet dsPrintInfo; //存放要打印的信息,資料從DB中獲取,表一位SN信息,表二為WO信息
private int lineNo; //存放當前要打印行的行號
private int lineQty; //存放總 共要打印的行數,可以是一個估算值,略大於實際行數
private int printingPageNo = 0; //當前 打印的頁號
#endregion
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
string strLine;//用於存放當前行打印的信息
float leftMargin = (e.MarginBounds.Left) * 3 / 4; //左邊距
float topMargin = e.MarginBounds.Top * 2 / 3; //頂邊距
float verticalPosition = topMargin; //初始化垂 直位置,設為頂邊距
Font mainFont = new Font("CourIEr New", 10);//打印的字 體
//每頁的行數,當打印行數超過這個時,要換頁(1.05這個值是根據實際情況中設定的,可 以不要)
int linesPerPage = (int)(e.MarginBounds.Height * 1.05 / mainFont.GetHeight (e.Graphics));
//Format of this DocReport#region //Format of this DocReport
if (printingPageNo == 0) //打印第一頁時,需要打印以下頭信息
{
mainFont = new Font("CourIEr New", 24, FontStyle.Bold);
strLine = String.Format("{0,21}", "FQA Report");
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight (e.Graphics);
mainFont = new Font("CourIEr New", 16, FontStyle.Bold);
strLine = "Pallet ID:" + dsPrintInfo.Tables[0].Rows [0]["Pallet_ID"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("3 of 9 Barcode", 24, FontStyle.Bold);
strLine = "*" + dsPrintInfo.Tables[0].Rows[0]["Pallet_ID"].ToString() + "*";
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin * 4, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font ("CourIEr New", 16, FontStyle.Bold);
strLine = "QCI_PN:" + dsPrintInfo.Tables[0].Rows[0]["PARTNUMBER"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight (e.Graphics);
strLine = "Date :" + DateTime.Now;
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight (e.Graphics);
strLine = "Line :" + dsPrintInfo.Tables[0].Rows[0] ["Mfg_Line"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
strLine = "Model:" + dsPrintInfo.Tables[0].Rows[0]["Model"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight (e.Graphics) * 2;
//linesPerPage:上面的頭信息預計占的行數,因此第一頁還能打印 (linesPerPage - 15)行
linesPerPage = linesPerPage - 15;
//lineQty:lineQty行數大概是表行數加上15
lineQty = dsPrintInfo.Tables [0].Rows.Count + 15;
}
#endregion
//打印具體的SN,BoxID,PalletID列 表#region //打印具體的SN,BoxID,PalletID列表
//打印標題頭信息
mainFont = new Font("CourIEr New", 10);
strLine = String.Format("{0,-10}", "NO#") +
String.Format("{0,-25}", "System S/N") +
String.Format("{0,-25}", "BOX_ID") +
String.Format("{0,-25}", "Pallet_ID");
e.Graphics.DrawString (strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
//打印一 條橫線
mainFont = new Font("CourIEr New", 3);
e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
//打印記錄集信息
int count = 0; //當前頁 的行數計數
mainFont = new Font("CourIEr New", 10);
//如果(當前頁 行計數器)小於(每頁可以打印的行數)且(要打印行的行號)小於(總共要打印的行數)
while (count < linesPerPage && lineNo < this.lineQty)
{
if (lineNo < dsPrintInfo.Tables[0].Rows.Count) //由於lineNo用於表中的下標值,因此需要加上該 判斷
{
string strLineTemp = String.Format("{0,-10}", dsPrintInfo.Tables[0].Rows[lineNo]["NO"].ToString()) +
String.Format("{0,-25}", dsPrintInfo.Tables[0].Rows[lineNo] ["SN"].ToString()) +
String.Format("{0,-25}", dsPrintInfo.Tables[0].Rows[lineNo]["BOX_ID"].ToString()) +
String.Format("{0,-25}", dsPrintInfo.Tables[0].Rows[lineNo] ["Pallet_ID"].ToString());
e.Graphics.DrawString(strLineTemp, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
count++; //注意:這句要放到If中,否則,如果實際只有一頁的話,尾信息會跑到第二頁去
}
lineNo++;
}
#endregion
//如果總行數大於目前實 際行號,表明還有頁要打,lineQty是估計數,因此實際用時要仔細估算
if (lineQty > lineNo)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
//打印結尾信息#region //打印結尾 信息
if (!e.HasMorePages)
{
mainFont = new Font("CourIEr New", 10);
e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
foreach (DataRow dr in dsPrintInfo.Tables[1].Rows)
{
mainFont = new Font ("CourIEr New", 16);
strLine = String.Format("{0,-3}", "WO:") + String.Format("{0,-16}",dr["WO"].ToString())
+ String.Format("{0,-7}", "Qty:") + String.Format(" {0,-10}",dr["Qty"].ToString());
e.Graphics.DrawString (strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
}
foreach (DataRow dr in dsPrintInfo.Tables[2].Rows)
{
mainFont = new Font("CourIEr New", 16);
strLine = String.Format("{0,-9}", "TotalQty:") + String.Format("{0,-10} ", dr["TotalQty"].ToString())
+ String.Format("{0, -7}", "BoxQty:") + String.Format("{0,-10}",dr ["BoxQty"].ToString());
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
}
}
#endregion
printingPageNo++; //頁號加一
}