private void Form1_Load(object sender, EventArgs e) { //See if any printers are installed if (PrinterSettings.InstalledPrinters.Count <= 0) { MessageBox.Show("Printer not found!"); return; } //Get all available printers and add them to the combo box foreach (String printer in PrinterSettings.InstalledPrinters) { comboBox1.Items.Add(printer.ToString()); } } private void button1_Click(object sender, EventArgs e) { //Create a PrintDocument object PrintDocument pd = new PrintDocument(); //Set PrinterName as the selected printer in the printers list pd.PrinterSettings.PrinterName = comboBox1.SelectedItem.ToString(); //Add PrintPage event handler pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); //Print the document pd.Print(); } //The PrintPage event handler public void pd_PrintPage(object sender, PrintPageEventArgs ev) { //Get the Graphics object Graphics g = ev.Graphics; //Create a font Arial with size 16 Font font = new Font("Arial", 16); //Create a solid brush with black color SolidBrush brush = new SolidBrush(Color.Black); //Draw "Hello Printer!"; g.DrawString("Hello Printer!", font, brush, new Rectangle(20, 20, 200, 100)); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)