如何列出系統中的印表機,以及指定印表機來做列印
這篇筆記一下小技巧,如果在軟體中,想要列印東西到指定的印表機上,可以利用PrinterSettings.PrinterName的屬性來做,很簡單的就可以指定要使用的印表機,那麼如果要知道系統中所有已經安裝的印表機名稱,則可以用PrinterSettings.InstalledPrinters來做,下面是簡單的測試程式碼
using System; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { PrintDocument printer; public Form1() { InitializeComponent(); printer = new PrintDocument(); printer.PrintPage += printer_PrintPage; foreach (var item in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { cboPrint.Items.Add(item); } } void printer_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString("12345", new Font("Arial", 14), Brushes.Black, 10, 10); e.HasMorePages = false; } private void btnShowIP_Click(object sender, EventArgs e) { printer.PrinterSettings.PrinterName = cboPrint.Text; printer.Print(); } } }
留言