之前公司標籤一直用ZPL開發,前段時間公司購買了bartender軟件用於標籤設計。功能大大的,沒得說。廢話少說了,進入正題。
需求:標籤模板已經設計好,設計個簡單程序調用該標籤模板並打印。(標籤變量通過程序傳遞)
以下爲簡單寫的winform打印程序
程序界面:
代碼如下(其中一個標籤類型的代碼,其他省略):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace CS_label_Print { public partial class BOX_Label : Form { public BOX_Label() { InitializeComponent(); } BarTender.Application btApp; BarTender.Format btFormat; private void btn2_Click(object sender, EventArgs e) { int a = Int32.Parse(this.num2.Value.ToString());//設置打印數量的變量 if (this.TXT3.Text.Length == 0 || this.TXT4.Text.Length == 0) { MessageBox.Show("未輸入料號或者QTY"); } else { btFormat = btApp.Formats.Open(@"c:\BarTenderFiles\CS\CS_Package Label", false, ""); btFormat.PrintSetup.IdenticalCopiesOfLabel = 1; //設置同序列打印的份數 btFormat.PrintSetup.NumberSerializedLabels = a; //設置需要打印的序列數 btFormat.SetNamedSubStringValue("SN", this.TXT3.Text); //向bartender模板傳遞變量 btFormat.SetNamedSubStringValue("QTY", this.TXT4.Text); btFormat.PrintOut(false, false); //第二個false設置打印時是否跳出打印屬性 btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出時是否保存標籤 } } private void BOX_Label_Load(object sender, EventArgs e) { btApp = new BarTender.Application(); this.num2.Value = 1; } private void BOX_Label_FormClosed(object sender, FormClosedEventArgs e) { btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出時同步退出bartender進程 } } }