欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#打印类PrintDocument、PrintDialog、PrintPreviewDialog使用示例

程序员文章站 2023-01-11 08:22:32
1.使用printdocument进行打印 using system; using system.drawing; using system.drawing....

1.使用printdocument进行打印

using system;
using system.drawing;
using system.drawing.printing;
using system.windows.forms;
 
namespace printtest
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
 
    private void button1_click(object sender, eventargs e)
    {
      //实例化打印对象
      printdocument printdocument1 = new printdocument();
      //设置打印用的纸张,当设置为custom的时候,可以自定义纸张的大小
      printdocument1.defaultpagesettings.papersize = new papersize("custum", 500, 500);
      //注册printpage事件,打印每一页时会触发该事件
      printdocument1.printpage += new printpageeventhandler(this.printdocument_printpage);
      //开始打印
      printdocument1.print();
    }
    private void printdocument_printpage(object sender, system.drawing.printing.printpageeventargs e)
    {
      //设置打印内容及其字体,颜色和位置
      e.graphics.drawstring("hello world!", new font(new fontfamily("黑体"), 24), system.drawing.brushes.red, 50, 50);
    }
  }
}

2.使用printdialog增加打印对话框

using system;
using system.drawing;
using system.drawing.printing;
using system.windows.forms;
 
namespace printtest
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
 
    private void button1_click(object sender, eventargs e)
    {
      //实例化打印对象
      printdocument printdocument1 = new printdocument();
      //设置打印用的纸张,当设置为custom的时候,可以自定义纸张的大小
      printdocument1.defaultpagesettings.papersize = new papersize("custum", 500, 500);
      //注册printpage事件,打印每一页时会触发该事件
      printdocument1.printpage += new printpageeventhandler(this.printdocument_printpage);
   
      //初始化打印对话框对象
      printdialog printdialog1 = new printdialog();
      //将printdialog.useexdialog属性设置为true,才可显示出打印对话框
      printdialog1.useexdialog = true;
      //将printdocument1对象赋值给打印对话框的document属性
      printdialog1.document = printdocument1;
      //打开打印对话框
      dialogresult result = printdialog1.showdialog();
      if (result == dialogresult.ok)         
        printdocument1.print();//开始打印
    }
    private void printdocument_printpage(object sender, system.drawing.printing.printpageeventargs e)
    {
      //设置打印内容及其字体,颜色和位置
      e.graphics.drawstring("hello world!", new font(new fontfamily("黑体"), 24), system.drawing.brushes.red, 50, 50);
    }
  }
}

打印对话框如下图所示。

C#打印类PrintDocument、PrintDialog、PrintPreviewDialog使用示例

3.使用printpreviewdialog增加打印预览对话框

using system;
using system.drawing;
using system.drawing.printing;
using system.windows.forms;
 
namespace printtest
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
 
    private void button1_click(object sender, eventargs e)
    {
      //实例化打印对象
      printdocument printdocument1 = new printdocument();
      //设置打印用的纸张,当设置为custom的时候,可以自定义纸张的大小
      printdocument1.defaultpagesettings.papersize = new papersize("custum", 500, 500);
      //注册printpage事件,打印每一页时会触发该事件
      printdocument1.printpage += new printpageeventhandler(this.printdocument_printpage);
   
      //初始化打印预览对话框对象
      printpreviewdialog printpreviewdialog1 = new printpreviewdialog();
      //将printdocument1对象赋值给打印预览对话框的document属性
      printpreviewdialog1.document = printdocument1;
      //打开打印预览对话框
      dialogresult result = printpreviewdialog1.showdialog();
      if (result == dialogresult.ok)         
        printdocument1.print();//开始打印
    }
    private void printdocument_printpage(object sender, system.drawing.printing.printpageeventargs e)
    {
      //设置打印内容及其字体,颜色和位置
      e.graphics.drawstring("hello world!", new font(new fontfamily("黑体"), 24), system.drawing.brushes.red, 50, 50);
    }
  }
}

打印时,会显示下图所示预览画面。

C#打印类PrintDocument、PrintDialog、PrintPreviewDialog使用示例

注意:printdialog与printpreviewdialog位于名称空间system.windows.forms(程序集为system.windows.forms.dll)中,而printdocument位于名称空间system.drawing.printing(程序集为system.drawing.dll)中。