diff --git a/linesider_screen_bankend/linesider_screen_bankend.Core/Tools/BartenderPrintingTool.cs b/linesider_screen_bankend/linesider_screen_bankend.Core/Tools/BartenderPrintingTool.cs
index d1022ad..0a9cb2a 100644
--- a/linesider_screen_bankend/linesider_screen_bankend.Core/Tools/BartenderPrintingTool.cs
+++ b/linesider_screen_bankend/linesider_screen_bankend.Core/Tools/BartenderPrintingTool.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using BarTender;
namespace linesider_screen_bankend.Core.Tools
@@ -111,6 +112,155 @@ namespace linesider_screen_bankend.Core.Tools
}
}
+ ///
+ /// 批量打印多个List标签
+ ///
+ /// 标签模板路径
+ /// 包含多个标签数据的列表
+ /// 每个标签的打印份数
+ /// 序列化标签数量
+ /// 从列表项获取标签变量的委托
+ /// 是否全部打印成功
+ public bool PrintLabels(
+ string templatePath,
+ List items,
+ int copiesPerItem = 1,
+ int serializedLabels = 1,
+ Func> getSubStringValues = null)
+ {
+ if (_disposed)
+ throw new ObjectDisposedException("BartenderPrintHelper", "对象已被释放,不能执行打印操作。");
+
+ if (items == null || items.Count == 0)
+ return false;
+
+ try
+ {
+ // 打开标签模板
+ _btFormat = _btApp.Formats.Open(templatePath);
+
+ // 设置打印参数
+ _btFormat.PrintSetup.IdenticalCopiesOfLabel = copiesPerItem;
+ _btFormat.PrintSetup.NumberSerializedLabels = serializedLabels;
+
+ bool allSuccess = true;
+
+ // 遍历所有项目并打印
+ foreach (var item in items)
+ {
+ try
+ {
+ // 获取当前项的变量值
+ var subStringValues = getSubStringValues?.Invoke(item);
+
+ // 设置标签变量值
+ if (subStringValues != null)
+ {
+ foreach (var kvp in subStringValues)
+ {
+ _btFormat.SetNamedSubStringValue(kvp.Key, kvp.Value);
+ }
+ }
+
+ // 执行打印
+ _btFormat.PrintOut(false, false);
+ }
+ catch (Exception ex)
+ {
+ // 记录错误但继续打印其他项
+ allSuccess = false;
+ // 可以在这里添加日志记录
+ Console.WriteLine($"打印标签时出错: {ex.Message}");
+ }
+ }
+
+ return allSuccess;
+ }
+ catch (Exception ex)
+ {
+ throw new Exception($"批量打印标签时出错: {ex.Message}", ex);
+ }
+ finally
+ {
+ if (_btFormat != null)
+ {
+ _btFormat.Close(BtSaveOptions.btDoNotSaveChanges);
+ System.Runtime.InteropServices.Marshal.ReleaseComObject(_btFormat);
+ _btFormat = null;
+ }
+ }
+ }
+
+
+ ///
+ /// 批量打印多个标签(新增方法)
+ ///
+ /// 标签模板路径
+ /// 多个标签的变量键值对列表
+ /// 每个标签的打印份数
+ /// 序列化标签数量
+ /// 是否全部打印成功
+ public bool PrintBatchLabels(
+ string templatePath,
+ List> batchSubStringValues,
+ int copiesPerLabel = 1,
+ int serializedLabels = 1)
+ {
+ if (_disposed)
+ throw new ObjectDisposedException(nameof(BartenderPrintHelper), "对象已被释放,不能执行打印操作。");
+
+ if (batchSubStringValues == null || batchSubStringValues.Count == 0)
+ return false;
+
+ try
+ {
+ // 打开标签模板(只打开一次,提升性能)
+ _btFormat = _btApp.Formats.Open(templatePath);
+ _btFormat.PrintSetup.IdenticalCopiesOfLabel = copiesPerLabel;
+ _btFormat.PrintSetup.NumberSerializedLabels = serializedLabels;
+
+ bool allSuccess = true;
+
+ // 遍历所有标签数据并打印
+ foreach (var subStringValues in batchSubStringValues)
+ {
+ try
+ {
+ // 设置变量值
+ foreach (var kvp in subStringValues)
+ {
+ _btFormat.SetNamedSubStringValue(kvp.Key, kvp.Value);
+ }
+
+ // 执行打印(不关闭模板,继续复用)
+ _btFormat.PrintOut(false, false);
+ }
+ catch (Exception ex)
+ {
+ allSuccess = false;
+ // 可记录日志或抛出特定异常
+ Console.WriteLine($"打印标签时出错: {ex.Message}");
+ }
+ }
+
+ return allSuccess;
+ }
+ catch (Exception ex)
+ {
+ throw new Exception($"批量打印标签时出错: {ex.Message}", ex);
+ }
+ finally
+ {
+ // 确保释放资源
+ if (_btFormat != null)
+ {
+ _btFormat.Close(BtSaveOptions.btDoNotSaveChanges);
+ System.Runtime.InteropServices.Marshal.ReleaseComObject(_btFormat);
+ _btFormat = null;
+ }
+ }
+ }
+
~BartenderPrintHelper()
{
Dispose(false);
diff --git a/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs b/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs
index 0d45567..5ff43da 100644
--- a/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs
+++ b/linesider_screen_bankend/linesider_screen_bankend/Controller/OnlyPrintProductLabelController.cs
@@ -24,6 +24,8 @@ namespace linesider_screen_bankend.Controller
public class OnlyPrintProductLabelController : WebApiController
{
private readonly Action _logAction;
+ // 准备打印数据
+ string templatePath = ConfigurationManager.AppSettings["label_path"];
public OnlyPrintProductLabelController(Action logAction)
{
@@ -49,9 +51,6 @@ namespace linesider_screen_bankend.Controller
string model = queryParams["model"];
string direction = queryParams["direction"];
-
-
-
try
{
ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable()
@@ -62,22 +61,21 @@ namespace linesider_screen_bankend.Controller
// 使用 using 语句确保资源正确释放
using (var bartender = new BartenderPrintHelper())
{
- // 准备打印数据
- string templatePath = ConfigurationManager.AppSettings["label_path"];
+
var variables = new Dictionary
{
{ "partnumber", workorderInfo.Specification },
{ "model", model },
{ "direction", direction },
{ "productdate", DateTime.Now.ToString("yyyy/MM/dd") },
- {"workorder",workorderInfo.Workorder+"|"+workorderInfo.Specification+"|"+model+"|"+direction }
+ { "workorder",workorderInfo.Workorder+"|"+workorderInfo.Specification+"|"+model+"|"+direction }
};
// 打印标签
bool success = bartender.PrintLabel(
templatePath: templatePath,
subStringValues: variables,
- copies: 1, // 打印2份
+ copies: 1, // 打印1份
serializedLabels: 1 // 序列化标签数为1
);
@@ -90,8 +88,7 @@ namespace linesider_screen_bankend.Controller
}
- // 模拟从数据库获取用户
- var user = new { Id = 123, Name = "张三", Age = 25 };
+
// 返回成功响应
await this.HttpContext.SendApiSuccessAsync(null);
}
@@ -137,29 +134,35 @@ namespace linesider_screen_bankend.Controller
// 获取特定参数
string workorder = queryParams["workorder"];
int LabelNum = int.Parse(queryParams["label_num"]);
+ // 获取特定参数
+ string model = queryParams["model"];
+ string direction = queryParams["direction"];
try
{
ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable()
.Where(x => x.Workorder == workorder)
.First();
+
+
// 使用 using 语句确保资源正确释放
using (var bartender = new BartenderPrintHelper())
{
- // 准备打印数据
- string templatePath = ConfigurationManager.AppSettings["label_path"];
+
var variables = new Dictionary
{
- { "ProductName", "高级笔记本电脑" },
- { "SerialNumber", "SN-20230401-001" },
- { "ManufactureDate", DateTime.Now.ToString("yyyy-MM-dd") }
+ { "partnumber", workorderInfo.Specification },
+ { "model", model },
+ { "direction", direction },
+ { "productdate", DateTime.Now.ToString("yyyy/MM/dd") },
+ {"workorder",workorderInfo.Workorder+"|"+workorderInfo.Specification+"|"+model+"|"+direction }
};
// 打印标签
bool success = bartender.PrintLabel(
templatePath: templatePath,
subStringValues: variables,
- copies: 2, // 打印2份
+ copies: LabelNum, // 打印LabelNum份
serializedLabels: 1 // 序列化标签数为1
);
@@ -172,8 +175,7 @@ namespace linesider_screen_bankend.Controller
}
- // 模拟从数据库获取用户
- var user = new { Id = 123, Name = "张三", Age = 25 };
+
// 返回成功响应
await this.HttpContext.SendApiSuccessAsync(null);
}
@@ -195,6 +197,9 @@ namespace linesider_screen_bankend.Controller
// 获取特定参数
string workorder = queryParams["workorder"];
int LabelNum = int.Parse(queryParams["label_num"]);
+ // 获取特定参数
+ string model = queryParams["model"];
+ string direction = queryParams["direction"];
try
{
@@ -204,20 +209,21 @@ namespace linesider_screen_bankend.Controller
// 使用 using 语句确保资源正确释放
using (var bartender = new BartenderPrintHelper())
{
- // 准备打印数据
- string templatePath = ConfigurationManager.AppSettings["label_path"];
+
var variables = new Dictionary
{
- { "ProductName", "高级笔记本电脑" },
- { "SerialNumber", "SN-20230401-001" },
- { "ManufactureDate", DateTime.Now.ToString("yyyy-MM-dd") }
+ { "partnumber", workorderInfo.Specification },
+ { "model", model },
+ { "direction", direction },
+ { "productdate", DateTime.Now.ToString("yyyy/MM/dd") },
+ {"workorder",workorderInfo.Workorder+"|"+workorderInfo.Specification+"|"+model+"|"+direction }
};
// 打印标签
bool success = bartender.PrintLabel(
templatePath: templatePath,
subStringValues: variables,
- copies: 2, // 打印2份
+ copies: 1, // 打印2份
serializedLabels: 1 // 序列化标签数为1
);
@@ -230,8 +236,7 @@ namespace linesider_screen_bankend.Controller
}
- // 模拟从数据库获取用户
- var user = new { Id = 123, Name = "张三", Age = 25 };
+
// 返回成功响应
await this.HttpContext.SendApiSuccessAsync(null);
}
diff --git a/linesider_screen_bankend/linesider_screen_bankend/Resources/gxassembly_production_label.btw b/linesider_screen_bankend/linesider_screen_bankend/Resources/gxassembly_production_label.btw
new file mode 100644
index 0000000..dd60197
Binary files /dev/null and b/linesider_screen_bankend/linesider_screen_bankend/Resources/gxassembly_production_label.btw differ
diff --git a/linesider_screen_bankend/linesider_screen_bankend/appsettings.json b/linesider_screen_bankend/linesider_screen_bankend/appsettings.json
index 6f911ce..afad165 100644
--- a/linesider_screen_bankend/linesider_screen_bankend/appsettings.json
+++ b/linesider_screen_bankend/linesider_screen_bankend/appsettings.json
@@ -1,4 +1,7 @@
{
- "label_path": "C:\\Labels\\MyLabel.btw" //标签路径
+
+ "label_path": "./Resources/gxassembly_production_label.btw" //标签路径
+
+
}
\ No newline at end of file