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