星火微课系统客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LatticeFileHelper.cs 15KB

1 ano atrás
4 anos atrás
1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing.Printing;
  5. using System.Threading;
  6. namespace Common.system
  7. {
  8. using XHWK.WKTool.system;
  9. /// <summary>
  10. /// 点阵文件制作打印
  11. /// 创建时间:2020年9月1日
  12. /// </summary>
  13. public class LatticeFileHelper
  14. {
  15. /// <summary>
  16. /// 打印进程
  17. /// </summary>
  18. public static Process PrintProcess = null;
  19. /// <summary>
  20. /// 输出日志文件地址 每个文件2MB左右
  21. /// </summary>
  22. private static string LogPath = "";
  23. /// <summary>
  24. /// 运行配置 首次打开必须运行
  25. /// </summary>
  26. public static void RunPrintConfig()
  27. {
  28. string Path = FileToolsCommon.GetFileAbsolutePath("/PrintTool/PrintConfig.exe");
  29. Process myProcess = new Process();
  30. LogPath = CreateLog();
  31. myProcess.StartInfo.FileName = Path; //绝对路径
  32. //myProcess.StartInfo.Arguments = "";
  33. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  34. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  35. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  36. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  37. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  38. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  39. myProcess.Start(); //启动线程
  40. myProcess.BeginErrorReadLine(); //开始异步读取
  41. myProcess.WaitForExit(); //阻塞等待进程结束
  42. myProcess.Close(); //关闭进程
  43. myProcess.Dispose(); //释放资源
  44. }
  45. /// <summary>
  46. /// 生成点阵打印文件
  47. /// </summary>
  48. /// <param name="PDFPath">PDF文件位置</param>
  49. /// <param name="SavePath">保存文件位置 TPF</param>
  50. /// <param name="PrintResult">输出结果,非0为失败</param>
  51. /// <param name="StandardError">错误信息</param>
  52. /// <param name="StandardOutput">输出信息["1536.688.35.70","1536.688.35.71","1536.688.35.72"]</param>
  53. /// <param name="PointType">0 为方点,1 为圆点 默认方点</param>
  54. public static void GeneratingPDF(string PDFPath, string SavePath, out int PrintResult, out string StandardError, out string StandardOutput, int PointType = 0)
  55. {
  56. //XML点阵文件位置
  57. string XmlFile = FileToolsCommon.GetFileAbsolutePath("/LatticeXML/print_license.xml");
  58. string ContentParameter = null;
  59. ContentParameter = "-sMode=Generate \"-sPDF=" + PDFPath + "\" \"-sLIC=" + XmlFile + "\" \"-oTPF=" + SavePath + "\" \"-dType=" + PointType + "\" ";//[-dType=0] [-dPrint=0] [-pStart=0]
  60. #region 制作点阵文件参数说明
  61. //-sMode = Generate - sPDF = d:\l\e.pdf - sLIC = d:\l\a.xml - oPDF = d:\l\11210.pdf"
  62. //当 - sMode = Generate,参数如下:
  63. //PrintTool.exe - sMode = Generate - sPDF = -sLIC = < -oPDF =| -oTPF=> [dType=] [-dPrint] [-pStart =]
  64. //- sPDF 原始 PDF 文件路径
  65. //- sLIC 点阵资源文件路径
  66. //- oPDF 生成的点阵 PDF 文件路径,-oPDF 与 - oTPF 至少设置一个。PDF 与 TPF 说明参见 2 生成文件类型说明
  67. //- oTPF 生成的 TPF 文件,-oPDF 与 - oTPF 至少设置一个。PDF 与 TPF 说明参见 2 生成文件类型说明
  68. //- dType 0 为方点,1 为圆点;可选参数,默认 0。详细参数区别,参见 3.1 点阵形状
  69. //- dPrint 0 为激光打印机,1 为印刷机;可选参数,默认 0。详细参数区别,参见 3.2 点阵 PDF 适用打印场景
  70. //- pStart 整数,选择从哪页点阵资源开始制作点阵文件, 即起始点阵资源页序号;可选参数,默认0。关于 - pStart 的功能说明,参见 1.3.1.高级设置
  71. #endregion
  72. while (PrintProcess != null)
  73. {
  74. Thread.Sleep(100);
  75. }
  76. Process[] KillProcessArray = Process.GetProcessesByName("PrintTool");
  77. foreach (Process KillProcess in KillProcessArray)
  78. {
  79. KillProcess.Kill();
  80. }
  81. PrintProcess = new Process();
  82. LogPath = CreateLog();
  83. PrintProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath("/PrintTool/PrintTool.exe"); //绝对路径
  84. PrintProcess.StartInfo.Arguments = ContentParameter;
  85. PrintProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  86. PrintProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  87. PrintProcess.StartInfo.RedirectStandardOutput = true;
  88. PrintProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  89. PrintProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  90. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  91. PrintProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  92. PrintProcess.Start(); //启动线程
  93. PrintProcess.WaitForExit(); //阻塞等待进程结束
  94. StandardError = PrintProcess.StandardError.ReadToEnd();
  95. StandardOutput = PrintProcess.StandardOutput.ReadToEnd();
  96. //PrintProcess.BeginErrorReadLine(); //开始异步读取
  97. Output("【制作点阵文件】错误:" + StandardError);
  98. Output("【制作点阵文件】返回信息:" + StandardOutput);
  99. PrintResult = PrintProcess.ExitCode; //打印结果 非0则为打印失败
  100. Output("【制作点阵文件】" + ContentParameter);//记录打印日志
  101. PrintProcess.Close(); //关闭进程
  102. PrintProcess.Dispose(); //释放资源
  103. #region 进程是否已经释放
  104. bool IsRunning = true;
  105. while (IsRunning)
  106. {
  107. IsRunning = false;
  108. Process[] ProcessArray = Process.GetProcessesByName("PrintTool");
  109. foreach (Process KillProcess in ProcessArray)
  110. {
  111. IsRunning = true;
  112. Thread.Sleep(100);
  113. }
  114. }
  115. #endregion
  116. PrintProcess = null;
  117. }
  118. /// <summary>
  119. /// 获取打印机列表
  120. /// </summary>
  121. /// <param name="DefaultPrinter">默认打印机</param>
  122. /// <returns>打印机列表</returns>
  123. public static List<string> GetPrinterList(out string DefaultPrinter)
  124. {
  125. List<string> PrintersList = new List<string>();
  126. DefaultPrinter = null;
  127. try
  128. {
  129. PrintDocument print = new PrintDocument();
  130. DefaultPrinter = print.PrinterSettings.PrinterName;//默认打印机名
  131. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  132. {
  133. PrintersList.Add(sPrint);
  134. }
  135. }
  136. catch (Exception)
  137. {
  138. }
  139. return PrintersList;
  140. }
  141. /// <summary>
  142. /// 打印TPF点阵文件
  143. /// </summary>
  144. /// <param name="TPFPath">TPF路径</param>
  145. /// <param name="PrinterNum">打印份数</param>
  146. /// <param name="PrinterName">打印机名称</param>
  147. /// <param name="PrintResult">输出结果,非0为失败</param>
  148. /// <param name="StandardError">错误信息</param>
  149. /// <param name="StandardOutput">输出信息</param>
  150. public static void PrinterTPFFile(string TPFPath, int PrinterNum, string PrinterName, out int PrintResult, out string StandardError, out string StandardOutput)
  151. {
  152. TPFPath = TPFPath.Replace("/", "\\");
  153. //XML点阵文件位置
  154. //string XmlFile = FileToolsCommon.GetFileAbsolutePath("/LatticeXML/print_license.xml");
  155. string ContentParameter = null;
  156. ContentParameter = " -sMode=Print \"-sTPF=" + TPFPath + "\" \"-sPrinter=" + PrinterName + "\" \"-dCopy=" + PrinterNum + "\"";//\"-dPaperSize=9\"
  157. #region 打印参数说明
  158. //-sMode = Print,参数如下:
  159. //PrintTool.exe - sMode = Print - sTPF = [-dCopy =][-dPaperSize =] - sPrinter =
  160. //-sTPF 需要打印的 TPF 文件路径
  161. //- dCopy 整数,可选参数,打印份数,默认 1
  162. //- dPaperSize 整数,可选参数,设置打印纸张尺寸。不设置此值,系统自动选择匹配纸张打印;A4:"-dPaperSize=9" DMPAPER_A4 9 A4 210 x 297 mm 如
  163. // 需要手动设置纸张尺寸,不同尺寸纸张对应值,参见 https://docs.microsoft.com/en-us/windows/win32/intl/paper-sizes
  164. //- sPrinter 打印机名称
  165. // 打印完成后,如果打印成功,PrintTool.exe 退出值为 0。
  166. //如果打印失败,程序退出值为非 0。
  167. #endregion
  168. while (PrintProcess != null)
  169. {
  170. Thread.Sleep(100);
  171. }
  172. Process[] KillProcessArray = Process.GetProcessesByName("PrintTool");
  173. foreach (Process KillProcess in KillProcessArray)
  174. {
  175. KillProcess.Kill();
  176. }
  177. PrintProcess = new Process();
  178. LogPath = CreateLog();
  179. PrintProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath("/PrintTool/PrintTool.exe").Replace("/", "\\"); ; //绝对路径
  180. PrintProcess.StartInfo.Arguments = ContentParameter;
  181. PrintProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  182. PrintProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  183. PrintProcess.StartInfo.RedirectStandardOutput = true;
  184. PrintProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  185. PrintProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  186. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  187. PrintProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  188. PrintProcess.Start(); //启动线程
  189. //PrintProcess.BeginErrorReadLine(); //开始异步读取
  190. PrintProcess.WaitForExit(); //阻塞等待进程结束
  191. StandardError = PrintProcess.StandardError.ReadToEnd();
  192. StandardOutput = PrintProcess.StandardOutput.ReadToEnd();
  193. Output("【制作点阵文件】错误:" + StandardError);
  194. Output("【制作点阵文件】返回信息:" + StandardOutput);
  195. PrintResult = PrintProcess.ExitCode; //打印结果 非0则为打印失败
  196. Output("【打印TPF文件】" + ContentParameter);//记录打印日志
  197. PrintProcess.Close(); //关闭进程
  198. PrintProcess.Dispose(); //释放资源
  199. #region 进程是否已经释放
  200. bool IsRunning = true;
  201. while (IsRunning)
  202. {
  203. IsRunning = false;
  204. Process[] ProcessArray = Process.GetProcessesByName("PrintTool");
  205. foreach (Process KillProcess in ProcessArray)
  206. {
  207. IsRunning = true;
  208. Thread.Sleep(100);
  209. }
  210. }
  211. #endregion
  212. PrintProcess = null;
  213. }
  214. /// <summary>
  215. /// 创建日志文件
  216. /// </summary>
  217. /// <returns></returns>
  218. private static string CreateLog()
  219. {
  220. string LogFileName = DateTime.Now.ToString("yyyyMMdd");
  221. string LogFilePath = FileToolsCommon.GetFileAbsolutePath("/Log/PrintLog/");
  222. FileToolsCommon.CreateDirectory(LogFilePath);
  223. string LogName = LogFilePath + LogFileName + ".log";
  224. try
  225. {
  226. if (!FileToolsCommon.IsExistFile(LogName))
  227. {
  228. FileToolsCommon.CreateFile(LogName);
  229. //FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  230. // "****************************************************************************************************\r\n");
  231. return LogName;
  232. }
  233. else
  234. {
  235. int num = 0;
  236. while (FileToolsCommon.GetFileSizeByMb(LogName) > 2)
  237. {
  238. num++;
  239. LogName = LogFilePath + LogFileName + "_" + num + ".log";
  240. FileToolsCommon.CreateFile(LogName);
  241. }
  242. //FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  243. // "****************************************************************************************************\r\n");
  244. return LogName;
  245. }
  246. }
  247. catch (Exception)
  248. {
  249. return LogName;
  250. }
  251. }
  252. /// <summary>
  253. /// 输出日志
  254. /// </summary>
  255. /// <param name="Message"></param>
  256. private static void Output(string Message)
  257. {
  258. if (!string.IsNullOrEmpty(Message))
  259. {
  260. FileToolsCommon.AppendText(LogPath, Message + "\r\n");
  261. FileToolsCommon.AppendText(LogPath, "\r\n****************************************************************************************************" +
  262. "****************************************************************************************************\r\n");
  263. }
  264. }
  265. /// <summary>
  266. /// 输出结果
  267. /// </summary>
  268. /// <param name="sendProcess"></param>
  269. /// <param name="output"></param>
  270. private static void Output(object sendProcess, DataReceivedEventArgs output)
  271. {
  272. Output(output.Data);
  273. }
  274. }
  275. }