星火微课系统客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LatticeFileHelper.cs 13KB

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