调整
This commit is contained in:
parent
a54269ef87
commit
be1c77de18
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量打印多个List标签
|
||||
/// </summary>
|
||||
/// <param name="templatePath">标签模板路径</param>
|
||||
/// <param name="items">包含多个标签数据的列表</param>
|
||||
/// <param name="copiesPerItem">每个标签的打印份数</param>
|
||||
/// <param name="serializedLabels">序列化标签数量</param>
|
||||
/// <param name="getSubStringValues">从列表项获取标签变量的委托</param>
|
||||
/// <returns>是否全部打印成功</returns>
|
||||
public bool PrintLabels<T>(
|
||||
string templatePath,
|
||||
List<T> items,
|
||||
int copiesPerItem = 1,
|
||||
int serializedLabels = 1,
|
||||
Func<T, Dictionary<string, string>> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 批量打印多个标签(新增方法)
|
||||
/// </summary>
|
||||
/// <param name="templatePath">标签模板路径</param>
|
||||
/// <param name="batchSubStringValues">多个标签的变量键值对列表</param>
|
||||
/// <param name="copiesPerLabel">每个标签的打印份数</param>
|
||||
/// <param name="serializedLabels">序列化标签数量</param>
|
||||
/// <returns>是否全部打印成功</returns>
|
||||
public bool PrintBatchLabels(
|
||||
string templatePath,
|
||||
List<Dictionary<string, string>> 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);
|
||||
|
||||
@ -24,6 +24,8 @@ namespace linesider_screen_bankend.Controller
|
||||
public class OnlyPrintProductLabelController : WebApiController
|
||||
{
|
||||
private readonly Action<string> _logAction;
|
||||
// 准备打印数据
|
||||
string templatePath = ConfigurationManager.AppSettings["label_path"];
|
||||
|
||||
public OnlyPrintProductLabelController(Action<string> logAction)
|
||||
{
|
||||
@ -49,9 +51,6 @@ namespace linesider_screen_bankend.Controller
|
||||
string model = queryParams["model"];
|
||||
string direction = queryParams["direction"];
|
||||
|
||||
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
ProWorkorder workorderInfo = SqlSugarHelper.Db.Queryable<ProWorkorder>()
|
||||
@ -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<string, string>
|
||||
{
|
||||
{ "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<ProWorkorder>()
|
||||
.Where(x => x.Workorder == workorder)
|
||||
.First();
|
||||
|
||||
|
||||
// 使用 using 语句确保资源正确释放
|
||||
using (var bartender = new BartenderPrintHelper())
|
||||
{
|
||||
// 准备打印数据
|
||||
string templatePath = ConfigurationManager.AppSettings["label_path"];
|
||||
|
||||
var variables = new Dictionary<string, string>
|
||||
{
|
||||
{ "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<string, string>
|
||||
{
|
||||
{ "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);
|
||||
}
|
||||
|
||||
Binary file not shown.
@ -1,4 +1,7 @@
|
||||
{
|
||||
|
||||
"label_path": "C:\\Labels\\MyLabel.btw" //标签路径
|
||||
|
||||
"label_path": "./Resources/gxassembly_production_label.btw" //标签路径
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user