2025-10-31 09:56:25 +08:00

101 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BarTender;
using Microsoft.Extensions.Logging;
using Syc.Basic.Web.WMS.Dto;
using Syc.Basic.Web.WMS.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Syc.Basic.Web.WMS
{
public class BarTenderHelper
{
/// <summary>
/// 打印标签
/// </summary>
/// <param name="templatePath">模板全路径D://AD盘/新建文件夹/xxxx.btw</param>
/// <param name="deviceName">打印机名称</param>
/// <param name="@params">模板变量参数</param>
/// <returns></returns>
public static void Print(string templatePath,string deviceName,Dictionary<string,string> @params = null)
{
Console.WriteLine("开始打印");
Application bartenderApp = new Application();
var btFormat = bartenderApp.Formats.Open(templatePath,false,deviceName);
if (@params is not null && @params.Any())
{
foreach (var param in @params)
btFormat.SetNamedSubStringValue(param.Key, param.Value);
}
btFormat.IdenticalCopiesOfLabel = 1;
//设置打印份数,自动贴标就只出一张
BarTender.Messages btMsgs;
//开始打印
var btPrintRtn = btFormat.Print(PrintJobName:"打印标签",WaitForSpoolJobToComplete:false,Messages:out btMsgs);
if (btPrintRtn != BarTender.BtPrintResult.btSuccess)
foreach (BarTender.Message msg in btMsgs)
{
var text = msg.Message;
}
//输出错误日志
if (btPrintRtn != BarTender.BtPrintResult.btSuccess)
{
foreach(BarTender.Message msg in btMsgs)
{
var text = msg.Message;
}
}
//关闭模板
btFormat.Close(BarTender.BtSaveOptions.btPromptSave);
//退出打印程序
bartenderApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
}
/// <summary>
/// 打印箱子标签
/// </summary>
/// <param name="box"></param>
public static void BoxPrint(Box box)
{
Dictionary<string, string> list = new Dictionary<string, string>()
{
{"$TYPE", box.Type },
{"$SPEC", box.Spec },
{"$QTY", box.Qty.ToString()},
{"$CTN", Math.Round(box.Net_Weight.Value,2).ToString()},
{ "$LOTNO", box.Lot_No },
{"$LENGTH", box.Length },
{ "$DATE", box.Dom_Time.ToString()},
{ "$EXP", box.Exp_Time.ToString()},
{ "$CODE", box.Code }
};
Print(@"D:\btw\装箱码垛标签.btw", "NOVEXX Solutions XLP 504 (300 dpi)Max", list);
}
/// <summary>
/// 打印丝锭标签
/// </summary>
/// <param name="box"></param>
public static void SilkPrint(Silk silk)
{
Dictionary<string, string> list = new Dictionary<string, string>()
{
{"$Name", silk.Name },
{"$Type", silk.Type },
{"$NetWeight", Math.Round(silk.Net_Weight.Value,2).ToString() },
{ "$Length", silk.Length},
{"$Date", silk.Date.ToString() },
{ "$LotNo", silk.Lot_No},
{ "$Code", silk.Code}
};
Print(@"D:\btw\丝锭标签.btw", "NOVEXX Solutions XLP 504 (300 dpi)Min", list);
}
}
}