修改打印逻辑

This commit is contained in:
小魔仙
2025-05-15 11:22:42 +08:00
parent e8e5b22407
commit e253204f8d
3 changed files with 64 additions and 4 deletions

View File

@@ -1,9 +1,11 @@
using System;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace linesider_screen_tool
{
@@ -12,7 +14,7 @@ namespace linesider_screen_tool
private const string BarTenderProgId = "BarTender.Application";
private object? _btApp; // 声明为可空类型
private int _disposedValue;
public BartenderPrintHelper()
{
// 延迟初始化,在首次使用时创建 Bartender 应用实例
@@ -38,6 +40,51 @@ namespace linesider_screen_tool
}, templatePath);
}
public Task<List<string>> GetNamedSubStrings(string templatePath)
{
dynamic format =null;
dynamic btApp = null;
try
{
btApp = Activator.CreateInstance(Type.GetTypeFromProgID(BarTenderProgId));
format = btApp.Formats.Open(
templatePath,
false,
null
);
format.PrintSetup.IdenticalCopiesOfLabel = 1;
format.PrintSetup.NumberSerializedLabels = 1;
List<string> subStrings = new List<string>();
dynamic namedSubStrings = format.NamedSubStrings;
foreach (var iteam in namedSubStrings)
{
subStrings.Add(iteam.Name);
}
return Task.FromResult(subStrings);
}
catch (Exception ex)
{
Console.WriteLine($"发生错误: {ex.Message}");
return null;
}
finally
{
// 清理资源
if (format != null)
{
Marshal.ReleaseComObject(format);
}
if (btApp != null)
{
btApp.Quit(0);
Marshal.ReleaseComObject(_btApp);
}
}
}
/// <summary>
/// 批量打印标签(高性能实现)
/// </summary>