35 changed files with 1189 additions and 695 deletions
@ -1,327 +1,341 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace BBWY.Client.Helpers |
|||
{ |
|||
public class BartenderHelper |
|||
{ |
|||
#region BarTender打印方法
|
|||
private static BarTender.Application btApp = new BarTender.Application(); |
|||
private static BarTender.Format btFormat = new BarTender.Format(); |
|||
public string TemplateFile { set; get; } |
|||
public string ErrorMsg { set; get; } |
|||
public bool Status { set; get; } |
|||
/// <summary>
|
|||
/// 使用 BarTender进行打印,批量打印
|
|||
/// </summary>
|
|||
/// <param name="labelName"> BarTender模板标签</param>
|
|||
/// <param name="labelParamsValuesList">打印数据</param>
|
|||
/// <param name="msg">回调消息</param>
|
|||
/// <param name="printQty">打印数量</param>
|
|||
/// <returns>打印OK true</returns>
|
|||
public bool BTPrint(string labelName, Dictionary<string, string> labelParamsValues, out string msg, int printQty = 1, string printer = "Deli DL-730C(NEW)") |
|||
{ |
|||
try |
|||
{ |
|||
bool checkLabelVar = true; |
|||
string tmpMsg = ""; |
|||
this.TemplateFile = labelName; |
|||
|
|||
if (!File.Exists(TemplateFile)) |
|||
{ |
|||
msg = "Error:Label File Not Exists" + TemplateFile; |
|||
this.ErrorMsg = msg; |
|||
return false; |
|||
} |
|||
if (!string.IsNullOrEmpty(printer)) |
|||
{ |
|||
bool printerFlag = false; |
|||
foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取所有打印机名称
|
|||
{ |
|||
if (sPrint.Equals(printer)) |
|||
{ |
|||
printerFlag = true; |
|||
} |
|||
} |
|||
|
|||
if (!printerFlag) |
|||
{ |
|||
msg = "Error:Printer Name Error"; |
|||
return false; |
|||
} |
|||
|
|||
|
|||
} |
|||
//找到打印模板的标签页
|
|||
btFormat = btApp.Formats.Open(labelName, false, ""); |
|||
if (!string.IsNullOrEmpty(printer)) |
|||
{ |
|||
btFormat.Printer = printer; |
|||
} |
|||
if (btApp != null) |
|||
{ |
|||
//btFormat.IdenticalCopiesOfLabel = printQty;
|
|||
//btFormat.PrintOut(true, false);
|
|||
|
|||
//取模板所有的值
|
|||
string strBT = btFormat.NamedSubStrings.GetAll("#", "$"); |
|||
|
|||
//将模板所有值进行拆分
|
|||
strBT = strBT.Substring(0, strBT.Length - 1); |
|||
string[] strBTValue = strBT.Split(new char[] { '$' }); |
|||
|
|||
//循环将模板的值写入lbldicVar中
|
|||
Dictionary<string, string> lbldicVar = new Dictionary<string, string>(); |
|||
for (int i = 0; i < strBTValue.Length; i++) |
|||
{ |
|||
string[] cc = strBTValue[i].Split(new char[] { '#' }); |
|||
|
|||
lbldicVar.Add(cc[0].ToString(), cc[1].ToString()); //0是模板的参数 1是参数值
|
|||
} |
|||
if (labelParamsValues.Count > 0) |
|||
{ |
|||
foreach (var item in lbldicVar) |
|||
{ |
|||
if (labelParamsValues.Count(q => q.Key.ToUpper() == item.Key.ToUpper()) == 0) |
|||
{ |
|||
tmpMsg = "数据源缺少参数: " + item.Key; |
|||
if (checkLabelVar) |
|||
{ |
|||
this.ErrorMsg = tmpMsg; |
|||
checkLabelVar = false; |
|||
} |
|||
else |
|||
{ |
|||
this.ErrorMsg = this.ErrorMsg + (char)10 + tmpMsg; |
|||
} |
|||
} |
|||
} |
|||
if (!checkLabelVar) |
|||
{ |
|||
msg = this.ErrorMsg; |
|||
this.Status = false; |
|||
return false; |
|||
} |
|||
foreach (var param in labelParamsValues.Keys) |
|||
{ |
|||
if (lbldicVar.Keys.Count(q => q.ToUpper() == param.ToUpper()) > 0) |
|||
{ |
|||
var labvar = lbldicVar.Keys.First(q => q.ToUpper() == param.ToUpper()); |
|||
|
|||
if (string.IsNullOrEmpty(labelParamsValues[param])) |
|||
{ |
|||
tmpMsg = "数据源参数值为空:" + labvar; |
|||
if (checkLabelVar) |
|||
{ |
|||
this.ErrorMsg = tmpMsg; |
|||
checkLabelVar = false; |
|||
} |
|||
else |
|||
{ |
|||
this.ErrorMsg = this.ErrorMsg + (char)10 + tmpMsg; |
|||
} |
|||
} |
|||
btFormat.SetNamedSubStringValue(param, labelParamsValues[param]); |
|||
} |
|||
} |
|||
if (!checkLabelVar) |
|||
{ |
|||
msg = this.ErrorMsg; |
|||
this.Status = false; |
|||
return false; |
|||
} |
|||
btFormat.IdenticalCopiesOfLabel = printQty; |
|||
|
|||
//第二个false设置打印时是否跳出打印属性
|
|||
btFormat.PrintOut(true, false); |
|||
//退出是是否保存标签
|
|||
btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); |
|||
//lblDoc.FormFeed();
|
|||
msg = "OK"; |
|||
this.ErrorMsg = msg; |
|||
this.Status = true; |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
msg = "Print Data is null"; |
|||
this.ErrorMsg = msg; |
|||
this.Status = false; |
|||
return false; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
msg = "Error:CodeSoft Application can't boot up"; |
|||
this.ErrorMsg = msg; |
|||
this.Status = false; |
|||
return false; |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
msg = ex.Message + ex.InnerException; |
|||
this.ErrorMsg = msg; |
|||
this.Status = false; |
|||
return false; |
|||
} |
|||
finally |
|||
{ |
|||
if (btApp != null) |
|||
{ |
|||
btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出时同步退出bartender进程
|
|||
//lblApp.Documents.CloseAll(true);
|
|||
//lblDoc = null;
|
|||
} |
|||
GC.Collect(); |
|||
} |
|||
} |
|||
#endregion
|
|||
|
|||
#region 数据库调用打印
|
|||
/// <summary>
|
|||
/// 打印从文本数据库中打
|
|||
/// </summary>
|
|||
/// <returns></returns>
|
|||
public bool BTPrintByDataBase(string path, string filePath, string printer = "") |
|||
{ |
|||
|
|||
try |
|||
{ |
|||
BarTender.Application btapp; |
|||
BarTender.Format btformat; |
|||
btapp = new BarTender.Application(); |
|||
btformat = btapp.Formats.Open(path, false, ""); |
|||
BarTender.Database database = btformat.Databases.GetDatabase(1); |
|||
if (!string.IsNullOrEmpty(printer)) |
|||
{ |
|||
btformat.Printer = printer; |
|||
} |
|||
|
|||
database.TextFile.FileName = filePath; |
|||
|
|||
//database.SQLStatement += $" where printID='{printId}' order by RIGHT(Var1,7)";//填写SQL语句附加条件
|
|||
//database.Password = VSLoginInfo.SQLPassword;
|
|||
//database.User = VSLoginInfo.SQLUser;
|
|||
btformat.PrintOut(false, false); //第二个参数设置是否跳出打印属性;
|
|||
btapp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);//退出时同步退出bartender进程(SQL条件会改变,不更新模板信息)
|
|||
|
|||
} |
|||
catch |
|||
{ |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
|
|||
/// <summary>
|
|||
/// 打印从文本数据库中打
|
|||
/// </summary>
|
|||
/// <param name="templatepath">模板地址</param>
|
|||
/// <param name="dataLine">数据行,一行一个标签,第一行为标题,多列逗号分割</param>
|
|||
/// <returns></returns>
|
|||
public bool BTPrintByDataBase(string templatepath, List<string> dataLine, string printer = "") |
|||
{ |
|||
|
|||
try |
|||
{ |
|||
BarTender.Application btapp; |
|||
BarTender.Format btformat; |
|||
btapp = new BarTender.Application(); |
|||
var filepath = WriteLog(dataLine, "", Guid.NewGuid().ToString()); |
|||
btformat = btapp.Formats.Open(templatepath, false, ""); |
|||
BarTender.Database database = btformat.Databases.GetDatabase(1); |
|||
if (!string.IsNullOrEmpty(printer)) |
|||
{ |
|||
btformat.Printer = printer; |
|||
} |
|||
database.TextFile.FileName = filepath; |
|||
|
|||
//database.SQLStatement += $" where printID='{printId}' order by RIGHT(Var1,7)";//填写SQL语句附加条件
|
|||
//database.Password = VSLoginInfo.SQLPassword;
|
|||
//database.User = VSLoginInfo.SQLUser;
|
|||
btformat.PrintOut(false, false); //第二个参数设置是否跳出打印属性;
|
|||
btapp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);//退出时同步退出bartender进程(SQL条件会改变,不更新模板信息)
|
|||
|
|||
} |
|||
catch |
|||
{ |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
/// <summary>
|
|||
/// 写文本
|
|||
/// </summary>
|
|||
/// <param name="logStr"></param>
|
|||
public static string WriteLog(List<string> lines, string fileDirectory = "", string guid = "") |
|||
{ |
|||
|
|||
try |
|||
{ |
|||
string path1 = Environment.CurrentDirectory + Path.Combine("/printFile/") + fileDirectory; |
|||
if (!Directory.Exists(path1)) |
|||
{ |
|||
//创建索引目录
|
|||
Directory.CreateDirectory(path1); |
|||
} |
|||
string path = path1 + "/" + DateTime.Now.ToString("yyyy-MM-dd") + guid + ".txt"; |
|||
FileStream stream = null; |
|||
if (File.Exists(path)) |
|||
{ |
|||
File.Delete(path); |
|||
} |
|||
stream = new FileStream(path, FileMode.Create); |
|||
|
|||
StreamWriter writer = new StreamWriter(stream); |
|||
for (var i = 0; i < lines.Count; i++) |
|||
{ |
|||
writer.Write(lines[i] + Environment.NewLine); |
|||
|
|||
} |
|||
writer.Flush(); |
|||
writer.Close(); |
|||
stream.Close(); |
|||
return path; |
|||
} |
|||
catch |
|||
{ |
|||
} |
|||
finally |
|||
{ |
|||
|
|||
} |
|||
return ""; |
|||
} |
|||
#endregion
|
|||
public static void ExportToFile(string btFileName, Dictionary<string, string> data) |
|||
{ |
|||
//BarTender.Application btApp = new BarTender.Application();
|
|||
//BarTender.Format btFormat;
|
|||
|
|||
btFormat = btApp.Formats.Open(btFileName); |
|||
|
|||
foreach (var key in data.Keys) |
|||
{ |
|||
|
|||
btFormat.SetNamedSubStringValue(key, data[key]); |
|||
} |
|||
|
|||
|
|||
//btFormat.SaveAs(btFileName, true);
|
|||
|
|||
btFormat.ExportToFile(@"E:/temp.jpg", "jpg", BarTender.BtColors.btColors24Bit, BarTender.BtResolution.btResolutionPrinter, BarTender.BtSaveOptions.btSaveChanges); |
|||
btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); |
|||
btApp.Quit(BarTender.BtSaveOptions.btSaveChanges); |
|||
|
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
} |
|||
//using System;
|
|||
//using System.Collections.Generic;
|
|||
//using System.IO;
|
|||
//using System.Text;
|
|||
//using System.Linq;
|
|||
//using System.Threading.Tasks;
|
|||
|
|||
//namespace BBWY.Client.Helpers
|
|||
//{
|
|||
// public class BartenderHelper
|
|||
// {
|
|||
// #region BarTender打印方法
|
|||
// private static BarTender.Application btApp = new BarTender.Application();
|
|||
// private static BarTender.Format btFormat = new BarTender.Format();
|
|||
// public string TemplateFile { set; get; }
|
|||
// public string ErrorMsg { set; get; }
|
|||
// public bool Status { set; get; }
|
|||
// /// <summary>
|
|||
// /// 使用 BarTender进行打印,批量打印
|
|||
// /// </summary>
|
|||
// /// <param name="labelName"> BarTender模板标签</param>
|
|||
// /// <param name="labelParamsValuesList">打印数据</param>
|
|||
// /// <param name="msg">回调消息</param>
|
|||
// /// <param name="printQty">打印数量</param>
|
|||
// /// <returns>打印OK true</returns>
|
|||
// public bool BTPrint(string labelName, Dictionary<string, string> labelParamsValues, out string msg, int printQty = 1, string printer = "Deli DL-730C(NEW)")
|
|||
// {
|
|||
// try
|
|||
// {
|
|||
// bool checkLabelVar = true;
|
|||
// string tmpMsg = "";
|
|||
// this.TemplateFile = labelName;
|
|||
|
|||
// if (!File.Exists(TemplateFile))
|
|||
// {
|
|||
// msg = "Error:Label File Not Exists" + TemplateFile;
|
|||
// this.ErrorMsg = msg;
|
|||
// return false;
|
|||
// }
|
|||
// if (!string.IsNullOrEmpty(printer))
|
|||
// {
|
|||
// bool printerFlag = false;
|
|||
// foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取所有打印机名称
|
|||
// {
|
|||
// if (sPrint.Equals(printer))
|
|||
// {
|
|||
// printerFlag = true;
|
|||
// }
|
|||
// }
|
|||
|
|||
// if (!printerFlag)
|
|||
// {
|
|||
// msg = "Error:Printer Name Error";
|
|||
// return false;
|
|||
// }
|
|||
|
|||
|
|||
// }
|
|||
// //找到打印模板的标签页
|
|||
// btFormat = btApp.Formats.Open(labelName, false, "");
|
|||
// if (!string.IsNullOrEmpty(printer))
|
|||
// {
|
|||
// btFormat.Printer = printer;
|
|||
// }
|
|||
// if (btApp != null)
|
|||
// {
|
|||
// //btFormat.IdenticalCopiesOfLabel = printQty;
|
|||
// //btFormat.PrintOut(true, false);
|
|||
|
|||
// //取模板所有的值
|
|||
// string strBT = btFormat.NamedSubStrings.GetAll("#", "$");
|
|||
|
|||
// //将模板所有值进行拆分
|
|||
// strBT = strBT.Substring(0, strBT.Length - 1);
|
|||
// string[] strBTValue = strBT.Split(new char[] { '$' });
|
|||
|
|||
// //循环将模板的值写入lbldicVar中
|
|||
// Dictionary<string, string> lbldicVar = new Dictionary<string, string>();
|
|||
// for (int i = 0; i < strBTValue.Length; i++)
|
|||
// {
|
|||
// string[] cc = strBTValue[i].Split(new char[] { '#' });
|
|||
|
|||
// lbldicVar.Add(cc[0].ToString(), cc[1].ToString()); //0是模板的参数 1是参数值
|
|||
// }
|
|||
// if (labelParamsValues.Count > 0)
|
|||
// {
|
|||
// foreach (var item in lbldicVar)
|
|||
// {
|
|||
// if (labelParamsValues.Count(q => q.Key.ToUpper() == item.Key.ToUpper()) == 0)
|
|||
// {
|
|||
// tmpMsg = "数据源缺少参数: " + item.Key;
|
|||
// if (checkLabelVar)
|
|||
// {
|
|||
// this.ErrorMsg = tmpMsg;
|
|||
// checkLabelVar = false;
|
|||
// }
|
|||
// else
|
|||
// {
|
|||
// this.ErrorMsg = this.ErrorMsg + (char)10 + tmpMsg;
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
// if (!checkLabelVar)
|
|||
// {
|
|||
// msg = this.ErrorMsg;
|
|||
// this.Status = false;
|
|||
// return false;
|
|||
// }
|
|||
// foreach (var param in labelParamsValues.Keys)
|
|||
// {
|
|||
// if (lbldicVar.Keys.Count(q => q.ToUpper() == param.ToUpper()) > 0)
|
|||
// {
|
|||
// var labvar = lbldicVar.Keys.First(q => q.ToUpper() == param.ToUpper());
|
|||
|
|||
// if (string.IsNullOrEmpty(labelParamsValues[param]))
|
|||
// {
|
|||
// tmpMsg = "数据源参数值为空:" + labvar;
|
|||
// if (checkLabelVar)
|
|||
// {
|
|||
// this.ErrorMsg = tmpMsg;
|
|||
// checkLabelVar = false;
|
|||
// }
|
|||
// else
|
|||
// {
|
|||
// this.ErrorMsg = this.ErrorMsg + (char)10 + tmpMsg;
|
|||
// }
|
|||
// }
|
|||
// btFormat.SetNamedSubStringValue(param, labelParamsValues[param]);
|
|||
// }
|
|||
// }
|
|||
// if (!checkLabelVar)
|
|||
// {
|
|||
// msg = this.ErrorMsg;
|
|||
// this.Status = false;
|
|||
// return false;
|
|||
// }
|
|||
// btFormat.IdenticalCopiesOfLabel = printQty;
|
|||
|
|||
// //第二个false设置打印时是否跳出打印属性
|
|||
// btFormat.PrintOut(true, false);
|
|||
// //退出是是否保存标签
|
|||
// btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
|
|||
// //lblDoc.FormFeed();
|
|||
// msg = "OK";
|
|||
// this.ErrorMsg = msg;
|
|||
// this.Status = true;
|
|||
// return true;
|
|||
// }
|
|||
// else
|
|||
// {
|
|||
// msg = "Print Data is null";
|
|||
// this.ErrorMsg = msg;
|
|||
// this.Status = false;
|
|||
// return false;
|
|||
// }
|
|||
// }
|
|||
// else
|
|||
// {
|
|||
// msg = "Error:CodeSoft Application can't boot up";
|
|||
// this.ErrorMsg = msg;
|
|||
// this.Status = false;
|
|||
// return false;
|
|||
// }
|
|||
// }
|
|||
// catch (Exception ex)
|
|||
// {
|
|||
// msg = ex.Message + ex.InnerException;
|
|||
// this.ErrorMsg = msg;
|
|||
// this.Status = false;
|
|||
// File.AppendAllText("c:/1.txt", msg);
|
|||
// return false;
|
|||
// }
|
|||
// finally
|
|||
// {
|
|||
// if (btApp != null)
|
|||
// {
|
|||
|
|||
// try
|
|||
// {
|
|||
// // File.AppendAllText("c:/1.txt","退出界面");
|
|||
// btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出时同步退出bartender进程
|
|||
// // File.AppendAllText("c:/1.txt", "退出成功");
|
|||
// }
|
|||
// catch (Exception ex)
|
|||
// {
|
|||
|
|||
// //File.AppendAllText("c:/1.txt",ex.Message+"|||||"+ ex.ToString());
|
|||
// }
|
|||
|
|||
|
|||
// //lblApp.Documents.CloseAll(true);
|
|||
// //lblDoc = null;
|
|||
// }
|
|||
// GC.Collect();
|
|||
// }
|
|||
// }
|
|||
// #endregion
|
|||
|
|||
// #region 数据库调用打印
|
|||
// /// <summary>
|
|||
// /// 打印从文本数据库中打
|
|||
// /// </summary>
|
|||
// /// <returns></returns>
|
|||
// public bool BTPrintByDataBase(string path, string filePath, string printer = "")
|
|||
// {
|
|||
|
|||
// try
|
|||
// {
|
|||
// BarTender.Application btapp;
|
|||
// BarTender.Format btformat;
|
|||
// btapp = new BarTender.Application();
|
|||
// btformat = btapp.Formats.Open(path, false, "");
|
|||
// BarTender.Database database = btformat.Databases.GetDatabase(1);
|
|||
// if (!string.IsNullOrEmpty(printer))
|
|||
// {
|
|||
// btformat.Printer = printer;
|
|||
// }
|
|||
|
|||
// database.TextFile.FileName = filePath;
|
|||
|
|||
// //database.SQLStatement += $" where printID='{printId}' order by RIGHT(Var1,7)";//填写SQL语句附加条件
|
|||
// //database.Password = VSLoginInfo.SQLPassword;
|
|||
// //database.User = VSLoginInfo.SQLUser;
|
|||
// btformat.PrintOut(false, false); //第二个参数设置是否跳出打印属性;
|
|||
// btapp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);//退出时同步退出bartender进程(SQL条件会改变,不更新模板信息)
|
|||
|
|||
// }
|
|||
// catch
|
|||
// {
|
|||
// return false;
|
|||
// }
|
|||
// return true;
|
|||
// }
|
|||
|
|||
|
|||
// /// <summary>
|
|||
// /// 打印从文本数据库中打
|
|||
// /// </summary>
|
|||
// /// <param name="templatepath">模板地址</param>
|
|||
// /// <param name="dataLine">数据行,一行一个标签,第一行为标题,多列逗号分割</param>
|
|||
// /// <returns></returns>
|
|||
// public bool BTPrintByDataBase(string templatepath, List<string> dataLine, string printer = "")
|
|||
// {
|
|||
|
|||
// try
|
|||
// {
|
|||
// BarTender.Application btapp;
|
|||
// BarTender.Format btformat;
|
|||
// btapp = new BarTender.Application();
|
|||
// var filepath = WriteLog(dataLine, "", Guid.NewGuid().ToString());
|
|||
// btformat = btapp.Formats.Open(templatepath, false, "");
|
|||
// BarTender.Database database = btformat.Databases.GetDatabase(1);
|
|||
// if (!string.IsNullOrEmpty(printer))
|
|||
// {
|
|||
// btformat.Printer = printer;
|
|||
// }
|
|||
// database.TextFile.FileName = filepath;
|
|||
|
|||
// //database.SQLStatement += $" where printID='{printId}' order by RIGHT(Var1,7)";//填写SQL语句附加条件
|
|||
// //database.Password = VSLoginInfo.SQLPassword;
|
|||
// //database.User = VSLoginInfo.SQLUser;
|
|||
// btformat.PrintOut(false, false); //第二个参数设置是否跳出打印属性;
|
|||
// btapp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);//退出时同步退出bartender进程(SQL条件会改变,不更新模板信息)
|
|||
|
|||
// }
|
|||
// catch
|
|||
// {
|
|||
// return false;
|
|||
// }
|
|||
// return true;
|
|||
// }
|
|||
// /// <summary>
|
|||
// /// 写文本
|
|||
// /// </summary>
|
|||
// /// <param name="logStr"></param>
|
|||
// public static string WriteLog(List<string> lines, string fileDirectory = "", string guid = "")
|
|||
// {
|
|||
|
|||
// try
|
|||
// {
|
|||
// string path1 = Environment.CurrentDirectory + Path.Combine("/printFile/") + fileDirectory;
|
|||
// if (!Directory.Exists(path1))
|
|||
// {
|
|||
// //创建索引目录
|
|||
// Directory.CreateDirectory(path1);
|
|||
// }
|
|||
// string path = path1 + "/" + DateTime.Now.ToString("yyyy-MM-dd") + guid + ".txt";
|
|||
// FileStream stream = null;
|
|||
// if (File.Exists(path))
|
|||
// {
|
|||
// File.Delete(path);
|
|||
// }
|
|||
// stream = new FileStream(path, FileMode.Create);
|
|||
|
|||
// StreamWriter writer = new StreamWriter(stream);
|
|||
// for (var i = 0; i < lines.Count; i++)
|
|||
// {
|
|||
// writer.Write(lines[i] + Environment.NewLine);
|
|||
|
|||
// }
|
|||
// writer.Flush();
|
|||
// writer.Close();
|
|||
// stream.Close();
|
|||
// return path;
|
|||
// }
|
|||
// catch
|
|||
// {
|
|||
// }
|
|||
// finally
|
|||
// {
|
|||
|
|||
// }
|
|||
// return "";
|
|||
// }
|
|||
// #endregion
|
|||
// public static void ExportToFile(string btFileName, Dictionary<string, string> data)
|
|||
// {
|
|||
// //BarTender.Application btApp = new BarTender.Application();
|
|||
// //BarTender.Format btFormat;
|
|||
|
|||
// btFormat = btApp.Formats.Open(btFileName);
|
|||
|
|||
// foreach (var key in data.Keys)
|
|||
// {
|
|||
|
|||
// btFormat.SetNamedSubStringValue(key, data[key]);
|
|||
// }
|
|||
|
|||
|
|||
// //btFormat.SaveAs(btFileName, true);
|
|||
|
|||
// btFormat.ExportToFile(@"E:/temp.jpg", "jpg", BarTender.BtColors.btColors24Bit, BarTender.BtResolution.btResolutionPrinter, BarTender.BtSaveOptions.btSaveChanges);
|
|||
// btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
|
|||
// btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);
|
|||
|
|||
// }
|
|||
|
|||
|
|||
// }
|
|||
|
|||
//}
|
|||
|
@ -0,0 +1,183 @@ |
|||
using BarcodeLib; |
|||
using BBWY.Client.Models; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Drawing; |
|||
using System.Drawing.Printing; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace BBWY.Client.Helpers |
|||
{ |
|||
public class MyPrintHelper |
|||
{ |
|||
|
|||
public static void PrintBarcode(ref PrintPageEventArgs args, BarCodeModel barCode, Font font) |
|||
{ |
|||
args.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
|||
args.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; |
|||
var sige = args.Graphics.VisibleClipBounds;// 宽:236 高:157 打印可视化区域
|
|||
args.Graphics.TranslateTransform((sige.Width - 236) / 2, 0);//设置起始位置
|
|||
//args.Graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Black), 0, 0, sige.Width, sige.Height);//画出条码编辑区域
|
|||
args.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; |
|||
args.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
|||
if (barCode.LabelModel == BarcodeLabelModel.barsimplify) |
|||
{ |
|||
font = new Font("宋体", 10, System.Drawing.FontStyle.Regular); |
|||
SetBarSimplify(barCode, ref args, font); |
|||
} |
|||
else |
|||
{ |
|||
font = new Font("宋体", 8, System.Drawing.FontStyle.Regular); |
|||
SetBarStander(barCode, ref args, font); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 标准
|
|||
/// </summary>
|
|||
/// <param name="barCode"></param>
|
|||
/// <param name="args"></param>
|
|||
/// <param name="font"></param>
|
|||
private static void SetBarStander(BarCodeModel barCode, ref PrintPageEventArgs args, Font font) |
|||
{ |
|||
|
|||
|
|||
string barcodeSku = $"POP{barCode.SkuId}"; |
|||
Barcode barcode = new Barcode(); |
|||
System.Drawing.Image img = barcode.Encode(TYPE.CODE128, barcodeSku, 500, 40); |
|||
SizeF size = args.Graphics.MeasureString(barCode.BrandName, font); |
|||
var sizeHeight = size.Height;//字体高度
|
|||
var heightSpace = 5;//间隔高度
|
|||
|
|||
args.Graphics.DrawString($"品牌: {barCode.Brand}", font, System.Drawing.Brushes.Black, new PointF(10, 10)); |
|||
args.Graphics.DrawString($"品名: {barCode.BrandName}", font, System.Drawing.Brushes.Black, new PointF(10, 10 + heightSpace + sizeHeight)); |
|||
args.Graphics.DrawString($"型号: {barCode.ProductNo}", font, System.Drawing.Brushes.Black, new PointF(10, 10 + 2 * (heightSpace + sizeHeight))); |
|||
args.Graphics.DrawString($"规格: {barCode.SkuName}", font, System.Drawing.Brushes.Black, new PointF(10, 10 + 3 * (heightSpace + sizeHeight))); |
|||
// g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
|
|||
size = args.Graphics.MeasureString(barcodeSku, font); |
|||
args.Graphics.DrawString(barcodeSku, font, System.Drawing.Brushes.Black, new PointF((236 - size.Width) / 2, 10 + 4 * (heightSpace + sizeHeight) + 50 + 5)); |
|||
args.Graphics.DrawImage(img, 1, 10 + 4 * (heightSpace + sizeHeight), 234, 50); |
|||
} |
|||
/// <summary>
|
|||
/// 精简
|
|||
/// </summary>
|
|||
/// <param name="barCode"></param>
|
|||
/// <param name="args"></param>
|
|||
/// <param name="font"></param>
|
|||
private static void SetBarSimplify(BarCodeModel barCode, ref PrintPageEventArgs args, Font font) |
|||
{ |
|||
string barcodeSku = $"POP{barCode.SkuId}"; |
|||
Barcode barcode = new Barcode(); |
|||
System.Drawing.Image img = barcode.Encode(TYPE.CODE128, barcodeSku, 500, 60); |
|||
args.Graphics.DrawString($"品名: {barCode.BrandName}", font, System.Drawing.Brushes.Black, new PointF(10, 10)); |
|||
args.Graphics.DrawString($"规格: {barCode.SkuName}", font, System.Drawing.Brushes.Black, new PointF(10, 40)); |
|||
// g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
|
|||
SizeF size = args.Graphics.MeasureString(barcodeSku, font); |
|||
args.Graphics.DrawString(barcodeSku, font, System.Drawing.Brushes.Black, new PointF((236 - size.Width) / 2, 130)); |
|||
args.Graphics.DrawImage(img, 1, 68, 234, 60); |
|||
} |
|||
|
|||
public static void PrintCertificate(ref PrintPageEventArgs args, CertificateModel certificate, Font font) |
|||
{ |
|||
args.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
|||
args.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; |
|||
var sige = args.Graphics.VisibleClipBounds;// 宽:236 高:157 打印可视化区域
|
|||
args.Graphics.TranslateTransform((sige.Width - 236) / 2, 0);//设置起始位置
|
|||
args.Graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Black), 5, 5, sige.Width - 10, sige.Height - 10);//画出条码编辑区域
|
|||
args.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; |
|||
args.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
|||
|
|||
font = new Font("宋体", 6, System.Drawing.FontStyle.Regular); |
|||
SetCerStander(ref args, certificate, font); |
|||
} |
|||
|
|||
private static void SetCerStander(ref PrintPageEventArgs args, CertificateModel certificate, Font font) |
|||
{ |
|||
|
|||
int bigJiange = 20;//合格证与品牌的高度差
|
|||
var heightSpace = 4;//所有行间隔高度
|
|||
int standerSpace = 1;//执行标准之间的
|
|||
if (certificate.IsLogo == 1)//含图标
|
|||
{ |
|||
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); |
|||
|
|||
string path = appPath + $"/Resources/Images/3c.png"; |
|||
Image image = Image.FromFile(path); |
|||
args.Graphics.DrawImage(image, 190, 7, 28, 21); |
|||
args.Graphics.DrawString(certificate.FactoryNumber, font, System.Drawing.Brushes.Black, 188, 28); |
|||
|
|||
} |
|||
|
|||
var fontBig = new Font("宋体", 14, System.Drawing.FontStyle.Regular); |
|||
string cerName = "合格证"; |
|||
SizeF bigSize = args.Graphics.MeasureString(cerName, fontBig); |
|||
|
|||
float verHeight = bigJiange + bigSize.Height; |
|||
args.Graphics.DrawString(cerName, fontBig, System.Drawing.Brushes.Black, new PointF((236 - bigSize.Width) / 2, 10)); |
|||
|
|||
args.Graphics.DrawString($"品牌: {certificate.Brand}", font, System.Drawing.Brushes.Black, new PointF(10, verHeight)); |
|||
args.Graphics.DrawString($"型号: {certificate.ProductNo}", font, System.Drawing.Brushes.Black, new PointF(122, verHeight)); |
|||
var smallSize = args.Graphics.MeasureString(certificate.Brand, font);//小字体高度
|
|||
|
|||
verHeight += (smallSize.Height + heightSpace); |
|||
args.Graphics.DrawString($"品名: {certificate.BrandName}", font, System.Drawing.Brushes.Black, new PointF(10, verHeight)); |
|||
|
|||
args.Graphics.DrawString($"材质: {certificate.Shader}", font, System.Drawing.Brushes.Black, new PointF(122, verHeight)); |
|||
|
|||
StringBuilder sb = new StringBuilder(); |
|||
var excutes = certificate.ExcuteStander.Split(',', StringSplitOptions.RemoveEmptyEntries); |
|||
|
|||
|
|||
int hangCount = excutes.Count() / 2 + excutes.Count() % 2;//获取行数
|
|||
|
|||
for (int i = 0; i < excutes.Count(); i++) |
|||
{ |
|||
if (i % 2 == 0 && i > 0)//间隔两个换行
|
|||
{ |
|||
sb.Append("\n"); |
|||
} |
|||
sb.Append(excutes[i]).Append(" "); |
|||
} |
|||
sb.Remove(sb.Length - 3, 3); |
|||
var rows = sb.ToString().Split('\n'); |
|||
string excuteStander = "执行标准: "; |
|||
smallSize = args.Graphics.MeasureString(excuteStander, font); |
|||
verHeight += (smallSize.Height + heightSpace); |
|||
args.Graphics.DrawString($"执行标准: {rows[0]}", font, System.Drawing.Brushes.Black, new PointF(10, verHeight)); |
|||
|
|||
for (int i = 1; i < rows.Count(); i++) |
|||
{ |
|||
verHeight += (smallSize.Height + standerSpace); |
|||
args.Graphics.DrawString($"{rows[i]}", font, System.Drawing.Brushes.Black, new PointF(10 + smallSize.Width, verHeight)); |
|||
} |
|||
verHeight += (smallSize.Height + heightSpace); |
|||
|
|||
RectangleF rect = new RectangleF(10, verHeight, 220, smallSize.Height * 2 + heightSpace); |
|||
var productStr = $"生产商: {certificate.ProductShop}"; |
|||
if (args.Graphics.MeasureString(productStr, font).Width <= 220) |
|||
{ |
|||
args.Graphics.DrawString(productStr, font, System.Drawing.Brushes.Black,10,verHeight); |
|||
verHeight += smallSize.Height + heightSpace; |
|||
} |
|||
else |
|||
{ |
|||
args.Graphics.DrawString(productStr, font, System.Drawing.Brushes.Black, rect); |
|||
verHeight += 2 * smallSize.Height + heightSpace; |
|||
} |
|||
|
|||
//var format = new StringFormat
|
|||
//{
|
|||
// LineAlignment = StringAlignment.Far,
|
|||
// // Alignment = StringAlignment.Center
|
|||
//};
|
|||
|
|||
var rect1 = new RectangleF(10, verHeight, 220, smallSize.Height * 3 + heightSpace); |
|||
args.Graphics.DrawString($"地址: {certificate.ProductAdress}", font, System.Drawing.Brushes.Black, rect1 ); |
|||
//certificate.ExcuteStander =;
|
|||
|
|||
|
|||
//args.Graphics.DrawImage(img, 1, 68, 234, 60);
|
|||
} |
|||
} |
|||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue