C#調(diào)用barTender打印標(biāo)簽示例的實現(xiàn)
更新時間:2023年08月28日 14:46:55 作者:小黃人軟件
Bartender是最優(yōu)秀的條碼打印軟件,在企業(yè)里使用非常普遍,本文主要介紹了C#調(diào)用barTender打印標(biāo)簽示例的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
使用的電腦需要先安裝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進(jìn)程 } 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;//打印份數(shù) btFormat.PrintSetup.NumberSerializedLabels = 1;//序列標(biāo)簽數(shù) btFormat.SetNamedSubStringValue("打印文本", txt.Trim()); btFormat.SetNamedSubStringValue("打印條碼", barcode.Trim()); btFormat.PrintOut(true, false);//第二個false設(shè)置打印時是否跳出打印屬性 btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出時是否保存標(biāo)簽 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;//打印份數(shù) btFormat.PrintSetup.NumberSerializedLabels = 1;//序列標(biāo)簽數(shù) btFormat.SetNamedSubStringValue("打印文本", txtMemo.Text.Trim()); btFormat.SetNamedSubStringValue("打印條碼", txtBarcode.Text.Trim()); btFormat.PrintOut(true, false);//第二個false設(shè)置打印時是否跳出打印屬性 btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出時是否保存標(biāo)簽 } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { btAPP.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出時同步退出bartender進(jìn)程 } } }
示例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 從腳本中調(diào)用此 Web 服務(wù),請取消注釋以下行。 // [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 = "生產(chǎn)日期"; pt.printFBStr = "防爆證號"; pt.printGasStr = "測量氣體"; pt.printSNStr = "產(chǎn)品編號"; pt.printTELStr = "熱線電話"; pt.printVolStr = "工作電壓"; pt.printRangeStr = "檢測范圍"; cell.printDate = "2019.12"; cell.printFB = "CNEX17.0263"; cell.printGas = "可燃?xì)怏w"; 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"; } } }
到此這篇關(guān)于C#調(diào)用barTender打印標(biāo)簽示例的實現(xiàn)的文章就介紹到這了,更多相關(guān)C#調(diào)用barTender打印標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(上)
這篇文章主要介紹了如何學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程的相關(guān)知識,文中講解的非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06C#以太網(wǎng)Sockets客戶端設(shè)計實現(xiàn)
本文主要介紹了C#以太網(wǎng)Sockets客戶端設(shè)計實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C# 實現(xiàn)的圖片蓋章功能,支持拖拽、旋轉(zhuǎn)、放縮、保存
這篇文章主要介紹了C# 實現(xiàn)的圖片蓋章功能,支持拖拽、旋轉(zhuǎn)、放縮、保存,需要的朋友可以參考下2014-04-04