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";
        }

    }
}

猜你喜欢

转载自blog.csdn.net/chenhao0568/article/details/132534761