当前位置: 首页 > news >正文

C#调用barTender打印标签示例

使用的电脑需要先安装BarTender 

 

 

 我封装成一个类

using System;
using System.Windows.Forms;namespace FT_Tools
{public class SysContext{public static BarTender.Application btapp = new BarTender.Application();public static BarTender.Format btFormat;public void Quit(){SysContext.btapp.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出时同步退出bartender进程}private static bool btPrint_Test(string modelPath,string txt,string barcode ){try{btFormat = btapp.Formats.Open(modelPath, false, ""); //System.Windows.Forms.Application.StartupPath + @"\1.btw"btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//打印份数btFormat.PrintSetup.NumberSerializedLabels = 1;//序列标签数btFormat.SetNamedSubStringValue("打印文本", txt.Trim());btFormat.SetNamedSubStringValue("打印条码", barcode.Trim());btFormat.PrintOut(true, false);//第二个false设置打印时是否跳出打印属性btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出时是否保存标签return true;}catch (Exception ex){MessageBox.Show(ex.Message);return false;}}}}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace BarTenderDemo
{public partial class Form1 : Form{private BarTender.Application btAPP;private BarTender.Format btFormat;//SELECT MaterielId,ProductBigPackId,NetWeight,GrossWeight,num //FROM Make_ProductBigPack mpb//LEFT JOIN Make_TaskInfo mti ON mpb.TaskId=mti.TaskId//WHERE ProductBigPackId=''public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){btAPP = new BarTender.Application();}private void btPrint_Click(object sender, EventArgs e){try{btFormat = btAPP.Formats.Open(System.Windows.Forms.Application.StartupPath+@"\1.btw", false, "");btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//打印份数btFormat.PrintSetup.NumberSerializedLabels = 1;//序列标签数btFormat.SetNamedSubStringValue("打印文本", txtMemo.Text.Trim());btFormat.SetNamedSubStringValue("打印条码", txtBarcode.Text.Trim());btFormat.PrintOut(true, false);//第二个false设置打印时是否跳出打印属性btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出时是否保存标签}catch(Exception ex){MessageBox.Show(ex.Message);}}private void Form1_FormClosed(object sender, FormClosedEventArgs e){btAPP.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出时同步退出bartender进程}}
}

示例2:与上面一样的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;
using BarTender;
using System.Text;
using Newtonsoft.Json;namespace bartenderService
{/// <summary>/// BartenderWebService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService]public class SysContext{public static BarTender.Application btapp = new BarTender.Application();public static BarTender.Format btfat;}public class BartenderWebService : System.Web.Services.WebService{[WebMethod]public string HelloWorld(){return "Hello World";}[WebMethod]public string GetTestLabelJson(){printContext pt=new printContext();pt.data = new List<printCell>();printCell cell=new printCell();pt.modelName = "气体检测仪.btw";pt.printDateStr = "生产日期";pt.printFBStr = "防爆证号";pt.printGasStr = "测量气体";pt.printSNStr = "产品编号";pt.printTELStr = "热线电话";pt.printVolStr = "工作电压";pt.printRangeStr = "检测范围";cell.printDate = "2019.12";cell.printFB = "CNEX17.0263";cell.printGas = "可燃气体";cell.printRange = "0-100";cell.printVol = "24V";cell.printUnit = "%LEL";cell.printSN = "226013221";pt.data.Add(cell);cell.printGas = "氨气";cell.printUnit = "PPM";pt.data.Add(cell);return JsonConvert.SerializeObject(pt);}[WebMethod]public string PrintLabel(string jsonstr){int printcount = 0,i=0;string path = System.AppDomain.CurrentDomain.BaseDirectory + "model";string file_ini = path + "\\configure.ini";//pt.modelName + pt.data[1].printRange + pt.data[1].printGas//Newtonsoft.Json.Linq.JArray.Parse(jsonstr);printContext pt = JsonConvert.DeserializeObject<printContext>(jsonstr);if (pt.modelName == null)pt.modelName = "";if (pt.modelName == "")pt.modelName = "气体检测仪.btw";try{SysContext.btfat = SysContext.btapp.Formats.Open(path + "\\" + pt.modelName, false, "");}catch{pt.modelName = "气体检测仪.btw";SysContext.btfat = SysContext.btapp.Formats.Open(path + "\\" + pt.modelName, false, "");}SysContext.btfat.PrintSetup.IdenticalCopiesOfLabel = 1;SysContext.btapp.Visible = false;printcount = pt.data.Count();for(i=0;i<printcount;i++){SysContext.btfat.SetNamedSubStringValue("BT_Gas", String.Format(pt.printGasStr+":"+pt.data[i].printGas));SysContext.btfat.SetNamedSubStringValue("BT_Range", String.Format(pt.printRangeStr+":"+pt.data[i].printRange+ pt.data[i].printUnit));SysContext.btfat.SetNamedSubStringValue("BT_Vol", String.Format(pt.printVolStr + ":" + pt.data[i].printVol));SysContext.btfat.SetNamedSubStringValue("BT_SN", String.Format(pt.printSNStr + ":" + pt.data[i].printSN));SysContext.btfat.SetNamedSubStringValue("BT_DATE", String.Format(pt.printDateStr + ":" + pt.data[i].printDate));SysContext.btfat.SetNamedSubStringValue("BT_FB", String.Format(pt.printFBStr + ":" + pt.data[i].printFB));if(pt.data[i].printTEL!=""&& pt.data[i].printTEL !=null)SysContext.btfat.SetNamedSubStringValue("BT_TEL", String.Format(pt.printTELStr + ":" + pt.data[i].printTEL));SysContext.btfat.PrintOut(false, false);} return "ok";}}
}

http://www.lryc.cn/news/147821.html

相关文章:

  • Spring——Spring读取文件
  • 这是一条求助贴(postman测试的时候一直是404)
  • 信号完整性分析基础知识之有损传输线、上升时间衰减和材料特性(四):有损传输线建模
  • elk日志收集系统
  • perl 语言中 AUTOLOAD 的用法
  • 服务器放在香港好用吗?
  • C++设计模式_01_设计模式简介(多态带来的便利;软件设计的目标:复用)
  • Docker技术--WordPress博客系统部署初体验
  • 提高代码可读性和可维护性的命名建议
  • Docker基础入门:Docker网络与微服务项目发布
  • Docker安装详细步骤
  • 十六、pikachu之SSRF
  • 最新PHP短网址生成系统/短链接生成系统/URL缩短器系统源码
  • 漱玉平民大药房:多元化药店变革的前夜
  • 如何实现AI的矢量数据库
  • Java与Modbus-TCP/IP网络通讯
  • 音视频 ffmpeg命令图片与视频互转
  • C++的基类和派生类构造函数
  • C语言中对json格式数据的解析和封装
  • RT-Thread自动初始化机制
  • 在本地搭建Jellyfin影音服务器,支持公网远程访问影音库的方法分享
  • 强盛集团面试题实战(持续更新)
  • golang 协程的实现原理
  • go gin 参数绑定常用验证器
  • 多用户商城系统常见的安全性和数据保护措施有哪些?
  • 如何在WSL上导入任何Linux发行版
  • 汽车自适应巡航系统车距控制策略研究
  • 鸿蒙系列-如何更好地使用 ArkUI 的 Image 组件?
  • 机器学习简介[01/2]:简单线性回归
  • Kubernetes技术--k8s核心技术yaml资源编排