Printdocument,printdialog and PrintPreviewDialog

Source: Internet
Author: User
Tags print object

1. Use PrintDocument for printing

Using system;using system.drawing;using system.drawing.printing;using System.Windows.Forms; namespace printtest{public partial class Form1:form {public Form1 () {Initializecompone        NT (); private void Button1_Click (object sender, EventArgs e) {//Instantiate the Print object PrintDocument PR            IntDocument1 = new PrintDocument ();  Set the paper for printing, when set to custom, you can customize the size of the paper printDocument1.DefaultPageSettings.PaperSize = new PaperSize ("Custum", 500,            500); Registers the PrintPage event, which triggers the event when each page is printed printdocument1.printpage + = new Printpageeventhandler (this.            Printdocument_printpage);        Start printing printdocument1.print ();            private void Printdocument_printpage (object sender, System.Drawing.Printing.PrintPageEventArgs e) { Set the print content and its font, color, and position e.graphics.drawstring ("Hello world!        ", New Font (New FontFamily (" bold "), System.Drawing.Brushes.Red, 50, 50);   } }} 

2. Use PrintDialog to add a print dialog box

Using system;using system.drawing;using system.drawing.printing;using System.Windows.Forms; namespace printtest{public partial class Form1:form {public Form1 () {Initializecompone        NT (); private void Button1_Click (object sender, EventArgs e) {//Instantiate the Print object PrintDocument PR            IntDocument1 = new PrintDocument ();  Set the paper for printing, when set to custom, you can customize the size of the paper printDocument1.DefaultPageSettings.PaperSize = new PaperSize ("Custum", 500,            500); Registers the PrintPage event, which triggers the event when each page is printed printdocument1.printpage + = new Printpageeventhandler (this.                  Printdocument_printpage);            Initialize the Print Dialog object PrintDialog printDialog1 = new PrintDialog ();            Set the Printdialog.useexdialog property to True to display the print dialog box Printdialog1.useexdialog = true;            Assigns the PrintDocument1 object to the Document property of the print dialog box printdialog1.document = printDocument1; Open the Print dialog box DialogResult result = Printdialog1.showdialog (); if (result = = DialogResult.OK) printdocument1.print ();//start printing} private void Printdocument_printpage (object sender, System.Drawing.Printing.PrintPageEventArgs e) {//Set the print content and its font, color, and bit Place E.graphics.drawstring ("Hello world!        ", New Font (New FontFamily (" bold "), System.Drawing.Brushes.Red, 50, 50); }    }}

The Print dialog box looks like this.

3 Add Print Preview dialog box using PrintPreviewDialog

Using system;using system.drawing;using system.drawing.printing;using System.Windows.Forms; namespace printtest{public partial class Form1:form {public Form1 () {Initializecompone        NT (); private void Button1_Click (object sender, EventArgs e) {//Instantiate the Print object PrintDocument PR            IntDocument1 = new PrintDocument ();  Set the paper for printing, when set to custom, you can customize the size of the paper printDocument1.DefaultPageSettings.PaperSize = new PaperSize ("Custum", 500,            500); Registers the PrintPage event, which triggers the event when each page is printed printdocument1.printpage + = new Printpageeventhandler (this.                  Printdocument_printpage);            Initializes the Print Preview dialog object PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ();            Assigns the PrintDocument1 object to the Document property of the Print preview dialog box Printpreviewdialog1.document = printDocument1;            Opens the Print Preview dialog box DialogResult result = Printpreviewdialog1.showdialog (); if (result = = DialOgresult.ok) Printdocument1.print ();//start printing} private void Printdocument_prin Tpage (object sender, System.Drawing.Printing.PrintPageEventArgs e) {//Set the print content and its font, color, and location E.gra Phics. DrawString ("Hello world!        ", New Font (New FontFamily (" bold "), System.Drawing.Brushes.Red, 50, 50); }    }}

When you print, the displayed preview screen appears.

Note: PrintDialog and PrintPreviewDialog are located in the namespace System.Windows.Forms (assembly is System.Windows.Forms.dll), and PrintDocument is located in the namespace System.dra Wing. Printing (assembly is System.Drawing.dll).

Printdocument,printdialog and PrintPreviewDialog

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.