C# PrintDocument类

一、  PrintDocument类。

打印类PrintDocument类是包含在System.Drawing.Printing名词空间下的,这个类是用来与打印机进行交流传输使用的,它有专门的属性用来指定应该选择哪个打印机来打印,还有属性设置当前打印的纸张默认设置。

使用方式:PrintDocument pdt = new PrintDocument();

二、  PrintDocument类属性。

类型 属性 方法 说明
PrinterSittings PrinterSittings 读/写 获取或设置对文档要进行的打印机。
PageSettings DefaultPageSettings 读/写 获取或设置打印页设置,这些页设置用做要打印的所有页设置。
以上的两个属性都是以类对象的形式被封装到PrintDocument类中,这些属性要想使用都必须通过new操作符重new出对象来,但是光要这两个属性还是不够的,我们还要使用其他的类来与这两个类进行配合学习。

例:PrintDocument pdt = new PrintDocument();

      pdt.PrinterSettings = new PrinterSettings();

     pdt.DefaultPageSettings = new PageSettings();

三、  PrintDocument类事件。

事件 方法 委托 说明
PrintPage OnPrintPage

PrintPageEventHandler
当需要为当前页打印输出时发生。
这个事件是等待用户指定打印时发生的,其实这个事件非常好用,打印时就跟往窗体上绘制一样,获得Graphics信息,在使用这个类下的DrawString往页面上绘制就可以了。

四、  PageSettings类。

这个类是专门用来设置单页打印信息的,这个信息还得获取一下pdt.DefaultPageSettings这个属性。

例:PageSettings ps = pdt.DefaultPageSettings;

当我们获取完这个之后就可以使用Print()方法进行打印了。

例:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Hty
{
    class MyForm : Form
    { 
        PrintDocument pdt;
        static void Main()
        {
            Application.Run(new MyForm());
        }
 
        public MyForm()
        {
            this.Text = "打印";
            Button bt = new Button();
            bt.Parent = this;
            bt.Location = new Point(ClientSize.Width/2-bt.Width/2,ClientSize.Height/2-bt.Height/2);
            bt.Text = "打印";
            bt.Click += new EventHandler(bt_Click);
        }
 
        void bt_Click(object sender, EventArgs e)
        {
            pdt = new PrintDocument();
            pdt.PrinterSettings = new PrinterSettings();
            pdt.DefaultPageSettings = new PageSettings();
            PageSettings page = pdt.DefaultPageSettings;
            pdt.PrintPage += new PrintPageEventHandler(pdt_PrintPage);
            pdt.Print();
        }
 
        void pdt_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics grfx = e.Graphics; 
            grfx.DrawString("aa", new Font("宋体", 20), Brushes.Black, 0, 0);
        }
    }
}
通过以上的例子我们发现,当我们使用打印的时候跟我们以往在其他的软件中使用的打印有很大的区别,区别在于他们都有可选择界面而我们没有这样的界面支持,其实我们也可以做到的微软已经为我们准备好了几个现成的类了,叫做打印对话框类。

五、  PrintPreviewDialog类。

这个类是微软专门制作的打印预览对话框类,如果要想使用这个类就要使用这个类下的Document属性与我们的pdt对象相关联才可以。

例:PrintPreviewDialog ppd = new PrintPreviewDialog();
       ppd.Document = pdt;
      ppd.ShowDialog();
注意:当我们使用了这个类时,我们就不能在使用pdt下的Print打印执行方法了。
例:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Hty
{
    class MyForm : Form
    { 
        PrintDocument pdt;
        static void Main()
        {
            Application.Run(new MyForm());
        }
        public MyForm()
        {
            this.Text = "打印";
            Button bt = new Button();
            bt.Parent = this;
            bt.Location = new Point(ClientSize.Width/2-bt.Width/2,ClientSize.Height/2-bt.Height/2);
            bt.Text = "打印";
            bt.Click += new EventHandler(bt_Click);
        }
 
        void bt_Click(object sender, EventArgs e)
        {
            pdt = new PrintDocument();
            pdt.PrinterSettings = new PrinterSettings();
            pdt.DefaultPageSettings = new PageSettings();
            PageSettings page = pdt.DefaultPageSettings;
            pdt.PrintPage += new PrintPageEventHandler(pdt_PrintPage);
            //pdt.Print();
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            ppd.Document = pdt;
            ppd.ShowDialog();
        }
        void pdt_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics grfx = e.Graphics;
            grfx.DrawString("aa", new Font("宋体", 20), Brushes.Black, 0, 0);
        }
    }
}




你可能感兴趣的:(打印,C#)