GDI+画打印内容
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
DataGridViewRow dr = this.dataGridView1.SelectedRows[0];
Graphics g = e.Graphics;//获得绘图对象
g.PageUnit = GraphicsUnit.Display;
float yPosition = 0; //绘制字符串的纵向位置
float xp = MeasureStringWidth("用户名称:");
float leftMargin = e.MarginBounds.Left + tx / 10 * 39; //左边距
float topMargin = e.MarginBounds.Top + ty / 10 * 39; //上边距
float p1 = leftMargin + xp + xp / 3.5f;
float p2 = p1 + 5.5f * 39;
float p3 = p2 + 5.5f * 39;
Font printFont = new Font(fontName, float.Parse(fontSize));//当前的打印字体
//Font printFont = new Font("宋体", 9);
SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
//line 1
yPosition = topMargin;
float noPosition=leftMargin + e.MarginBounds.Width - MeasureStringWidth("No:5090426001") + 39 * 1;
g.DrawString("NO:" + dr.Cells[0].Value.ToString(), printFont, myBrush, noPosition, topMargin - 39 * 0.1f, new StringFormat());
//line 2
yPosition = topMargin + (0.8f * printFont.GetHeight(g));
float userPosition=leftMargin + xp - 39 * 0.3f;
float year=leftMargin + xp * 2 + 1.3f * 39;
float month=year + xp / 4 + 39;
float day=month + 39 * 1;
float userNum = leftMargin + xp * 2 + 3.3f * 39 + xp / 4 + 39 * 1.5f + xp * 2;
DateTime ticketDate=Convert.ToDateTime(dr.Cells["开票日期"].Value.ToString());
g.DrawString(dr.Cells["用户"].Value.ToString(), printFont, myBrush, userPosition, yPosition, new StringFormat());
g.DrawString(ticketDate.Year.ToString(), printFont, myBrush, year, yPosition, new StringFormat());
g.DrawString(ticketDate.Month.ToString(), printFont, myBrush, month, yPosition, new StringFormat());
g.DrawString(ticketDate.Day.ToString(), printFont, myBrush, day, yPosition, new StringFormat());
g.DrawString(dr.Cells["用户编号"].Value.ToString(), printFont, myBrush, userNum, yPosition, new StringFormat());
//line 3
yPosition = topMargin + (3 * printFont.GetHeight(g));
g.DrawString(dr.Cells["支口"].Value.ToString(), printFont, myBrush, p1, yPosition, new StringFormat());
g.DrawString(dr.Cells["开启尺度"].Value.ToString(), printFont, myBrush, p2, yPosition, new StringFormat());
g.DrawString(dr.Cells["时流量"].Value.ToString(), printFont, myBrush, p3,yPosition, new StringFormat());
//line 4
yPosition = topMargin + (5.5f * printFont.GetHeight(g));
g.DrawString(dr.Cells["放水量"].Value.ToString(), printFont, myBrush, p1, yPosition, new StringFormat());
g.DrawString(dr.Cells["开启时间"].Value.ToString(), printFont, myBrush, p2-39*0.2f, yPosition, new StringFormat());
g.DrawString(dr.Cells["关闭时间"].Value.ToString(), printFont, myBrush, p3-39*0.3f, yPosition, new StringFormat());
//line 5
yPosition = topMargin + (8.0f * printFont.GetHeight(g));
g.DrawString(dr.Cells["灌溉面积"].Value.ToString(), printFont, myBrush, p1, yPosition, new StringFormat());
g.DrawString(dr.Cells["水价"].Value.ToString(), printFont, myBrush, p2, yPosition, new StringFormat());
DateTime sDate = Convert.ToDateTime(dr.Cells["开启时间"].Value.ToString());
DateTime eDate = Convert.ToDateTime(dr.Cells["关闭时间"].Value.ToString());
//TimeSpan span = eDate - sDate;
//string totalhour = span.Days * 24 + span.Hours.ToString()+span.Minutes;
//-----xxx201075162325修改----------
TimeSpan ts1 = new TimeSpan(sDate.Ticks);
TimeSpan ts2 = new TimeSpan(eDate.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
string totalhour = string.Format("{0:00}", ts.Days * 24 + ts.Hours) + ":" + string.Format("{0:00}", ts.Minutes) + ":" + string.Format("{0:00}", ts.Seconds); ;
//-------------------------------------
g.DrawString(totalhour.ToString(), printFont, myBrush, p3, yPosition, new StringFormat());
//line 6
yPosition = topMargin + (10.5f * printFont.GetHeight(g));
g.DrawString(dr.Cells["水价类型"].Value.ToString(), printFont, myBrush, p1, yPosition, new StringFormat());
g.DrawString(dr.Cells["水费"].Value.ToString(), printFont, myBrush, p2, yPosition, new StringFormat());
string ticketState = "";//水票状态
try
{
string tid = dr.Cells[0].Value.ToString().Trim();
string[] strs = BusinessUserBLL.GetWaterTicketCheckResult(tid);
if (strs[0] == "True")
{
if (strs[1] == "True")
{
ticketState = "审核通过!";
}
}
else
{
ticketState = "确认";
}
}
catch
{ }
g.DrawString(ticketState, printFont, myBrush, p3, yPosition-3.9f*0.3f, new StringFormat());
yPosition = topMargin + (12.7f * printFont.GetHeight(g));
g.DrawString(FrmMain.sessionuser.Uname, printFont, myBrush, p1 + 39 * 4.4f, yPosition, new StringFormat());
g.DrawString(BusinessUserBLL.CheckPerson(dr.Cells[0].Value.ToString()), printFont, myBrush, leftMargin + xp - 39 * 0.6f, yPosition, new StringFormat());
}