#region 打印
private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//int x = 100;
///*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11)中的“黑体”改成自己要的字体就行了,黑体 后面的数字代表字体的大小
// System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色,后面的为输出的位置 */
//e.Graphics.DrawString("新乡市三月软件公司入库单", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 170, 10);
//e.Graphics.DrawString("供货商:河南科技学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Blue, 10, 12);
////信息的名称
//e.Graphics.DrawLine(Pens.Black, 8, 30, 520, 30);
//e.Graphics.DrawString("序号", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 9, 35);
//e.Graphics.DrawString("入库编号", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 75, 35);
//e.Graphics.DrawString("商品名称", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 230, 35);
//e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 330, 35);
//e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 405, 35);
//e.Graphics.DrawString("总金额", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 470, 35);
//e.Graphics.DrawLine(Pens.Black, 8, 50, 520, 50);
////产品信息
//double value = 100;
//int num = 0;
//for (int i = 1; i <= strData.Count-1 ; i++)
//{
// e.Graphics.DrawString(i.ToString(), new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 9, 55+ num);
// e.Graphics.DrawString("R2011-01-2016:06:35", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 75, 55 + num);
// e.Graphics.DrawString(strData[i], new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 230 , 55 + num);
// e.Graphics.DrawString("100", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 330, 55 + num);
// e.Graphics.DrawString("200.00", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 405, 55 + num);
// e.Graphics.DrawString(value.ToString(), new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 470, 55 + num);
// num += 20;
// value = Convert.ToDouble(value);
//}
//e.Graphics.DrawString("总金额:"+value.ToString(), new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black,400, 70 + num);
//e.Graphics.DrawLine(Pens.Black, 8, 200+num, 520, 200+num);
//e.Graphics.DrawString("收货地址:新乡市河南科技学院信息工程学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 210 + num);
//e.Graphics.DrawString("收货人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 230 + num);
//e.Graphics.DrawString("电话号码:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 250 + num);
//e.Graphics.DrawString("经办人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 270 + num);
//e.Graphics.DrawString("服务热线:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 290 + num);
//e.Graphics.DrawString("出库时间:" + DateTime.Now.ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 310 + num);
private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//int x = 100;
///*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11)中的“黑体”改成自己要的字体就行了,黑体 后面的数字代表字体的大小
// System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色,后面的为输出的位置 */
//e.Graphics.DrawString("新乡市三月软件公司入库单", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 170, 10);
//e.Graphics.DrawString("供货商:河南科技学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Blue, 10, 12);
////信息的名称
//e.Graphics.DrawLine(Pens.Black, 8, 30, 520, 30);
//e.Graphics.DrawString("序号", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 9, 35);
//e.Graphics.DrawString("入库编号", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 75, 35);
//e.Graphics.DrawString("商品名称", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 230, 35);
//e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 330, 35);
//e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 405, 35);
//e.Graphics.DrawString("总金额", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 470, 35);
//e.Graphics.DrawLine(Pens.Black, 8, 50, 520, 50);
////产品信息
//double value = 100;
//int num = 0;
//for (int i = 1; i <= strData.Count-1 ; i++)
//{
// e.Graphics.DrawString(i.ToString(), new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 9, 55+ num);
// e.Graphics.DrawString("R2011-01-2016:06:35", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 75, 55 + num);
// e.Graphics.DrawString(strData[i], new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 230 , 55 + num);
// e.Graphics.DrawString("100", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 330, 55 + num);
// e.Graphics.DrawString("200.00", new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 405, 55 + num);
// e.Graphics.DrawString(value.ToString(), new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 470, 55 + num);
// num += 20;
// value = Convert.ToDouble(value);
//}
//e.Graphics.DrawString("总金额:"+value.ToString(), new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black,400, 70 + num);
//e.Graphics.DrawLine(Pens.Black, 8, 200+num, 520, 200+num);
//e.Graphics.DrawString("收货地址:新乡市河南科技学院信息工程学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 210 + num);
//e.Graphics.DrawString("收货人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 230 + num);
//e.Graphics.DrawString("电话号码:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 250 + num);
//e.Graphics.DrawString("经办人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 270 + num);
//e.Graphics.DrawString("服务热线:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 290 + num);
//e.Graphics.DrawString("出库时间:" + DateTime.Now.ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 15, 310 + num);
//第一条竖线开始30,100
//第2条竖线坐标开始130,100
//第三条竖线开始230,linebegin,结束230,lineend
//第四条竖线开始340,linebegin,结束340,lineend
//第五条竖线开始460,linebegin,结束460,lineend
//第六条竖线开始670,linebegin,结束670,lineend
//最后一条竖线开始790,100
int y = 50;
int linebegin = 0;
int lineend = 0;
int end = 0;
Font font = new Font("宋体", 10, FontStyle.Regular);
Pen title_pen = new Pen(Color.Black, 1);
e.Graphics.DrawString("专家抽取列表", new Font("宋体", 20, FontStyle.Bold), Brushes.Black, 270, y);
y += 80;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第一条横线
e.Graphics.DrawString("项目名称", font, Brushes.Black, (20 + (90 - GetWidth("项目名称", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("1", font, Brushes.Black, (110 + (570 - GetWidth("1", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第二条横线
e.Graphics.DrawString("招标单位", font, Brushes.Black, (20 + (90 - GetWidth("招标单位", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("2", font, Brushes.Black, (110 + (570 - GetWidth("2", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第三条横线
e.Graphics.DrawString("抽取时间", font, Brushes.Black, (20 + (90 - GetWidth("抽取时间", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("3", font, Brushes.Black, (110 + (570 - GetWidth("3", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第四条横线
//第2条竖线坐标开始130,100
//第三条竖线开始230,linebegin,结束230,lineend
//第四条竖线开始340,linebegin,结束340,lineend
//第五条竖线开始460,linebegin,结束460,lineend
//第六条竖线开始670,linebegin,结束670,lineend
//最后一条竖线开始790,100
int y = 50;
int linebegin = 0;
int lineend = 0;
int end = 0;
Font font = new Font("宋体", 10, FontStyle.Regular);
Pen title_pen = new Pen(Color.Black, 1);
e.Graphics.DrawString("专家抽取列表", new Font("宋体", 20, FontStyle.Bold), Brushes.Black, 270, y);
y += 80;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第一条横线
e.Graphics.DrawString("项目名称", font, Brushes.Black, (20 + (90 - GetWidth("项目名称", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("1", font, Brushes.Black, (110 + (570 - GetWidth("1", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第二条横线
e.Graphics.DrawString("招标单位", font, Brushes.Black, (20 + (90 - GetWidth("招标单位", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("2", font, Brushes.Black, (110 + (570 - GetWidth("2", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第三条横线
e.Graphics.DrawString("抽取时间", font, Brushes.Black, (20 + (90 - GetWidth("抽取时间", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("3", font, Brushes.Black, (110 + (570 - GetWidth("3", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第四条横线
linebegin = y;
//开始画列名
e.Graphics.DrawString("专业", font, Brushes.Black, (20 + (90 - GetWidth("专业", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("姓名", font, Brushes.Black, (110 + (70 - GetWidth("姓名", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("固定电话", font, Brushes.Black, (180 + (90 - GetWidth("固定电话", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("手机", font, Brushes.Black, (270 + (110 - GetWidth("手机", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("工作单位", font, Brushes.Black, (380 + (200 - GetWidth("工作单位", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("职称", font, Brushes.Black, (580 + (110 - GetWidth("职称", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第五条横线
//开始画table数据
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
e.Graphics.DrawString(dr.Cells[1].Value.ToString(), font, Brushes.Black, (20 + (90 - GetWidth(dr.Cells[1].Value.ToString(), font)) / 2 - 5), y + 10);
e.Graphics.DrawString(dr.Cells[2].Value.ToString(), font, Brushes.Black, (110 + (70 - GetWidth(dr.Cells[2].Value.ToString(), font)) / 2 - 5), y + 10);
int phone = GetWidth(dr.Cells[3].Value.ToString(), font);
if (phone <= 90)
{
e.Graphics.DrawString(dr.Cells[3].Value.ToString(), font, Brushes.Black, (180 + (90 - phone) / 2 - 5), y + 10);
}
else
{
string _phone = dr.Cells[3].Value.ToString();
_phone = _phone.Substring(0, 9) + "…";
phone = GetWidth(_phone, font);
e.Graphics.DrawString(_phone, font, Brushes.Black, (180 + (90 - phone) / 2 - 5), y + 10);
}
int mobile = GetWidth(dr.Cells[4].Value.ToString(), font);
if (mobile <= 100)
{
e.Graphics.DrawString(dr.Cells[4].Value.ToString(), font, Brushes.Black, (270 + (110 - mobile) / 2 - 5), y + 10);
}
else
{
string _mobile = dr.Cells[4].Value.ToString();
_mobile = _mobile.Substring(0, 11) + "…";
phone = GetWidth(_mobile, font);
e.Graphics.DrawString(_mobile, font, Brushes.Black, (270 + (110 - phone) / 2 - 5), y + 10);
}
e.Graphics.DrawString(dr.Cells[5].Value.ToString(), font, Brushes.Black, (380 + (200 - GetWidth(dr.Cells[5].Value.ToString(), font)) / 2 - 5), y + 10);
e.Graphics.DrawString(dr.Cells[6].Value.ToString(), font, Brushes.Black, (580 + (110 - GetWidth(dr.Cells[6].Value.ToString(), font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);
}
lineend = y;
e.Graphics.DrawString("抽取人签字", font, Brushes.Black, (20 + (90 - GetWidth("抽取人签字", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);
e.Graphics.DrawString("监督人签字", font, Brushes.Black, (20 + (90 - GetWidth("监督人签字", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);
end = y;
//开始画列名
e.Graphics.DrawString("专业", font, Brushes.Black, (20 + (90 - GetWidth("专业", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("姓名", font, Brushes.Black, (110 + (70 - GetWidth("姓名", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("固定电话", font, Brushes.Black, (180 + (90 - GetWidth("固定电话", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("手机", font, Brushes.Black, (270 + (110 - GetWidth("手机", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("工作单位", font, Brushes.Black, (380 + (200 - GetWidth("工作单位", font)) / 2 - 5), y + 10);
e.Graphics.DrawString("职称", font, Brushes.Black, (580 + (110 - GetWidth("职称", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第五条横线
//开始画table数据
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
e.Graphics.DrawString(dr.Cells[1].Value.ToString(), font, Brushes.Black, (20 + (90 - GetWidth(dr.Cells[1].Value.ToString(), font)) / 2 - 5), y + 10);
e.Graphics.DrawString(dr.Cells[2].Value.ToString(), font, Brushes.Black, (110 + (70 - GetWidth(dr.Cells[2].Value.ToString(), font)) / 2 - 5), y + 10);
int phone = GetWidth(dr.Cells[3].Value.ToString(), font);
if (phone <= 90)
{
e.Graphics.DrawString(dr.Cells[3].Value.ToString(), font, Brushes.Black, (180 + (90 - phone) / 2 - 5), y + 10);
}
else
{
string _phone = dr.Cells[3].Value.ToString();
_phone = _phone.Substring(0, 9) + "…";
phone = GetWidth(_phone, font);
e.Graphics.DrawString(_phone, font, Brushes.Black, (180 + (90 - phone) / 2 - 5), y + 10);
}
int mobile = GetWidth(dr.Cells[4].Value.ToString(), font);
if (mobile <= 100)
{
e.Graphics.DrawString(dr.Cells[4].Value.ToString(), font, Brushes.Black, (270 + (110 - mobile) / 2 - 5), y + 10);
}
else
{
string _mobile = dr.Cells[4].Value.ToString();
_mobile = _mobile.Substring(0, 11) + "…";
phone = GetWidth(_mobile, font);
e.Graphics.DrawString(_mobile, font, Brushes.Black, (270 + (110 - phone) / 2 - 5), y + 10);
}
e.Graphics.DrawString(dr.Cells[5].Value.ToString(), font, Brushes.Black, (380 + (200 - GetWidth(dr.Cells[5].Value.ToString(), font)) / 2 - 5), y + 10);
e.Graphics.DrawString(dr.Cells[6].Value.ToString(), font, Brushes.Black, (580 + (110 - GetWidth(dr.Cells[6].Value.ToString(), font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);
}
lineend = y;
e.Graphics.DrawString("抽取人签字", font, Brushes.Black, (20 + (90 - GetWidth("抽取人签字", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);
e.Graphics.DrawString("监督人签字", font, Brushes.Black, (20 + (90 - GetWidth("监督人签字", font)) / 2 - 5), y + 10);
y += 40;
e.Graphics.DrawLine(title_pen, 20, y, 690, y);
end = y;
//开始画竖线
//e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
//e.Graphics.DrawLine(title_pen, 130, 130, 130, y);
//e.Graphics.DrawLine(title_pen, 210, linebegin, 210, lineend);
//e.Graphics.DrawLine(title_pen, 320, linebegin, 320, lineend);
//e.Graphics.DrawLine(title_pen, 440, linebegin, 440, lineend);
//e.Graphics.DrawLine(title_pen, 670, linebegin, 670, lineend);
//e.Graphics.DrawLine(title_pen, 800, 130, 800, y);
e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
e.Graphics.DrawLine(title_pen, 110, 130, 110, y);
e.Graphics.DrawLine(title_pen, 180, linebegin, 180, lineend);
e.Graphics.DrawLine(title_pen, 270, linebegin, 270, lineend);
e.Graphics.DrawLine(title_pen, 380, linebegin, 380, lineend);
e.Graphics.DrawLine(title_pen, 580, linebegin, 580, lineend);
e.Graphics.DrawLine(title_pen, 690, 130, 690, y);
}
//e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
//e.Graphics.DrawLine(title_pen, 130, 130, 130, y);
//e.Graphics.DrawLine(title_pen, 210, linebegin, 210, lineend);
//e.Graphics.DrawLine(title_pen, 320, linebegin, 320, lineend);
//e.Graphics.DrawLine(title_pen, 440, linebegin, 440, lineend);
//e.Graphics.DrawLine(title_pen, 670, linebegin, 670, lineend);
//e.Graphics.DrawLine(title_pen, 800, 130, 800, y);
e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
e.Graphics.DrawLine(title_pen, 110, 130, 110, y);
e.Graphics.DrawLine(title_pen, 180, linebegin, 180, lineend);
e.Graphics.DrawLine(title_pen, 270, linebegin, 270, lineend);
e.Graphics.DrawLine(title_pen, 380, linebegin, 380, lineend);
e.Graphics.DrawLine(title_pen, 580, linebegin, 580, lineend);
e.Graphics.DrawLine(title_pen, 690, 130, 690, y);
}
SpeechSynthesizer speak = new SpeechSynthesizer();
List<string> strData = new List<string>();
private void button2_Click(object sender, EventArgs e)
{
//// printDocument1 为 打印控件
////设置打印用的纸张 当设置为Custom的时候,可以自定义纸张的大小,还可以选择A4,A5等常用纸型
//this.MyPrintDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", 500, 300);
//this.MyPrintDocument.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);
////将写好的格式给打印预览控件以便预览
//printPreviewDialog1.Document = MyPrintDocument;
////printPreviewDialog1.PrintPreviewControl.Zoom = 3.0;
////显示打印预览
//DialogResult result = printPreviewDialog1.ShowDialog();
////if (result == DialogResult.OK)
//// this.MyPrintDocument.Print();
List<string> strData = new List<string>();
private void button2_Click(object sender, EventArgs e)
{
//// printDocument1 为 打印控件
////设置打印用的纸张 当设置为Custom的时候,可以自定义纸张的大小,还可以选择A4,A5等常用纸型
//this.MyPrintDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", 500, 300);
//this.MyPrintDocument.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);
////将写好的格式给打印预览控件以便预览
//printPreviewDialog1.Document = MyPrintDocument;
////printPreviewDialog1.PrintPreviewControl.Zoom = 3.0;
////显示打印预览
//DialogResult result = printPreviewDialog1.ShowDialog();
////if (result == DialogResult.OK)
//// this.MyPrintDocument.Print();
printDialog1.ShowDialog(); //设置打印文档
printPreviewDialog1.Document = this.MyPrintDocument;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ClientSize = new Size(800, 800);
printPreviewDialog1.ShowDialog();
printPreviewDialog1.Document = this.MyPrintDocument;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ClientSize = new Size(800, 800);
printPreviewDialog1.ShowDialog();
}
#endregion
#endregion
private int GetWidth(string str, Font myf)
{
Graphics g = Graphics.FromHwnd(this.Handle);
{
Graphics g = Graphics.FromHwnd(this.Handle);
StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
SizeF size = g.MeasureString(str, myf, 1000, sf);
return Convert.ToInt32(size.Width);
}
来源:https://www.cnblogs.com/ygcxy/p/12332165.html