c# 使用bartender sdk 打印
来源:互联网 发布:matlab定义未知数组 编辑:程序博客网 时间:2024/06/06 20:18
在网上参考了其它的资料,研究了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)); } } } } }}
0 0
- c# 使用bartender sdk 打印
- C#打印条码BarTender SDK
- C#使用BarTender打印Label
- c# BarTender打印
- 使用Delphi调用条形码控件BarTender打印标签
- bartender打印错误
- [Bartender]C#调用BartenderSDK使用场景-客户标签案列
- C#调用BarTender 错误
- C#调用BarTender 模板
- Bartender条码使用方法C#
- Delphi 调用BarTender打印条码
- Bartender打印和导出图片
- BarTender如何打印额外字符
- Delphi 调用BarTender打印条码
- Bartender使用心得
- [C#]调用Bartender服务并打印bartender标签
- 使用bartender打印RFID标签如何正确写入EPCID(intermec的RFID条码打印机)
- BarTender批量打印标签的教程
- 空中网线程题(3题)
- ubuntu环境下python爬虫项目需要的模块
- Ubuntu下安装visual studio code
- DATE和TIMESTAMP数据类型
- 设计十道随机问题
- c# 使用bartender sdk 打印
- Python学习笔记(三)
- Google 面试题 10 | 最多有k个不同字符的最长子字符串
- POJ 1502 MPI Maelstrom (SPFA)
- 插入排序的疑问
- ON、WHERE、HAVING的区别
- php多进程处理
- Android学习路线图
- orcale 中varchar2 和nvarchar2的区别