我觉得做的不错比较全的操作的一些BarTender

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

        } 

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值