opcenter调用bartender打印标签

在服务器上安装bartender 软件,创建一个新的标签模板

添加一个输入框

右键点击输入框,打开属性对话框

命名数据源的名字为Test,关闭对话框,然后保存模板

打开VS创建一个framework类库BTDemo,打开项目属性对话框,按下图进行更改(EA1BEC9E-833F-4227-8130-9AA0B68E611B)

添加引用

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

添加类BarTend

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文件

 

posted @ 2021-06-26 16:50  悳鎭  阅读(690)  评论(1编辑  收藏  举报