使用BarTender打印
/// <summary>/// BarTender列印方法2替换参数/// </summary>/// <param name="btwPath">文件路径</param>/// <param name="kvZ">参数-值对</param>/// <param name="prtName">打印机名,空则调用默认打印机</param>/// <param name="zCopies">列印份数</param>/// <returns></returns>public static bool vfPrintBtwR(string btwPath, string kvZ, string prtName, string zCopies){if (!File.Exists(btwPath)) throw new Exception("不存在模版文件" + btwPath);try{Hashtable htVV = new Hashtable();string varNam = "";string[] kvn = new string[0];int iCopies = 0;int.TryParse(zCopies, out iCopies);iCopies = Math.Max(iCopies, 1);kvZ = kvZ.Replace(",", ",").Replace(";", ",").Replace(":", ":").Trim();foreach (string kvG in kvZ.Split(',')){kvn = kvG.Split(':');if (kvn.Length < 2) continue;htVV[kvn[0].Trim().ToLower()] = kvn[1].Trim();}BarTender.Application btApp = new BarTender.Application();BarTender.Format btFmt = btApp.Formats.Open(btwPath, false, prtName);//btFmt.NumberSerializedLabels = Convert.ToInt32(1);//序列标签数,列印份数btFmt.PrintSetup.IdenticalCopiesOfLabel = iCopies; // 同样标签的份数 btFmt.PrintSetup.NumberSerializedLabels = 1; // 序列标签数 BarTender.NamedSubStrings nss = btFmt.NamedSubStrings;for (int ik = 0; ik < nss.Count; ik++){varNam = nss.GetSubString(ik + 1).Name;btFmt.SetNamedSubStringValue(varNam, "" + htVV[varNam.ToLower()]);}btFmt.PrintOut(false, false);btFmt.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);//btFmt.Close(BarTender.BtSaveOptions.btSaveChanges);return true;}catch (System.Exception ex){Console.WriteLine(ex.ToString());return false;}}
static BarTender.Application btApp;static BarTender.Format btFormat = null;static void Main(string[] args){RUN();}public static void RUN(){btApp = new BarTender.Application();try{string labelPath = @"C:\path\to\your\label.btw"; // 标签文件路径Format btFormat = btApp.Formats.Open(labelPath, false);// 设置数据btFormat.SetNamedSubStringValue("YourFieldName", "YourValue");btFormat.PrintOut();Console.WriteLine("标签打印成功。");}catch (Exception ex){Console.WriteLine($"打印失败: {ex.Message}");}finally{if (btApp != null){btApp.Quit();}}}
GitCode - 全球开发者的开源社区,开源代码托管平台 下载地址