How to send something to a printer from a console application or a WinForms application?
1 answer
Minimally required for printing:
using System.Drawing.Printing; using System.Drawing; void Print() { PrintDocument printDoc = new PrintDocument(); printDoc.PrintPage += PrintPageHandler; printDoc.Print(); } void PrintPageHandler(object sender, PrintPageEventArgs e) { //Замените на e.Graphics.DrawImage или любую другую логику e.Graphics.DrawString("Привет", new Font("Arial", 14), Brushes.Black, 0, 0); } at the same time the print will be on the default printer. PrintDialog allows PrintDialog to select a printer and configure some PrintDocument settings through a dialog box, but PrintDocument is responsible for printing.
Expand the example to use PrintDialog :
PrintDocument printDoc = new PrintDocument(); printDoc.PrintPage += PrintPageHandler; PrintDialog printDialog = new PrintDialog(); printDialog.Document = printDoc; if (printDialog.ShowDialog() == DialogResult.OK) printDialog.Document.Print(); We remove pop-up windows if they are not necessary:
printDoc.PrintController = new StandardPrintController(); Similar to adding a PrintDialog , you can add a PageSetupDialog and PrintPreviewDialog successively passing them a PrintDocument object.
|