2013年(92)
分类: 信息化
2013-04-26 05:35:46
实习开发进程中经常会遇到打印某种报表的情况,用C#结束打印报表的功用。第一,如果报表的大小适合,或许纸张的大小满意放得下报表,则可以选择直接截屏,打印截屏所得的图画;第二,如果报表和纸张的大小不匹配,则可以需要在程序中根据一定格式拼出适合大小的报表。 截屏结束打印报表 private void button3_Click(object sender, EventArgs e)
{
// 截屏
Form form = this;
if (form != null)
{
Graphics mygraphics = form.CreateGraphics();
memoryImage = new Bitmap(form.Width, form.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, form.Size);
}
// 设置打印文档名(如果运用Adobe PDF或许Microsoft Office Document Image Writer打印,则作为默许输出文件名)
this.printDocument1.DocumentName = this.label27.Text;
this.printDocument1.Print();
}
// printDocument1的PrintPage工作
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.DrawImage(memoryImage, 0, 0);
}
//定义一个图画
private Bitmap memoryImage = null;其间button3即为打印按钮,打印按钮的监听工作配备请自行结束(可直接在VS的button3的“特色”页中设置)。printDocument1为PrintDocument空间,请自行添加;其PrintPage工作的监听配备请自行结束。 聚集特定格式的报表打印 private void button3_Click(object sender, EventArgs e)
{
this.printDocument1.DocumentName = this.label27.Text;
this.printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.Clear(Color.White);
// 初步制造文档
// 默许为横排文字
e.Graphics.DrawString("标题",?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new Font(new FontFamily("宋体"), 22, FontStyle.Bold),?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Drawing.Brushes.Black,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 170, 10);
e.Graphics.DrawString("性别:" this.comboBox1.Text,
new Font(new FontFamily("宋体"), 14, FontStyle.Bold),
System.Drawing.Brushes.Black,
20, 50);
e.Graphics.DrawString("联系电话:" this.textBox9.Text,
new Font(new FontFamily("宋体"), 14, FontStyle.Bold),
System.Drawing.Brushes.Black,
350, 50);
e.Graphics.DrawString("地址:" this.textBox11.Text,
new Font(new FontFamily("宋体"), 14, FontStyle.Bold),
System.Drawing.Brushes.Black,
20, 80);
// 横线
e.Graphics.DrawLine(Pens.Black, 20, 110, 800, 110);
// 竖排文字
e.Graphics.DrawString("内容",
new Font(new FontFamily("宋体"), 14, FontStyle.Bold),
System.Drawing.Brushes.Black,
20, 120,
new StringFormat(StringFormatFlags.DirectionVertical));
}