星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LatticeFileHelper.cs 14KB

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