usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing.Printing;namespaceDemo{publicpartialclass printother:form{ PublicPrintother () {InitializeComponent ();}//The PrintDocument class is the core of the printing function that encapsulates the printing-related properties, events, and methodsPrintDocument PrintDocument =NewPrintDocument ();p rivatevoid Btnprint_click (Objectsender, EventArgs e) {//printdocument.printersettings can get or set computer default printing related properties or parameters, such as: PrintDocument.PrinterSettings.PrinterName get the default printer printer name//printdocument.defaultpagesettings//can get or set print page parameter information, such as paper size, whether landscape printing, etc.//Set document namePrintdocument.documentname ="Prescription Notes";//Display in the Print dialog box and queue (document is displayed by default) after Setup//Set the paper size (can not be set to the default settings)PaperSize PS =NewPaperSize ("Your Paper Name", -, -);p S. Rawkind= Max;//If the custom paper is more than 118, (the A4 value is 9, the detailed paper type and the value of the comparison, see http://Msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind (v=vs.85). aspx)PrintDocument.DefaultPageSettings.PaperSize =PS;//before printing startsPrintdocument.beginprint + =NewPrinteventhandler (printdocument_beginprint);//print output (process)Printdocument.printpage + =NewPrintpageeventhandler (printdocument_printpage);//Print EndPrintdocument.endprint + =NewPrinteventhandler (printdocument_endprint);//Jump out of the Print dialog box to provide visual settings for printing parameters, such as choosing which printer to print this documentPrintDialog PD =NewPrintDialog ();pd. Document=PrintDocument;if(DialogResult.OK = = PD. ShowDialog ())//if confirmed, all print parameter settings will be overwritten{//Page Setup dialog (can not be used, in fact PrintDialog dialog box has provided page Setup)PageSetupDialog PSD =NewPageSetupDialog ();p SD. Document=PrintDocument;if(DialogResult.OK = =PSD. ShowDialog ()) {//Print PreviewPrintPreviewDialog PPD =NewPrintPreviewDialog ();ppd. Document=PrintDocument;if(DialogResult.OK = =PPD. ShowDialog ()) Printdocument.print (); //Print}}}voidPrintdocument_beginprint (Objectsender, PrintEventArgs e) {//You can also put some of the printed parameters here to set}voidPrintdocument_printpage (Objectsender, PrintPageEventArgs e) {//Print What, it's written here.Graphics g =E.graphics; Brush b=NewSolidBrush (Color.Black); Font Titlefont=NewFont ("Song Body", -);stringtitle ="Prescriptions for Community health service stations"; g.drawstring (title, Titlefont, B,NewPointF (e.pagebounds.width-g.measurestring (title, Titlefont). Width)/2, -));//E.cancel//Gets or sets whether to cancel printing//e.hasmorepages//true, the function is executed again after execution (available for dynamic paging)}voidPrintdocument_endprint (Objectsender, PrintEventArgs e) {//related actions after printing finishes}}}