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; using Seagull.BarTender.Print; using DevExpress.XtraEditors; using Lskj.Control; using Lskj.Util; using Lskj.Business.Impl; namespace Lskj.PrintTool { public partial class FormMain : BaseForm { public FormMain() { InitializeComponent(); this.FormClosed+=new FormClosedEventHandler(closed); this.Opacity =0; } BarTender.Application btapp;//Bartender 应用实例 BarTender.Format btformat; //Bartender 样式 Dictionary dic = new Dictionary();//传入值改键值对 public string data;//参数2 传入值 public string stitchingAddress;//参数3 固定地址 /// /// 点击打印 /// /// /// private void button1_Click(object sender, EventArgs e) { try { print(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message); } } //打印 public void print() { try { btapp = new BarTender.Application(); btformat = btapp.Formats.Open(this.txtTemplateFile.Text.Trim(), false, ""); //MessageBox.Show(this.txtTemplateFile.Text+""); btformat.PrintSetup.IdenticalCopiesOfLabel = 1; //设置同序列打印的份数 btformat.PrintSetup.NumberSerializedLabels = 1; //设置需要打印的序列数 GetData(); foreach (var item in dic) { btformat.SetNamedSubStringValue(item.Key, item.Value); //MessageBox.Show(item.Key+":"+item.Value); } //btformat.PrintOut(true, false); //第二个参数设置是否跳出打印属性; btformat.PrintOut(true, true); //第二个参数设置是否跳出打印属性 btformat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges); //退出时是否保存标签 this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// /// 打开文件 /// /// /// private void button2_Click_1(object sender, EventArgs e) { try { OpenFileDialog of = new OpenFileDialog(); of.Title = "Select a folder that contains BarTender formats."; DialogResult dr = of.ShowDialog(); if (dr == DialogResult.OK) { this.txtTemplateFile.Text = of.FileName; } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message); } } /// /// 解析键值对数据 /// private void GetData() { try { data = data.Replace("\r\n", ",").Trim(); string[] strlist = data.Split(','); foreach (string str in strlist) { string key = str.Substring(0, str.IndexOf('=')); string val = str.Substring(str.IndexOf('=') + 1); dic.Add(key, val); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message); } } private void FormMain_Load(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(stitchingAddress)) { this.Opacity = 1; } else { this.txtTemplateFile.Text = PubUtil.PrintModePath + stitchingAddress; print(); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message); } } private void closed(object sender, FormClosedEventArgs e) { try { if (btapp != null) { btapp.Quit(BarTender.BtSaveOptions.btSaveChanges);//退出时同退退出BarTender进程 } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message); } } } }