添加项目文件。
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
using JinYuan.Helper;
|
||||
using Seagull.BarTender.Print;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
|
||||
namespace JinYuan.VirtualDataLibrary.Utlis
|
||||
{
|
||||
public class BartenderPrinter
|
||||
{
|
||||
LabelFormatDocument print;
|
||||
Engine engine;
|
||||
string PrinterName;
|
||||
string PrintLabelType;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化打印机
|
||||
/// </summary>
|
||||
/// <param name="PrinterName">打印机名称通过该名称指定打印机</param>
|
||||
/// <param name="PrintLabelType">打印机类型</param>
|
||||
public BartenderPrinter(string PrinterName, string PrintLabelType)
|
||||
{
|
||||
this.PrinterName = PrinterName;
|
||||
this.PrintLabelType = PrintLabelType;
|
||||
engine = new Engine(true);
|
||||
}
|
||||
|
||||
public BartenderPrinter(string PrinterName)
|
||||
{
|
||||
this.PrinterName = PrinterName;
|
||||
engine = new Engine(true);
|
||||
}
|
||||
|
||||
|
||||
public string[] read(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
LabelFormatDocument print;
|
||||
|
||||
|
||||
//System.Diagnostics.Process.Start("ExpLore", path);
|
||||
|
||||
|
||||
print = engine.Documents.Open(path);
|
||||
//print.PrintSetup.PrinterName = "Microsoft Print to PDF";
|
||||
//print.PrintSetup.PrinterName = "TSC MX340F";
|
||||
XmlDocument xml = new XmlDocument();
|
||||
xml.LoadXml(print.SubStrings.XML);
|
||||
//string s = print.SubStrings.XML;
|
||||
//获取XML中SubStrings/SubString
|
||||
XmlNodeList nodeList = xml.SelectNodes("SubStrings/SubString");
|
||||
string[] printColumns = new string[print.SubStrings.Count];
|
||||
int count = 0;
|
||||
foreach (XmlNode node in nodeList)
|
||||
{
|
||||
printColumns[count] = node.Attributes["Name"].Value;
|
||||
count++;
|
||||
}
|
||||
//获取栏位
|
||||
return printColumns;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行打印
|
||||
/// </summary>
|
||||
/// <param name="path">文件地址</param>
|
||||
/// <param name="printValue">字段栏位映射,key BTname ,value BTValue</param>
|
||||
/// <returns></returns>
|
||||
public bool write(string path, string jobName, Dictionary<string, string> printValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
print = engine.Documents.Open(path);
|
||||
//print.PrintSetup.PrinterName = "Microsoft Print to PDF";
|
||||
//print.PrintSetup.PrinterName = "TSC MX340";
|
||||
//print.PrintSetup.PrinterName = "TSC TTP-346MU";
|
||||
print.PrintSetup.PrinterName = this.PrinterName;
|
||||
foreach (var oneItem in printValue)
|
||||
{
|
||||
print.SubStrings.SetSubString(oneItem.Key, oneItem.Value);
|
||||
//log.Debug("[" + oneItem.Key + "]=" + oneItem.Value);
|
||||
}
|
||||
//Thread.Sleep(100);
|
||||
|
||||
Result result = print.Print(jobName); //print.Print("Label Print");
|
||||
|
||||
return result == Result.Success ? true : false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取btw檔 并确认各栏位是否能对应的到
|
||||
/// </summary>
|
||||
/// <param name="FileId"></param>
|
||||
/// <param name="Path"></param>
|
||||
/// <param name="SourceDataTable"></param>
|
||||
/// <param name="DtPrintColumn"></param>
|
||||
/// <returns></returns>
|
||||
public string LoadColumns(string FileId, string Path, DataTable SourceDataTable, DataTable DtPrintColumn)
|
||||
{
|
||||
try
|
||||
{
|
||||
string msg = "";
|
||||
|
||||
string[] btwColumns = read(Path);
|
||||
for (int i = 0; i < btwColumns.Length; i++)//版面欄位數量
|
||||
{
|
||||
bool flag = false;
|
||||
foreach (DataColumn dc in SourceDataTable.Columns)//資料庫欄位數量
|
||||
{
|
||||
if (btwColumns[i] == dc.ColumnName)
|
||||
{
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag == false)
|
||||
{
|
||||
if (msg == "") { msg = btwColumns[i]; }
|
||||
else { msg += "," + btwColumns[i]; }
|
||||
}
|
||||
}
|
||||
|
||||
if (msg == "")
|
||||
{
|
||||
DataTable dt = DtPrintColumn;
|
||||
string Id = FileId;
|
||||
|
||||
DataRow[] drAry = dt.Select(" Id='" + Id + "'");
|
||||
for (int j = 0; j < drAry.Length; j++)
|
||||
{
|
||||
dt.Rows.Remove(drAry[j]);
|
||||
}
|
||||
dt.AcceptChanges();
|
||||
|
||||
DataRow drNew;
|
||||
for (int j = 0; j < btwColumns.Length; j++)
|
||||
{
|
||||
drNew = dt.NewRow();
|
||||
drNew["Id"] = Id;
|
||||
drNew["Value"] = btwColumns[j];
|
||||
dt.Rows.Add(drNew);
|
||||
}
|
||||
dt.AcceptChanges();
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
return ex.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
// 列印 Reel或Box或Carton标签
|
||||
// 偵測非同步處理
|
||||
public bool PrintLabel(DataRow dr)
|
||||
{
|
||||
try
|
||||
{
|
||||
string Id = "";
|
||||
string fileIdColumnName = "";
|
||||
string filePathColumnName = "";
|
||||
if (dr == null)
|
||||
{
|
||||
LogHelper.Instance.WriteError("DataRow is NULL");
|
||||
return false;
|
||||
}
|
||||
DataTable columnDT = new DataTable();
|
||||
if (PrintLabelType == "Box")
|
||||
{
|
||||
fileIdColumnName = "FileId";
|
||||
filePathColumnName = "Path";
|
||||
|
||||
}
|
||||
else if (PrintLabelType == "Reel")
|
||||
{
|
||||
fileIdColumnName = "ReelFileId";
|
||||
filePathColumnName = "ReelFilePath";
|
||||
|
||||
}
|
||||
else if (PrintLabelType == "Carton")
|
||||
{
|
||||
fileIdColumnName = "FileId";
|
||||
filePathColumnName = "Path";
|
||||
|
||||
}
|
||||
else if (PrintLabelType == "Carton2")
|
||||
{
|
||||
fileIdColumnName = "FileId2";
|
||||
filePathColumnName = "Path2";
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHelper.Instance.WriteError("PrintLabelType =" + PrintLabelType);
|
||||
return false;
|
||||
}
|
||||
|
||||
Id = dr[fileIdColumnName].ToString();
|
||||
|
||||
DataRow[] drAry = columnDT.Select("id='" + Id + "'");
|
||||
|
||||
Dictionary<string, string> printValue = new Dictionary<string, string>();
|
||||
|
||||
for (int i = 0; i < drAry.Length; i++)
|
||||
{
|
||||
string column = drAry[i][1].ToString();
|
||||
|
||||
printValue.Add(column, dr[column].ToString().TrimEnd());
|
||||
|
||||
}
|
||||
bool writeResult = write(dr[filePathColumnName].ToString(), PrintLabelType, printValue);
|
||||
return writeResult;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
//列印 Interior Label 或 Outer Label
|
||||
public bool PrintSimpleLabel(string chrCkdh, string jobname, string FilePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Dictionary<string, string> printValue = new Dictionary<string, string>();
|
||||
printValue.Add("chrCkdh", chrCkdh.TrimEnd());
|
||||
bool writeResult = write(FilePath, jobname, printValue);
|
||||
return writeResult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
engine.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user