C#调用barTender打印标签示例

news2024/10/7 6:50:55

使用的电脑需要先安装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.coloradmin.cn/o/939446.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vivado复制工程后如何修改路径

在 vivado 工程复制之后&#xff0c;直接打开项目&#xff0c;会发现所有文件都指向原工程&#xff0c;这个问题困扰了我好久&#xff08;之前都是项目中 remove 之后再一个一个重新添加&#xff0c;特别麻烦&#xff09;。然而前几天突发奇想试着把除 .srcs 文件夹之外的所有文…

大规模数据爬取 - 增量和分布式爬虫架构实战

嗨&#xff0c;亲爱的爬虫开发者们&#xff01;在当今的数据驱动时代&#xff0c;大规模数据的爬取对于许多领域的研究和应用至关重要在本文中&#xff0c;我将与你分享大规模数据爬取的实战经验&#xff0c;重点介绍增量和分布式爬虫架构的应用&#xff0c;帮助你高效地处理海…

AUTOSAR DEM (一):简介

AUTOSAR DEM &#xff08;一&#xff09;:简介 故障事件触发故障信息上报故障信息处理故障事件存储DEM与其他模块的联系 缩略词说明 abbreviationdescriptionDEMDiagnostic event managerDTCDiagnostic Trouble CodeBSWBasic softwareSWCSoftware componenECUMECU state manag…

Java 大厂面试 —— 常见集合篇 List HashMap 红黑树

23Java面试专题 八股文面试全套真题&#xff08;含大厂高频面试真题&#xff09;多线程_软工菜鸡的博客-CSDN博客 常见集合篇-01-集合面试题-课程介绍 02-算法复杂度分析 2 List相关面试题 2.1 数组 2.1.1 数组概述 数组&#xff08;Array&#xff09;是一种用连续的内存空…

Win11 重启资源管理器的方法

方法一&#xff1a;按【Ctrl Alt Del】组合键后&#xff0c;调出锁定界面&#xff0c;然后点击【任务管理器】即可 方法二&#xff1a;按【Ctrl Shift ESC】组合键后&#xff0c;会直接调出任务管理器 1、在任务管理器窗口中&#xff0c;找到名称为【Windows 资源管理器】…

使用synchronized关键字同步类方法

要想解决“脏数据”的问题&#xff0c;最简单的方法就是使用synchronized关键字来使run方法同步&#xff0c;代码如下&#xff1a; public synchronized void run() { } 从上面的代码可以看出&#xff0c;只要在void和public之间加上synchronized关键字&#xff0c;就可以…

内网远程控制总结

前言 在内网渗透过程中&#xff0c;会碰到远程控制soft或者其他&#xff0c;这里针对远程控制软件做如下总结。 远程控制软件 向日葵篇 向日葵查看版本 向日葵&#xff08;可以攻击&#xff09; 针对向日葵的话其实如果有本地安装的话&#xff0c;是有可能存在漏洞的。这…

[CKA]考试注意事项及作者考试结果

在CKA考试的时候&#xff0c;注意目前可以使用中文名进行注册&#xff0c;最后证书上的名字也是中文名 考试前准备&#xff1a; 1、身份证 2、桌面除了电脑鼠标其他物品都收好 3、房间就自己一个人&#xff0c;不允许房间有其他人 4、网速要快&#xff0c;博主特意升级了自…

GBU814-ASEMI功率整流器件GBU814

编辑&#xff1a;ll GBU814-ASEMI功率整流器件GBU814 型号&#xff1a;GBU814 品牌&#xff1a;ASEMI 封装&#xff1a;GBU-4 恢复时间&#xff1a;&#xff1e;50ns 正向电流&#xff1a;8A 反向耐压&#xff1a;1400V 芯片个数&#xff1a;4 引脚数量&#xff1a;4 …

CGLIB代理,jsp,EL表达式,JSTL标准标签库

1、CGLIB代理 有一个类没有实现接口&#xff0c;想要对这个类实现增强&#xff0c;就需要使用CGLIB代理 导入CGLIB的包 <dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.3.0</version> </depende…

集成易点易动管理系统连接更多应用

场景描述&#xff1a; 基于易点易动开放平台能力&#xff0c;无代码集成易点易动与多个应用互通互连&#xff0c;实现固定资产管理数字化、智能化。通过Aboter可搭建业务自动化流程&#xff0c;实现多个应用之间的数据连接。 开放能力&#xff1a; 消息推送&#xff1a; 新…

无涯教程-分类算法 - 多项式逻辑回归模型函数

Logistic逻辑回归的另一种有用形式是多项式Lo​​gistic回归&#xff0c;其中目标或因变量可以具有3种或更多可能的unordered类型&#xff0c;即没有定量意义的类型。 用Python实现 现在&#xff0c;无涯教程将在Python中实现上述多项式逻辑回归的概念。为此&#xff0c;使用…

前端js实现获取指定元素(top,lef,right,bottom)到视窗的距离 ;getBoundingClientRect()获取

getBoundingClientRect()获取元素位置&#xff0c;这个方法没有参数 该函数返回一个Object对象&#xff0c;该对象有6个属性&#xff1a;top,lef,right,bottom,width,height&#xff1b; <div id"box"></div>var objectdocument.getElementById(box); …

产教融合 | 中南大学暑期实训,用万应低代码践行敏捷开发之路

融合学究与实践&#xff0c;方能成为当代“数字英才”。 2023年8月11日&#xff0c;由潇湘大数据研究院、中南大学计算机学院及云畅科技联合组织的2020级数据科学与大数据技术专业暑期‘生产实训’项目圆满结束。本次实训全程线下进行&#xff0c;基于“深度创新培育计划”&…

宇凡微Y51T合封射频芯片,集成433M芯片和MCU

宇凡微推出的Y51T芯片的设计理念很有趣&#xff0c;将MCU和射频芯片集成在一颗芯片内&#xff0c;从而实现高度的集成度和功能优势。这样的设计在某些应用中确实能够带来诸多优点&#xff1a; Y51T将51H MCU和Y4455 433MHz射频芯片融合在一颗芯片内&#xff0c;实现了高度集成的…

GPU中统一内存最新机制解析

通过异构内存管理简化 GPU 应用程序开发 异构内存管理 (HMM) 是一项 CUDA 内存管理功能&#xff0c;它扩展了 CUDA 统一内存编程模型的简单性和生产力&#xff0c;以包括具有 PCIe 连接的 NVIDIA GPU 的系统上的系统分配内存。 系统分配内存是指最终由操作系统分配的内存&#…

face-api实现人脸识别。

face-api实现人脸识别 face-api的由来tensorflow.js 是什么部分代码模型介绍 face-api的由来 访问地址 JavaScript API for face detection and face recognition in the browser implemented on top of the tensorflow.js core API 官方说明 翻译&#xff1a;在tensorflow.js…

oppo手机怎么录屏?录制屏幕,就看这里!

“有人知道oppo手机怎么录屏吗&#xff0c;前几年买的oppo手机&#xff0c;用到现在感觉挺流畅的&#xff0c;也不是很卡顿&#xff0c;最近听说我这个型号的手机也有录屏功能&#xff0c;但是我不知道怎么打开&#xff0c;就想问问大伙&#xff0c;oppo手机怎么录屏呀。” 在…

MySql015——使用子查询

一、创建customers表 ######################## # Create customers table ######################## use study;CREATE TABLE customers (cust_id int NOT NULL AUTO_INCREMENT,cust_name char(50) NOT NULL ,cust_address char(50) NULL ,cust_city char…

LED地板屏幕的工作原理

LED地砖屏是一款数字化地面展示设备&#xff0c;它的实现主要是以数字技术为核心&#xff0c;通过微电脑全数字化处理以及先进的电路保护设备&#xff0c;对视频进行同步控制&#xff0c;并实现了高分辨率的显示效果&#xff0c;在展厅设计以及舞台演出中都有相关的应用。免费提…