您好,登錄后才能下訂單哦!
本篇內容主要講解“C#怎么調用打印機實現打印”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C#怎么調用打印機實現打印”吧!
#region BarcodeStandard.dll /* * * 使用說明 需要通過NuGet進行安裝BarcodeLib.dll,必不可少 */ string inputString; /// <summary> /// 獲取所以打印機驅動名稱 /// </summary> private void getPrintDocumentlist() { PrintDocument print = new PrintDocument(); string sDefault = print.PrinterSettings.PrinterName;//默認打印機名 comboBox_drive.Items.Add(sDefault); comboBox_drive.Text = sDefault;//顯示默認驅動名稱 foreach (string sPrint in PrinterSettings.InstalledPrinters)//獲取所有打印機名稱 { if (sPrint != sDefault) { comboBox_drive.Items.Add(sPrint); } } } /// <summary> /// 打印繪制 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { Font titleFont = new Font("宋體", 9, FontStyle.Bold);//標題字體 Font fntTxt = new Font("宋體", 9, FontStyle.Regular);//正文文字 Brush brush = new SolidBrush(Color.Black);//畫刷 Pen pen = new Pen(Color.Black); //線條顏色 Point po = new Point(10, 10); try { //畫String e.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po);//打印內容 //畫橫線 //Point[] point = { new Point(20, 50), new Point(200, 50) };//縱坐標不變 //e.Graphics.DrawLines(pen, point); //畫豎線 //Point[] points1 = { new Point(60, 70), new Point(60, 70 + 40) };//橫坐標不變 //e.Graphics.DrawLines(pen, points1); //畫矩形 //e.Graphics.DrawRectangle(pen, 20, 70, 90, 90); } catch (Exception ex) { MessageBox.Show(this, "打印出錯!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 獲取打印內容 /// </summary> /// <returns></returns> public StringBuilder GetPrintSW() { StringBuilder sb = new StringBuilder(); string tou = "XXXXXX科技有限公司"; string address = "安徽省合肥市瑤海區"; string saleID = "100010000001"; //單號 string item = "項目"; decimal price = 25.00M; int count = 5; decimal total = 0.00M; decimal fukuan = 500.00M; sb.AppendLine(" " + tou + " \n"); sb.AppendLine("-----------------------------------------"); sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "單號:" + saleID); sb.AppendLine("-----------------------------------------"); sb.AppendLine("項目" + " " + "數量" + " " + "單價" + " " + "小計"); for (int i = 0; i < count; i++) { decimal xiaoji = (i + 1) * price; sb.AppendLine(item + (i + 1) + " " + (i + 1) + " " + price + " " + xiaoji); total += xiaoji; } sb.AppendLine("-----------------------------------------"); sb.AppendLine("數量:" + count + " 合計: " + total); sb.AppendLine("付款:" + fukuan); sb.AppendLine("現金找零:" + (fukuan - total)); sb.AppendLine("-----------------------------------------"); sb.AppendLine("地址:" + address + ""); sb.AppendLine("電話:130000000000"); sb.AppendLine("謝謝惠顧歡迎下次光臨!"); sb.AppendLine("-----------------------------------------"); return sb; } /// <summary> /// 生成條形碼 /// </summary> /// <param name="content">內容</param> /// <returns></returns> public static Image GenerateBarCodeBitmap(string content) { using (var barcode = new Barcode() { IncludeLabel = true, Alignment = AlignmentPositions.CENTER, Width = 250, Height = 100, RotateFlipType = RotateFlipType.RotateNoneFlipNone, BackColor = Color.White, ForeColor = Color.Black, }) { return barcode.Encode(TYPE.CODE128B, content); } } #endregion
#region Seagull.BarTender.Print.dll /// <summary> /// 打印測試 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printbt_Click(object sender, EventArgs e) { string qd = comboBox_drive.Text;//下拉列表選擇的驅動名稱 var printDocument = new PrintDocument(); //指定打印機 printDocument.PrinterSettings.PrinterName = qd;//驅動名稱 printDocument.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); try { //打印預覽 //PrintPreviewDialog ppd = new PrintPreviewDialog(); //ppd.Document = printDocument; //ppd.ShowDialog(); //打印 printDocument.Print(); } catch (InvalidPrinterException) { } finally { printDocument.Dispose(); } } /// <summary> /// BarTender打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BarTender_Click(object sender, EventArgs e) { try { //程序中寫入引用 using Seagull.BarTender.Print.dll,必不可少; //安裝Bartender后,在安裝的根目錄或者system32下課可找到對應的dll #region Engine btEngine = new Engine(); btEngine.Start(); string lj = AppDomain.CurrentDomain.BaseDirectory + "test.btw"; //test.btw是BT的模板 LabelFormatDocument btFormat = btEngine.Documents.Open(lj); //對BTW模版相應字段進行賦值 btFormat.SubStrings["name"].Value ="Liming"; btFormat.SubStrings["code"].Value = "1234567890"; //指定打印機名 btFormat.PrintSetup.PrinterName = "WPS 虛擬打印機"; //改變標簽打印數份連載 btFormat.PrintSetup.NumberOfSerializedLabels = 1; //打印份數 btFormat.PrintSetup.IdenticalCopiesOfLabel = 1; Messages messages; int waitout = 10000; // 10秒 超時 Result nResult1 = btFormat.Print("標簽打印軟件", waitout, out messages); btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession; //不保存對打開模板的修改 btFormat.Close(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges); //結束打印引擎 btEngine.Stop(); #endregion } catch (Exception ex) { MessageBox.Show("錯誤信息: " + ex.Message); return; } } #endregion
#region Interop.LabelManager2.dll /// <summary> /// 打印功能 CodeSoft /// </summary> /// <param name="PrintParam1">打印模板參數值1</param> /// <param name="PrintParam2">打印模板參數值2</param> /// <param name="PrintParam3">打印模板參數值3</param> /// <param name="PrintParam4">打印模板參數值4</param> /// <returns></returns> public bool SoftCodePrint(string PrintParam1 = "", string PrintParam2 = "", string PrintParam3 = "", string PrintParam4 = "") { bool result = false; int printNum = 2;//打印份數 try { string text = string.Empty; ApplicationClass labApp = null; Document doc = null; string labFileName = AppDomain.CurrentDomain.BaseDirectory + "Template\\" + "Test.Lab";//模板地址 if (!File.Exists(labFileName)) { throw new Exception("沒有找到標簽模版"); } for (int i = 0; i < printNum; i++) { labApp = new ApplicationClass(); labApp.Documents.Open(labFileName, false);// 調用設計好的label文件 doc = labApp.ActiveDocument; //可通過配置檔進行配置打印信息 doc.Variables.FreeVariables.Item("模板變量名稱1").Value = PrintParam1; doc.Variables.FreeVariables.Item("模板變量名稱2").Value = PrintParam2; doc.Variables.FreeVariables.Item("模板變量名稱3").Value = PrintParam3; doc.Variables.FreeVariables.Item("模板變量名稱4").Value = PrintParam4; doc.PrintDocument(1); } labApp.Quit(); result = true; } catch (Exception ex) { } return result; } #endregion
到此,相信大家對“C#怎么調用打印機實現打印”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。