星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LatticeFileHelper.cs 14KB

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