class BarTender_class
{
BarTender.Application BarTApp;
BarTender.Format BarTFormat;
private static BarTender_class BartenderPrin = null;
#region
public static BarTender_class GetInstance()
{
if (BartenderPrin != null)
{
return BartenderPrin;
}
BartenderPrin = new BarTender_class();
return BartenderPrin;
}
private BarTender_class()
{
if (BarTApp == null)
{
BarTApp = new BarTender.Application();
}
}
#endregion
/// <summary>
/// 数据设置方式打印条码
/// </summary>
/// <param name="Path">路径</param>
/// <param name="Qty">单个打印数量</param>
/// <returns></returns>
public bool PrintBarT(string Path, int Qty)
{
if (string.IsNullOrEmpty(Path))
{
MessageBox.Show("模版路径为空或不存在");
return false;
}
BarTFormat = BarTApp.Formats.Open(Path, false, "");
BarTFormat.PrintSetup.IdenticalCopiesOfLabel = Qty;
BarTApp.Visible = false;
BarTFormat.PrintSetup.NumberSerializedLabels = 1;
try
{
DesignObject btObject = null;
foreach (DesignObject obj in BarTFormat.Objects)
{
//对象为线条的时候会出错
if (obj.Name.Contains("文本") || obj.Name.Contains("条形码"))
{
if (obj.Value.ToString().Contains("[") && obj.Value.ToString().Contains("]"))
{
btObject = BarTFormat.Objects.Find(obj.Name);
btObject.DoNotPrint = true;
}
}
}
BarTFormat.PrintOut(false, false);//第2个参数是 是否显示打印机属性的。可以设置打印机路径
}
catch (Exception ex)
{
throw ex;
}
finally
{
BarTFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
}
return true;
}
public bool PrintBarT2(string lblPath, System.Collections.Hashtable hTable, int PrintQty)
{
if (string.IsNullOrEmpty(lblPath))
{
MessageBox.Show("模版路径为空或不存在");
return false;
}
if (hTable == null || hTable.Count <= 0)
{
MessageBox.Show("没有可打印数据");
return false;
}
BarTFormat = BarTApp.Formats.Open(lblPath, false, "");
BarTFormat.PrintSetup.IdenticalCopiesOfLabel = PrintQty;
BarTApp.Visible = false;
BarTFormat.PrintSetup.NumberSerializedLabels = 1;
try
{
foreach (string key in hTable.Keys)
{
try
{
BarTFormat.SetNamedSubStringValue(key, (hTable[key]).ToString());
}
catch (Exception e1)
{
throw new Exception("key:" + key + "," + e1.Message);
}
}
//标签上的一些东西不需要打印
//获取标签变量
string vars = this.GetVar(lblPath); //获取打印标签上所有的变量值
//模版上的变量,如果在hTable不存在,就设置为不显示
vars = vars.Substring(0, vars.Length - 1);
string[] bb = vars.Split(new char[] { '$' });
ArrayList list = new ArrayList();
for (int i = 0; i < bb.Length; i++)
{
string[] cc = bb[i].Split(new char[] { '#' });
string 数据源字段 = cc[0].ToString().Replace("{", "").Replace("}", "").Replace("\n", "").Replace("\r", ""); //模版上的数据源名称,数据源是不能重复的,Bartender已经限定
list.Add(数据源字段);
}
DesignObject btObject = null;
foreach (DesignObject obj in BarTFormat.Objects)
{
//对象为线条的时候会出错
if (obj.Name.Contains("文本") || obj.Name.Contains("条形码"))
{
if (obj.Value.ToString().Contains("[") && obj.Value.ToString().Contains("]"))
{
btObject = BarTFormat.Objects.Find(obj.Name);
btObject.DoNotPrint = true;
}
}
}
BarTFormat.PrintOut(false, false);//第2个参数是 是否显示打印机属性的。可以设置打印机路径
}
catch (Exception ex)
{
throw ex;
}
finally
{
BarTFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
}
return true;
}
public string GetVar(string _模版)
{
try
{
BarTFormat = BarTApp.Formats.Open(_模版, false, "");
string var = BarTFormat.NamedSubStrings.GetAll("#", "$");
return var;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交