在伺服器上安裝bartender 軟體,建立一個新的标簽模闆
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5CM4ITN5kTO2ETMtMTNyEzM3MjNxYjM2ATMyAjMtQzN2QzM48CX2ATMyAjMvwFN3YDNzgzLcd2bsJ2Lc12bj5ycn9Gbi52YuAjMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
添加引用
Camstar.AppServer.BusinessLogic.CustomProcImpl.dll,Camstar.AppServer.BusinessLogic.CustomProcParameters.dll,Newtonsoft.Json.dll在C:\Program Files (x86)\Camstar\InSite Server目錄,
Seagull.BarTender.Print.dll在C:\Program Files\Seagull\BarTender 2021\SDK\Assemblies
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Camstar.AppServer.BusinessLogic;
using Newtonsoft.Json;
using Seagull.BarTender.Print;
namespace BTDemo
{
[ClassInterface(ClassInterfaceType.None)]
public class BarTend : CustomProcImpl
{
protected override bool Execute()
{
try
{
var data = this.GetInputParam("PrintData").ToString();//列印資料
var temp = this.GetInputParam("PrintTemp").ToString();//列印模闆路徑
var row = (int)this.GetInputParam("PrintRow");//列印行數
var col =(int) this.GetInputParam("PrintCol");//列印列數
var printer= this.GetInputParam("Printer").ToString();//列印機
var values= JsonConvert.DeserializeObject<IEnumerable< PrintData>>(data);
if (values.Any())
{
// LogInfo ($"PrintData:{data}\nPrintTemp{temp}\nPrinter:{printer}");
Engine engine = new Engine(true);
var format = engine.Documents.Open(temp);
foreach (var subString in format.SubStrings)
{
var val = values.FirstOrDefault(m => m.Name == subString.Name);
//注冊資料
if (val!=null)
{
subString.Value = val.Value;
}
}
format.PrintSetup.PrinterName = printer;//列印機
format.PrintSetup.IdenticalCopiesOfLabel = row; //設定同序列列印的份數
format.PrintSetup.NumberOfSerializedLabels = col; //設定需要列印的序列數
var result = format.Print("LabelPrint", 60, out var messages);
SetOutputParam("PrintStatus", result == Result.Success ? "1" : "0");
SetOutputParam("PrintMessage",string.Join("\n", from message in messages select message.Text));
if (engine != null)
engine.Stop(SaveOptions.DoNotSaveChanges);
}
return true;
}
catch (Exception ex)
{
this.ErrorLabelName = "ErrorText";
this.SetErrorParam("ErrorMessage", (object)ex.ToString());
this.SetErrorParam("FunctionName", (object)ex.GetType().FullName);
this.LogError(ex.ToString());
return false;
}
}
public void LogError(string error)
{
try
{
string source = "BarTend";
string logName = "CustProc";
if (!EventLog.SourceExists(source))
EventLog.CreateEventSource(new EventSourceCreationData(source, logName));
new EventLog() { Source = source }.WriteEntry(error, EventLogEntryType.Error);
}
catch (Exception ex)
{
}
}
public void LogInfo(string error)
{
try
{
string source = "BarTend";
string logName = "CustProc";
if (!EventLog.SourceExists(source))
EventLog.CreateEventSource(new EventSourceCreationData(source, logName));
new EventLog() { Source = source }.WriteEntry(error, EventLogEntryType.Information);
}
catch (Exception ex)
{
}
}
}
public class PrintData
{
public string Name { get; set; }
public string Value { get; set; }
public string DataType { get; set; }
}
}
生成項目,把檔案複制到insite server
打開desinger 添加userfunction,除了classname為固定以外,其他參數和上面類庫項目代碼的input output參數對應
然後添加一個server和CLF來調用這個function
添加到事件
updateDB,然後使用API調用看
檢視輸出的PDF檔案