|
|
@@ -0,0 +1,128 @@
|
|
|
+package com.mes.print.bartender;
|
|
|
+
|
|
|
+import com.jacob.activeX.ActiveXComponent;
|
|
|
+import com.jacob.com.Dispatch;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * BarTender 打印 - 通过文本文件数据源
|
|
|
+ */
|
|
|
+public class BarTenderFilePrinter {
|
|
|
+
|
|
|
+ private static final String DATA_FILE = "C:\\Users\\jcxa\\Desktop\\dydriver\\BtMsg1.txt";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打印标签
|
|
|
+ * @param sn 批次号(如 +KB60IS3031T81L000401)
|
|
|
+ * @param printIndex 打印序号(如 4)
|
|
|
+ */
|
|
|
+ public void printLabel(String sn, int printIndex) {
|
|
|
+ try {
|
|
|
+ // 1. 生成数据文件
|
|
|
+ generateDataFile(sn, printIndex);
|
|
|
+
|
|
|
+ // 2. 确定模板文件
|
|
|
+ String templatePath = getTemplatePath(sn);
|
|
|
+
|
|
|
+ // 3. 调用 BarTender 打印
|
|
|
+ printWithBarTender(templatePath);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("打印失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成数据文件
|
|
|
+ */
|
|
|
+ private void generateDataFile(String sn, int printIndex) throws Exception {
|
|
|
+ // 根据 SN 前缀判断产品型号
|
|
|
+ String productModel = getProductModel(sn);
|
|
|
+
|
|
|
+ // 生成日期
|
|
|
+ String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
|
|
|
+
|
|
|
+ // 生成流水号(7位补零)
|
|
|
+ String serialNo = String.format("%07d", printIndex);
|
|
|
+
|
|
|
+ // 写入文件(制表符分隔)
|
|
|
+ try (OutputStreamWriter writer = new OutputStreamWriter(
|
|
|
+ new FileOutputStream(DATA_FILE), StandardCharsets.UTF_8)) {
|
|
|
+
|
|
|
+ writer.write("广西巨石\t");
|
|
|
+ writer.write("新能源科技有限公司\t");
|
|
|
+ writer.write("高压电池下壳体总成\t");
|
|
|
+ writer.write(productModel + "\t");
|
|
|
+ writer.write(sn + "\t");
|
|
|
+ writer.write(date + "\t");
|
|
|
+ writer.write(serialNo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据 SN 前缀获取模板路径
|
|
|
+ */
|
|
|
+ private String getTemplatePath(String sn) {
|
|
|
+ if (sn.startsWith("+KB60")) {
|
|
|
+ return "C:\\Users\\jcxa\\Desktop\\dydriver\\T09.btw";
|
|
|
+ } else if (sn.startsWith("+KB87")) {
|
|
|
+ return "C:\\Users\\jcxa\\Desktop\\dydriver\\T68.btw";
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("未知的 SN 前缀: " + sn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据 SN 前缀获取产品型号
|
|
|
+ */
|
|
|
+ private String getProductModel(String sn) {
|
|
|
+ if (sn.startsWith("+KB60")) {
|
|
|
+ return "2510094CDE2200"; // T09
|
|
|
+ } else if (sn.startsWith("+KB87")) {
|
|
|
+ return "2510094ANB0900"; // T68
|
|
|
+ } else {
|
|
|
+ return "UNKNOWN";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用 BarTender 打印
|
|
|
+ */
|
|
|
+ private void printWithBarTender(String templatePath) {
|
|
|
+ ActiveXComponent btApp = null;
|
|
|
+ Dispatch btFormat = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建 BarTender Application
|
|
|
+ btApp = new ActiveXComponent("BarTender.Application");
|
|
|
+
|
|
|
+ // 打开模板(带密码)
|
|
|
+ Dispatch btFormats = btApp.getProperty("Formats").toDispatch();
|
|
|
+ btFormat = Dispatch.call(btFormats, "Open", templatePath, false, "123654").toDispatch();
|
|
|
+
|
|
|
+ // 打印(不显示对话框)
|
|
|
+ Dispatch.call(btFormat, "PrintOut", false, false);
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ // 关闭
|
|
|
+ if (btFormat != null) {
|
|
|
+ try {
|
|
|
+ Dispatch.call(btFormat, "Close", 2); // btDoNotSaveChanges
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 忽略
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (btApp != null) {
|
|
|
+ try {
|
|
|
+ btApp.invoke("Quit", 2);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 忽略
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|