c# 使用bartender sdk 打印

  • Post author:
  • Post category:其他


在网上参考了其它的资料,研究了bartender打印,走了不少弯路。

安装完bartender后,装全功能版本,在安装路径下,可以找到sdk文件,我需要根据传入的excel文件,检查bartender标签中,有没有指定的列进行打印, barterder10.0版本不支持返回当前标签文件有哪些嵌入式字段,10.1支持,所以只好装了10.1版本,安装了全功能试用版本

引用Seagull.BarTender.Print.dll即可,在安装程序目录下可以找到, 调用代码如下

using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;

namespace libPrintBarTender
{
    public class CPrinter
    {
        /// <summary>
        ///   dtPrint.Columns.Add("EZPrinter");       // 打印机名字
        ///      dtPrint.Columns.Add("EZLabelFile");     // 打印的模板文件
        ///      dtPrint.Columns.Add("EZLabelType");     // 打印的类型    Label 标签
        ///       dtPrint.Columns.Add("EZLabelCopies");   // 打印的份数
        ///       dtPrint.Columns.Add("EZPrintMission");  // 默认值1
        /// </summary>
        /// <param name="dtPrintInfo"></param>
        public void PrintLabel(DataTable dtPrintInfo)
        {
            Engine engine = null;
            LabelFormatDocument btFormat = null;
            try
            {
                if (dtPrintInfo.Rows.Count == 0)
                {
                    return;
                }
                engine = new Engine(true);
                if (engine == null)
                {
                    throw new Exception("初始化打印机失败,请检查bartender软件!");
                }

                string printerName = dtPrintInfo.Rows[0]["EZPrinter"].ToString();
                string labelFile = dtPrintInfo.Rows[0]["EZLabelFile"].ToString();
                try
                {
                    btFormat = engine.Documents.Open(labelFile, printerName);
                }
                catch (Exception ex)
                {
                    throw new Exception("打开模块与打印机失败:" + ex.Message);
                }
                // 打印标签
                PrintInfo(btFormat, dtPrintInfo);

                if (btFormat != null)
                {
                    btFormat.Close(SaveOptions.DoNotSaveChanges);
                }
                if (engine != null)
                {
                    engine.Stop(SaveOptions.DoNotSaveChanges);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("打印失败:" + ex.Message);
            }
            finally
            {
            }
        }

        private static List<string> GetPrintLabelTemplateFields(LabelFormatDocument btFormat)
        {
            List<string> colLists = new List<string>(btFormat.SubStrings.Count);
            for(int i=0; i< btFormat.SubStrings.Count; i++ )
            {
                colLists.Add(btFormat.SubStrings[i].Name);
            }
            return colLists;
        }

        private static void RemoveMuchFields(LabelFormatDocument btFormat, ref DataTable dtPrintInfo)
        {
            List<string> colLists = GetPrintLabelTemplateFields(btFormat);
            int m = 0;
            while (dtPrintInfo.Columns.Count > m)
            {
                string colName = dtPrintInfo.Columns[m].ColumnName;
                if (colLists.Contains(colName))
                {
                    m++;
                    continue;
                }
                else if (string.Equals(colName, "EZLabelCopies"))
                {
                    m++;
                    continue;
                }
                else
                {
                    dtPrintInfo.Columns.RemoveAt(m);
                }
            }
        }

        private static void PrintInfo(LabelFormatDocument btFormat, DataTable dtPrintInfo)
        {
            bool bSerializedLabels = btFormat.PrintSetup.SupportsSerializedLabels;
            bool bCopies = btFormat.PrintSetup.SupportsIdenticalCopies;
            int nCopyies = 0;

            // 去除多余的字段
            RemoveMuchFields(btFormat, ref dtPrintInfo);
            if (dtPrintInfo.Columns.Count <= 1)
            {
                return;     // 没有要打印的字段了
            }

            for (int i = 0; i < dtPrintInfo.Rows.Count; i++)
            {
                string copies = dtPrintInfo.Rows[i]["EZLabelCopies"].ToString();
                nCopyies = copies.Length > 0 ? int.Parse(copies) : 1;
                if (nCopyies <= 0)
                {
                    continue;
                }

                if (bCopies)        // 打印机驱动是否支持打印份数
                {
                    btFormat.PrintSetup.IdenticalCopiesOfLabel = nCopyies;
                    nCopyies = 1;
                }

                if (bSerializedLabels)
                {
                    btFormat.PrintSetup.NumberOfSerializedLabels = 1;
                }
                for (int m = 1; m <= nCopyies; m++)
                {
                    for (int j = 0; j < dtPrintInfo.Columns.Count; j++)
                    {
                        string fieldName = dtPrintInfo.Columns[j].ToString();
                        if (string.Equals(fieldName, "EZLabelCopies"))
                        {
                            continue;
                        }
                        btFormat.SubStrings[fieldName].Value = dtPrintInfo.Rows[i][fieldName].ToString();
                    }

                    Messages messages;
                    int waitForCompletionTimeout = 10000; // 10 seconds
                    Result result = btFormat.Print("Label Print", waitForCompletionTimeout, out messages);
                    if (result == Result.Failure)
                    {
                        string messageString = "";
                        foreach (Seagull.BarTender.Print.Message message in messages)
                        {
                            messageString += "\n" + message.Text;
                        }
                        throw new Exception(string.Format("打印第[{0}]标签失败,原因:{1}!", i + 1, messageString));
                    }
                }
            }
        }


    }
}





版权声明:本文为liangzhonglin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。