1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Drawing.Printing; 7 using System.Linq; 8 using System.Management; 9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 namespace PrintDocument控件 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 /// <summary> 23 /// PrintDocument:用於打印最重要的類,幾乎所有的打印類都與這個類有關系。 24 /// </summary> 25 26 // 1、打印頁面設置 27 private void btnSetting_Click(object sender, EventArgs e) 28 { 29 // 打印頁面設置對話框 30 PageSetupDialog page = new PageSetupDialog(); 31 32 page.Document = printDocument1; 33 page.AllowMargins = true; 34 page.AllowOrientation = true; 35 page.AllowPaper = true; 36 page.AllowPrinter = true; 37 page.ShowHelp = true; 38 if (page.ShowDialog() == DialogResult.OK) 39 { 40 // 將設置好的打印頁 用作 PrintDocument進行打印。 41 printDocument1.DefaultPageSettings = page.PageSettings; 42 } 43 } 44 45 // 2、打印機設置 46 private void btnPrint_Click(object sender, EventArgs e) 47 { 48 // 打印機設置對話框 49 PrintDialog print = new PrintDialog(); 50 51 print.Document = printDocument1; 52 print.AllowCurrentPage = true; 53 print.AllowPrintToFile = true; 54 print.AllowSelection = true; 55 print.AllowSomePages = true; 56 print.ShowHelp = true; 57 if (print.ShowDialog() == DialogResult.OK) 58 { 59 // 將設置好的打印機 用作 PrinDocument進行打印。 60 printDocument1.PrinterSettings = print.PrinterSettings; 61 } 62 } 63 64 // 3、打印預覽 65 private void btnPreview_Click(object sender, EventArgs e) 66 { 67 if (MessageBox.Show("是否要預覽打印文件?", "打印預覽", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK) 68 { 69 // 設置要預覽的文檔 70 printPreviewDialog1.Document = printDocument1; 71 // 開啟操作系統的抗鋸齒功能 72 printPreviewDialog1.UseAntiAlias = true; 73 74 // 打開預覽窗口 75 if (printPreviewDialog1.ShowDialog() == DialogResult.OK) 76 { 77 // 如果選擇的是系統默認打印機,點擊“打印”按鈕之后,會跳出“文件另存為”窗口; 78 // 如果選擇別的打印機,點擊“打印”之后,會直接打印,不會返回“OK”。 79 MessageBox.Show("開始打印"); 80 } 81 else 82 { 83 MessageBox.Show("關閉預覽"); 84 } 85 } 86 } 87 88 // 4、開始打印 89 private void btnStart_Click(object sender, EventArgs e) 90 { 91 // PrintController:控制一個PrintDocument是如何打印的。 92 PrintController printController = new StandardPrintController(); 93 printDocument1.PrintController = printController; 94 printDocument1.DocumentName = "社保樣卡"; 95 printDocument1.PrinterSettings.PrinterName = "XID8600 U1"; 96 printDocument1.Print(); // 觸發Print_Page事件。 97 } 98 99 100 // PrintDocument 三個事件中的第二個參數 e 有如下屬性: 101 // e.Cancel:設置為true,將取消這次打印作業。 102 // e.Griphics:所使用打印機的設備環境。 103 // e.HasMorePages:PrintPage事件打印一頁后,如果仍有數據未打印,退出事件前設置 104 // HasMorePages=true;退出事件之后將再次出發PrintPage事件,打印下一頁。 105 // e.MarginBounds:打印區域的大小,是Rectangle結構,元素包括左上角坐標(Left和Top), 106 // 寬和高(Width和Height),單位為1/100英寸。 107 // e.PageSettings:PageSettings類對象,包含用對話框PageSetupDialog設置的頁面打印方式的 108 // 全部信息, 109 110 // 在調用 Print 方法后,在打印文檔的第一頁之前發生。 111 private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 112 { 113 } 114 115 // 需要打印新的一頁時發生,負責打印一頁所需要的數據 116 // 打印預覽會調用該事件,預覽的內容就是此處設置好的內容。 117 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 118 { 119 Image iamge = Image.FromFile(@"D:\photo\test.jpg"); 120 121 e.Graphics.DrawString("社保卡樣卡", new Font("黑體", 35), Brushes.Black, new Point(400, 120)); 122 123 e.Graphics.DrawString("姓名 張三", new Font("黑體", 25), Brushes.Black, new Point(480, 270)); 124 e.Graphics.DrawString("社會保障號碼 32032032302030230", new Font("黑體", 25), Brushes.Black, new Point(480, 360)); 125 e.Graphics.DrawString("社會保障卡號 JS2018098", new Font("黑體", 25), Brushes.Black, new Point(480, 450)); 126 e.Graphics.DrawString("制卡日期 2016年5月", new Font("黑體", 25), Brushes.Black, new Point(480, 540)); 127 e.Graphics.DrawImage(iamge, new Point(100, 240)); 128 } 129 130 // 在打印完最后一頁文檔時發生。 131 private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 132 { 133 } 134 135 /// <summary> 136 /// 打印機狀態值 137 /// </summary> 138 public enum PrinterStatus 139 { 140 其他狀態 = 1, 141 未知, 142 空閑, 143 正在打印, 144 預熱, 145 停止打印, 146 打印中, 147 離線 148 } 149 150 // 獲取指定打印機的狀態 151 private void btnState_Click(object sender, EventArgs e) 152 { 153 string strPrinter = "win32_printer.DeviceId='XID8300 U1'"; 154 155 // 用指定的打印機實例化一個打印機對象。 156 ManagementObject printer = new ManagementObject(strPrinter); 157 158 // 獲取打印機信息。 159 printer.Get(); 160 161 // 獲取打印機狀態屬性 162 string str = printer.Properties["PrinterStatus"].Value.ToString(); 163 textBox1.Text = str; 164 } 165 166 // 獲取本地所有打印機信息 167 private void button1_Click(object sender, EventArgs e) 168 { 169 string strPrinter = "win32_printer"; 170 171 // 獲取本地所有打印機 172 ManagementClass mc = new ManagementClass(strPrinter); 173 174 // 獲取所有打印機實例的集合 175 ManagementObjectCollection moc = mc.GetInstances(); 176 177 // 遍歷本地所有打印機 178 foreach (ManagementObject printer in moc) 179 { 180 // 打印機名字 181 string strName = printer.Properties["DeviceId"].Value.ToString(); 182 // 打印機狀態 183 string strState = printer.Properties["PrinterStatus"].Value.ToString(); 184 comboBox1.Items.Add(strName); 185 } 186 } 187 } 188 }