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