C # PrintDocument class

Source: Internet
Author: User

I. PrintDocument class.

The PrintDocument class is included in the System. drawing. printing is used for communication and transmission with printers. It has special attributes to specify which printer should be selected for Printing, you can also set the default settings for the printed paper.


Usage: PrintDocument pdt = new PrintDocument ();

Ii. PrintDocument class attributes.


Description of type attributes
Printersit.pdf read/write get or set the printer to be performed on the document.
PageSettings defadefapagesettings read/write get or set print page settings. These page settings are used for all page settings to be printed.
The above two attributes are encapsulated into the PrintDocument class in the form of class objects. To use these attributes, you must use the new operator to reproduce the new object, however, these two attributes are not enough. We also need to use other classes to work with these two classes.


For example, PrintDocument pdt = new PrintDocument ();


Pdt. PrinterSettings = new PrinterSettings ();

Pdt. DefaultPageSettings = new PageSettings ();

3. PrintDocument events.


Event method delegation description
PrintPage OnPrintPage

PrintPageEventHandler occurs when you need to print the output for the current page.
This event occurs when the user specifies to print it. In fact, this event is very useful. It is the same as drawing it on the form to obtain Graphics information, you can use the DrawString in this class to draw on the page.


4. PageSettings class.


This class is used to set the print information on a single page. You need to obtain the attributes of pdt. DefaultPageSettings.


Example: PageSettings ps = pdt. DefaultPageSettings;


After obtaining this information, we can Print it using the Print () method.


Example:

 

[Csharp]
Using System;
Using System. Drawing;
Using System. Windows. Forms;
Using System. Drawing. Printing;
Namespace Hty
{
Class MyForm: Form
{
PrintDocument pdt;
Static void Main ()
{

Using System;
Using System. Drawing;
Using System. Windows. Forms;
Using System. Drawing. Printing;
Namespace Hty
{
Class MyForm: Form
{
PrintDocument pdt;
Static void Main ()
{[Csharp] view plaincopyprint? Application. Run (new MyForm ());
}

Public MyForm ()
{
This. Text = "print ";
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 = "print ";
Bt. Click + = new EventHandler (bt_Click );
}

Void bt_Click (object sender, EventArgs e)
{
Pdt = new PrintDocument ();
Pdt. PrinterSettings = new PrinterSettings ();
Pdt. DefaultPageSettings = new PageSettings ();
Pagesetaskpage = 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 );
}
}
}

Application. Run (new MyForm ());
}
 
Public MyForm ()
{
This. Text = "print ";
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 = "print ";
Bt. Click + = new EventHandler (bt_Click );
}
 
Void bt_Click (object sender, EventArgs e)
{
Pdt = new PrintDocument ();
Pdt. PrinterSettings = new PrinterSettings ();
Pdt. DefaultPageSettings = new PageSettings ();
Pagesetaskpage = 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 );
}
}
} Through the above examples, we found that printing is very different from printing in other software, the difference is that they all have selectable interfaces, but we do not have such interface support. In fact, Microsoft has already prepared several ready-made classes for us, called the Print dialog box class.

5. PrintPreviewDialog class.

This class is a print preview dialog box class specially prepared by Microsoft. If you want to use this class, you must use the Document attribute under this class to associate it with our pdt object.

Example: PrintPreviewDialog ppd = new PrintPreviewDialog ();
Ppd. Document = pdt;
Ppd. ShowDialog ();
Note: When we use this class, we cannot use the Print execution method under pdt.
Example:


[Csharp]
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 = "print ";
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 = "print ";
Bt. Click + = new EventHandler (bt_Click );
}

Void bt_Click (object sender, EventArgs e)
{
Pdt = new PrintDocument ();
Pdt. PrinterSettings = new PrinterSettings ();
Pdt. DefaultPageSettings = new PageSettings ();
Pagesetaskpage = 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 );
}
}
}

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 = "print ";
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 = "print ";
Bt. Click + = new EventHandler (bt_Click );
}
 
Void bt_Click (object sender, EventArgs e)
{
Pdt = new PrintDocument ();
Pdt. PrinterSettings = new PrinterSettings ();
Pdt. DefaultPageSettings = new PageSettings ();
Pagesetaskpage = 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 );
}
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.