. NET Common techniques _c# printing (PrintDocument, PrintDialog, PageSetupDialog, PrintPreviewDialog)

Source: Internet
Author: User

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}}}

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.