生产型企业基本都有条码追溯管理的需求,不同的产品有不同的标签样式规格以及内容,打印的条码往往需要追溯以及防重校验,因此市面有很多打印软件,今天分享基于winform开发的调用bartender标签的工具。
先上效果图
/// <summary>
/// 使用 BarTender进行打印,批量打印
/// </summary>
/// <param name="labelName"> BarTender标签名称</param>
/// <param name="labelParamsValuesList">打印数据</param>
/// <param name="msg">消息</param>
/// <param name="printQty">打印数量</param>
/// <returns>打印OK true</returns>
public bool BTPrint(string labelName, Dictionary<string, string> labelParamsValues, out string msg, int printQty = 1, string printer = "")
{
try
{
bool checkLabelVar = true;
string tmpMsg = "";
this.TemplateFile = labelName;
if (!File.Exists(TemplateFile))
{
msg = "Error:Label File Not Exists" + TemplateFile;
this.ErrorMsg = msg;
return false;
}
if (!string.IsNullOrEmpty(printer))
{
bool printerFlag = false;
foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取所有打印机名称
{
if (sPrint.Equals(printer))
{
printerFlag = true;
}
}
if (!printerFlag)
{
msg = "Error:Printer Name Error";
return false;
}
}
//找到打印模板的标签页
btFormat = btApp.Formats.Open(labelName, false, "");
if (!string.IsNullOrEmpty(printer))
{
btFormat.Printer = printer;
}
if (btApp != null)
{
//取模板所有的值
string strBT = btFormat.NamedSubStrings.GetAll("#", "$");
//将模板所有值进行拆分
strBT = strBT.Substring(0, strBT.Length - 1);
string[] strBTValue = strBT.Split(new char[] { '$' });
//循环将模板的值写入lbldicVar中
Dictionary<string, string> lbldicVar = new Dictionary<string, string>();
for (int i = 0; i < strBTValue.Length; i++)
{
string[] cc = strBTValue[i].Split(new char[] { '#' });
lbldicVar.Add(cc[0].ToString(), cc[1].ToString()); //0是模板的参数 1是参数值
}
if (labelParamsValues.Count > 0)
{
foreach (var item in lbldicVar)
{
if (labelParamsValues.Count(q => q.Key.ToUpper() == item.Key.ToUpper()) == 0)
{
tmpMsg = "数据源缺少参数: " + item.Key;
if (checkLabelVar)
{
this.ErrorMsg = tmpMsg;
checkLabelVar = false;
}
else
{
this.ErrorMsg = this.ErrorMsg + (char)10 + tmpMsg;
}
}
}
if (!checkLabelVar)
{
msg = this.ErrorMsg;
this.Status = false;
return false;
}
foreach (var param in labelParamsValues.Keys)
{
if (lbldicVar.Keys.Count(q => q.ToUpper() == param.ToUpper()) > 0)
{
var labvar = lbldicVar.Keys.First(q => q.ToUpper() == param.ToUpper());
if (string.IsNullOrEmpty(labelParamsValues[param]))
{
tmpMsg = "数据源参数值为空:" + labvar;
if (checkLabelVar)
{
this.ErrorMsg = tmpMsg;
checkLabelVar = false;
}
else
{
this.ErrorMsg = this.ErrorMsg + (char)10 + tmpMsg;
}
}
btFormat.SetNamedSubStringValue(param, labelParamsValues[param]);
}
}
if (!checkLabelVar)
{
msg = this.ErrorMsg;
this.Status = false;
return false;
}
btFormat.IdenticalCopiesOfLabel =printQty;
//第二个false设置打印时是否跳出打印属性
btFormat.PrintOut(true, false);
//退出是是否保存标签
btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
//lblDoc.FormFeed();
msg = "OK";
this.ErrorMsg = msg;
this.Status = true;
return true;
}
else
{
msg = "Print Data is null";
this.ErrorMsg = msg;
this.Status = false;
return false;
}
}
else
{
msg = "Error:CodeSoft Application can't boot up";
this.ErrorMsg = msg;
this.Status = false;
return false;
}
}
catch (Exception ex)
{
msg = ex.Message + ex.InnerException;
this.ErrorMsg = msg;
this.Status = false;
return false;
}
finally
{
if (btApp != null)
{
btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出时同步退出bartender进程
//lblApp.Documents.CloseAll(true);
//lblDoc = null;
}
GC.Collect();
}
}