PrintDocument控件(收藏)



PrintDocument控件设置打印的文档。PrintDocument控件中比较常见的是控件的PrintPage事件和Print方法。PrintPage事件在需要为当前页打印的输出时发生。调用Print方法开始文档的打印进程。下面通过实例演示如何使用PrintDocument控件。
例  创建一个Windows应用程序,向窗体中添加一个Button控件、一个PrintDocument控件和一个PrintPreviewDialog控件。在PrintDocument控件的PrintPage事件中绘制打印的内容,然后在Button按钮的Click事件下设置PrintPreviewDialog的属性预览打印文档,并调用PrintDocument控件Print方法开始文档的打印进程,代码如下。

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//通过GDI+绘制打印文档
e.Graphics.DrawString("蝶恋花", new Font("宋体", 15), Brushes.Black, 350, 80);
e.Graphics.DrawLine(
new Pen(Color.Black, (float)3.00), 100, 185, 720, 185);
e.Graphics.DrawString(
"伫倚危楼风细细,望极春愁,黯黯生天际。", new Font("宋体", 12), Brushes.Black, 110, 195);
e.Graphics.DrawString(
"草色烟光残照里,无言谁会凭阑意。", new Font("宋体", 12), Brushes.Black, 110, 220);
e.Graphics.DrawString(
"拟把疏狂图一醉,对酒当歌,强乐还无味。", new Font("宋体", 12), Brushes.Black, 110, 245);
e.Graphics.DrawString(
"衣带渐宽终不悔。为伊消得人憔悴。", new Font("宋体", 12), Brushes.Black, 110, 270);
e.Graphics.DrawLine(
new Pen(Color.Black, (float)3.00), 100, 300, 720, 300);

}

点击按钮时事件

 
private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否要预览打印文档", "打印预览", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//开启操作系统的防锯齿功能
this.printPreviewDialog1.UseAntiAlias = true;
//设置要预览的文档
this.printPreviewDialog1.Document = this.printDocument1;
//打开预览窗口
printPreviewDialog1.ShowDialog();
}
else
{
//调用Print方法直接打印文档
//this.printDocument1.Print();
}

}

 

 

 

运行程序,单击“打印”按钮,弹出“打印预览”窗体,如图所示。




 

原文地址:https://www.cnblogs.com/wangsaiming/p/2065665.html