请问如何用GDI+将其画成表格,并将文字居中
Graphics g = e.Graphics;int linesPerPage = 2; //页面的行号
float yPosition = 0; //绘制字符串的纵向位置
int count = 0; //行计数器
float leftMargin = e.MarginBounds.Left; //左边距
float topMargin = e.MarginBounds.Top; //上边距
Font printFont = new Font("宋体", 10);
SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
Graphics jx=this.CreateGraphics();
Pen mypen = new Pen(Color.Black, 3);
while (count < linesPerPage && datagridviewRowIndex < dataGridView1.Rows.Count)
{
string s1 = string.Empty, s2 = string.Empty, s3 = string.Empty, s4 = string.Empty, s5 = string.Empty;
string s6 = string.Empty, s7 = string.Empty;//你有几个列这里就应该对应几个字段
if (count.Equals(0))
{
//这一部分是每页的列头和大标题
s1 = "物资类别";
s2 = "编号";
s3 = "入库编号";
s4 = "日期";
s5 = "材料名称";
s6 = "型号规格";
s7 = "数量";
//...
yPosition = topMargin + 5;
g.DrawString("入库信息", printFont, myBrush, leftMargin + 300, yPosition, new StringFormat());//打印大标题
}
else
{
//这部分是每一页的内容
s1 = dataGridView1.Rows[datagridviewRowIndex].Cells[1].Value.ToString();
s2 = dataGridView1.Rows[datagridviewRowIndex].Cells[2].Value.ToString();
s3 = dataGridView1.Rows[datagridviewRowIndex].Cells[3].Value.ToString();
s4 = dataGridView1.Rows[datagridviewRowIndex].Cells[4].Value.ToString();
s5 = dataGridView1.Rows[datagridviewRowIndex].Cells[5].Value.ToString();
s6 = dataGridView1.Rows[datagridviewRowIndex].Cells[6].Value.ToString();
s7 = dataGridView1.Rows[datagridviewRowIndex].Cells[7].Value.ToString();
datagridviewRowIndex++;
}
yPosition = topMargin + (count * printFont.GetHeight(g)) + 50;
//下面是打印数据或者列头
g.DrawString(s1, printFont, myBrush, leftMargin +70, yPosition, new StringFormat());
g.DrawString(s2, printFont, myBrush, leftMargin + 140, yPosition, new StringFormat());
g.DrawString(s3, printFont, myBrush, leftMargin + 210, yPosition, new StringFormat());
g.DrawString(s4, printFont, myBrush, leftMargin + 280, yPosition, new StringFormat());
g.DrawString(s5, printFont, myBrush, leftMargin + 350, yPosition, new StringFormat());
g.DrawString(s6, printFont, myBrush, leftMargin + 420, yPosition, new StringFormat());
g.DrawString(s7, printFont, myBrush, leftMargin + 490, yPosition, new StringFormat());
count++;
}
if (datagridviewRowIndex < dataGridView1.Rows.Count)
e.HasMorePages = true;
else
e.HasMorePages = false;