/// <summary>
/// 调用BarTender打印,模板的变量为:barcode0,barcode1,barcode2.......,barcode7 (内循环打印集合列表)
/// </summary>
/// <param name="pPrinter">打印机名称,必填</param>
/// <param name="pTagsTemplateId">标签模板ID,必填</param>
/// <param name="pPrintParameterList">打印参数列表,必填</param>
/// <param name="checkBillId">单据ID</param>
/// <param name="cellNum">每行打印的列数</param>
public static bool BarcodePrint(string pPrinter, string pTagsTemplateId,
List<PrintParameterObj> pPrintParameterList, string checkBillId, int cellNum)
{
BarTender.ApplicationClass btApp = null;
BarTender.Format btFormat = null;
bool isPrintOut = false;
try
{
if (string.IsNullOrEmpty(pPrinter))
{
MessageBox.Show(@"请检查打印驱动程序设置!");
return isPrintOut;
}
if (string.IsNullOrEmpty(pTagsTemplateId))
{
MessageBox.Show(@"请选择打印的模板!");
return isPrintOut;
}
var clsTagsTemplateObj = TemplateBusiness.GetObjectById(pTagsTemplateId);
if (clsTagsTemplateObj == null)
{
MessageBox.Show(@"找不到所需的模板!");
return isPrintOut;
}
modCommon.GetTemplateFile(clsTagsTemplateObj);
var errMsg = "";
modCommon.DownloadAndCreatTemplate(clsTagsTemplateObj.FileName, out errMsg);
if (!string.IsNullOrEmpty(errMsg))
{
MessageBox.Show(errMsg, GenericContext.strTip);
return isPrintOut;
}
var templateFullPath = modCommon.TagsTemplateDir + @"\" + clsTagsTemplateObj.FileName;
btApp = new BarTender.ApplicationClass();
btFormat = btApp.Formats.Open(templateFullPath, false, "");
btFormat.Printer = pPrinter;
if (pPrintParameterList != null && pPrintParameterList.Count > 0)
{
int iNum = 0;
string strWhere = string.Empty;
for (int i = 0; i < pPrintParameterList.Count; i++)
{
try
{
btFormat.SetNamedSubStringValue(("barcode" + iNum), pPrintParameterList[i].ParameterValue);
}
catch
{
MessageBox.Show("打印模板错误", GenericContext.strTip);
return false;
}
//组合更新数据的条件
if (!string.IsNullOrEmpty(pPrintParameterList[i].ParameterValue))
strWhere += ("'" + pPrintParameterList[i].ParameterValue + "',");
if (iNum == cellNum - 1)
{
iNum = 0;
btFormat.PrintOut(false, false);
//更新明细数据状态
if (!string.IsNullOrEmpty(strWhere))
{
strWhere = strWhere.Substring(0, strWhere.Length - 1);
CheckDetailBusiness.CheckDetailUpdate(strWhere, checkBillId);
}
strWhere = string.Empty;
}
else
iNum++;
}
}
isPrintOut = true;
}
catch
{
MessageBox.Show("调用模板打印时发生错误", GenericContext.strTip);
return false;
}
finally
{
if (btFormat != null)
btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
if (btApp != null)
btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
}
return isPrintOut;
}