C#怎么调用打印机实现打印
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本篇内容主要讲解"C#怎么调用打印机实现打印",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#怎么调用打印机实现打印"吧!一、引用BarcodeStand
千家信息网最后更新 2025年11月07日C#怎么调用打印机实现打印
本篇内容主要讲解"C#怎么调用打印机实现打印",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#怎么调用打印机实现打印"吧!
一、引用BarcodeStandard.dll
#region BarcodeStandard.dll /* * * 使用说明 需要通过NuGet进行安装BarcodeLib.dll,必不可少 */ string inputString; ////// 获取所以打印机驱动名称 /// 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); } } } ////// 打印绘制 /// /// /// 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); } } ////// 获取打印内容 /// ///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; } /// /// 生成条形码 /// /// 内容 ///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
二、引用Seagull.BarTender.Print.dll
#region Seagull.BarTender.Print.dll ////// 打印测试 /// /// /// 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(); } } ////// BarTender打印 /// /// /// 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
三、引用 Interop.LabelManager2.dll
#region Interop.LabelManager2.dll ////// 打印功能 CodeSoft /// /// 打印模板参数值1 /// 打印模板参数值2 /// 打印模板参数值3 /// 打印模板参数值4 ///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#怎么调用打印机实现打印"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
模板
打印机
名称
内容
参数
变量
驱动
C#
标签
份数
信息
单号
地址
宋体
必不可少
数量
模版
项目
学习
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
怎样判断数据库主键
找不到后端服务器战地2042
云服务器安装宝塔教程华为
hp塔式服务器维修保养
大二网络安全
医学文献王数据库
嘉聚宝软件开发专营店
语音标注数据库兼职
crsed游戏服务器在哪里
服务器神游中
网络安全态势总结
数据库4个范式
电脑显示服务器未登录是怎么回事
杭州电脑软件开发费用
蛋白质分子量 数据库
qq公开数据库查询系统
网络安全教育现场活动
大创软件开发申请书
计算机网络技术大专毕业论文选题
asp数据库 源码
网络存储服务器搭建图
实习报告计算机网络技术
软件开发两种策略的优劣
科蓝数据库是亚洲国家唯一
常州创新软件开发答疑解惑
80端口 服务器
美国专利数据库检索
天津软件开发公司厂家报价
psv如何重置数据库
东城系统软件开发