using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Text; using System.Windows.Forms; namespace ComponentSurveySample { public partial class PrintingForm : Form { public PrintingForm() { InitializeComponent(); } private void printDocumentButton_Click(object sender, EventArgs e) { // Print this document to default printer this.printDocument.Print(); } Font printerFont; private void printDocument_BeginPrint(object sender, PrintEventArgs e) { // Create font for printing this.printerFont = new Font("Lucida Console", 72); } private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { // Draw to the e.Graphics object that wraps the print target Graphics g = e.Graphics; g.DrawString("Hello,\nPrinter", this.printerFont, Brushes.Black, 0, 0); } private void printDocument_EndPrint(object sender, PrintEventArgs e) { // Reclaim font this.printerFont.Dispose(); this.printerFont = null; } // Use the PageSetupDialog private void pageSetupDialogButton_Click(object sender, EventArgs e) { this.pageSetupDialog.Document = this.printDocument; this.pageSetupDialog.ShowDialog(); } // Use the PrintPreviewDialog private void printPreviewDialogButton_Click(object sender, EventArgs e) { this.printPreviewDialog.Document = this.printDocument; this.printPreviewDialog.ShowDialog(); } // Use the PrintDialog private void printDialogButton_Click(object sender, EventArgs e) { // Set the print document this.printDialog.Document = this.printDocument; // Allow the user to set the printer if( this.printDialog.ShowDialog() == DialogResult.OK ) { this.printDocument.Print(); } } } }