/// <summary>
/// 适用于CodeSoft6.必须要安装CS6
/// 适用于CodeSoft6.必须要安装CS6
/// <para>平均2秒打一个,垃.- 圾</para>
/// <para>优化后速度还是挺快了,要先new这个类,再调用CodeSoftPrint方法</para>
/// <para>需要引用在在CodeSoft6安装文件下的"Lppx2.tlb"文件.命名空间为"LabelManager2" 最后变成了这个DLL Interop.LabelManager2.dll</para>
/// <para>如果报错 更改该DLL的[嵌入互操作类型]为false</para>
/// </summary>
public class CodeSoftPrinterHelper
{
private string _LabPathName;//标签名称
private LabelManager2.ApplicationClass _lbl;
private LabelManager2.Document _doc;
/// <summary>
/// initClass
/// </summary>
/// <param name="LabPathName"></param>
public CodeSoftPrinterHelper(string LabPathName)
{
_LabPathName = LabPathName;
List<string> varList = new List<string>();
if (!File.Exists(LabPathName))
{
throw new Exception("Lab文件不存在,请确认");
}
FileInfo fileInfo = new FileInfo(_LabPathName);
//Console.WriteLine("_lbl前时间为: " + DateTime.Now.ToString("yyyy - MM - dd HH.mm.ss.fff"));
_lbl = new LabelManager2.ApplicationClass();//这个步骤花了1600毫秒左右 一般400ms
//Console.WriteLine("Open前时间为: " + DateTime.Now.ToString("yyyy - MM - dd HH.mm.ss.fff"));
_lbl.Documents.Open(fileInfo.FullName, false);//调用设计好的Label文件 这个步骤也花了400毫秒左右
//Console.WriteLine("Open后 ActiveDocument前时间为: " + DateTime.Now.ToString("yyyy - MM - dd HH.mm.ss.fff"));
_doc = _lbl.ActiveDocument;//获取变量文本 这个步骤1ms左右
//Console.WriteLine("ActiveDocument后 时间为: " + DateTime.Now.ToString("yyyy - MM - dd HH.mm.ss.fff"));
}
/// <summary>
/// CodeSoft6 Lab文件打印
/// </summary>
/// <param name="VarDict"></param>
/// <returns></returns>
public bool CodeSoftPrint(Dictionary<string, string> VarDict = null)
{
bool result = false;
if (VarDict != null)
{
foreach (var varItem in VarDict)
{
_doc.Variables.FormVariables.Item(varItem.Key).Value = varItem.Value;//给变量传值
}
}//short shortResult = 1;
short shortResult = _doc.PrintDocument();//开始打印 打印成功为1
//打印完成后要重置变量值
if (VarDict != null)
{
foreach (var varItem in VarDict)
{
_doc.Variables.FormVariables.Item(varItem.Key).Value = "";//给变量传值
}
}
if (shortResult == 1)
{
result = true;
}
return result;
}
/// <summary>
/// 获取CS6的变量名
/// </summary>
/// <param name="LabPathName"></param>
/// <returns></returns>
public List<string> GetLabVarName()
{
List<string> varList = new List<string>();
for (int i = 1; i <= _doc.Variables.Count; i++)
{//Console.WriteLine("获取到的变量名为:" + doc.Variables.Item(i).Name.ToString());
varList.Add(_doc.Variables.Item(i).Name.ToString());
}
return varList;
}
/// <summary>
/// try释放lab资源
/// </summary>
public void CloseDisposable()
{
try
{
_doc.Close();//会关闭资源 这个步骤1ms左右
_lbl.Documents.CloseAll();//也会关闭资源 这个步骤14ms左右
_lbl.Quit();//退出 这个步骤1499ms左右
GC.Collect(0);
}
catch (Exception) { }
}
/// <summary>
/// CodeSoft6 Lab文件打印
/// </summary>
/// <param name="LabPathName">文件名</param>
/// <param name="VarDict">需要的变量数</param>
/// <returns></returns>
public static bool CodeSoftPrint(string LabPathName, Dictionary<string, string> VarDict = null)
{
bool result = false;
if (!File.Exists(LabPathName))
{
throw new Exception("Lab文件不存在,请确认");
}
FileInfo fileInfo = new FileInfo(LabPathName);
LabelManager2.ApplicationClass lbl = new LabelManager2.ApplicationClass();
lbl.Documents.Open(fileInfo.FullName, false);//调用设计好的Label文件
LabelManager2.Document doc = lbl.ActiveDocument;//获取变量文本
try
{
if (VarDict != null)
{
foreach (var varItem in VarDict)
{
Console.WriteLine(varItem.Key + "--------------" + varItem.Value);
doc.Variables.FormVariables.Item(varItem.Key).Value = varItem.Value;//给变量传值
}
}//doc.Printer.SwitchTo(UtilHelperPrintServer.GetDefaultPrinter());//定义打印机
short shortResult = doc.PrintDocument();//开始打印 打印成功为1
if (shortResult == 1)
{
result = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw ex;
}
finally
{
doc.Close();
lbl.Documents.CloseAll();
lbl.Quit();//退出
GC.Collect(0);
}
return result;
}
/// <summary>
/// 获取CS6的变量名
/// </summary>
/// <param name="LabPathName"></param>
/// <returns></returns>
public static List<string> GetLabVarName(string LabPathName)
{
List<string> varList = new List<string>();
if (!File.Exists(LabPathName))
{
throw new Exception("Lab文件不存在,请确认");
}
FileInfo fileInfo = new FileInfo(LabPathName);
LabelManager2.ApplicationClass lbl = new LabelManager2.ApplicationClass();
lbl.Documents.Open(fileInfo.FullName, false);//调用设计好的Label文件
LabelManager2.Document doc = lbl.ActiveDocument;//获取变量文本 为dos
for (int i = 1; i <= doc.Variables.Count; i++)
{//Console.WriteLine("获取到的变量名为:" + doc.Variables.Item(i).Name.ToString());
varList.Add(doc.Variables.Item(i).Name.ToString());
}
return varList;
}
}
CodeSoft 打印操作类;能获取到对应的lab变量,记得手动释放资源
最新推荐文章于 2023-03-27 10:55:58 发布