123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing.Printing;
- using System.Threading;
-
- namespace Common.system
- {
- using XHWK.WKTool.system;
-
- /// <summary>
- /// 点阵文件制作打印
- /// 创建时间:2020年9月1日
- /// </summary>
- public class LatticeFileHelper
- {
- /// <summary>
- /// 打印进程
- /// </summary>
- public static Process PrintProcess;
-
- /// <summary>
- /// 输出日志文件地址 每个文件2MB左右
- /// </summary>
- private static string _logPath = "";
-
- /// <summary>
- /// 运行配置 首次打开必须运行
- /// </summary>
- public static void RunPrintConfig()
- {
- string path = FileToolsCommon.GetFileAbsolutePath("/PrintTool/PrintConfig.exe");
- Process myProcess = new Process();
- _logPath = CreateLog();
- myProcess.StartInfo.FileName = path; //绝对路径
- //myProcess.StartInfo.Arguments = "";
- myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
- myProcess.ErrorDataReceived += Output;
- myProcess.Start(); //启动线程
- myProcess.BeginErrorReadLine(); //开始异步读取
- myProcess.WaitForExit(); //阻塞等待进程结束
- myProcess.Close(); //关闭进程
- myProcess.Dispose(); //释放资源
- }
-
- /// <summary>
- /// 生成点阵打印文件
- /// </summary>
- /// <param name="pdfPath">PDF文件位置</param>
- /// <param name="savePath">保存文件位置 TPF</param>
- /// <param name="printResult">输出结果,非0为失败</param>
- /// <param name="standardError">错误信息</param>
- /// <param name="standardOutput">输出信息["1536.688.35.70","1536.688.35.71","1536.688.35.72"]</param>
- /// <param name="pointType">0 为方点,1 为圆点 默认方点</param>
- public static void GeneratingPdf
- (
- string pdfPath,
- string savePath,
- out int printResult,
- out string standardError,
- out string standardOutput,
- int pointType = 0
- )
- {
- //XML点阵文件位置
- string xmlFile = FileToolsCommon.GetFileAbsolutePath("/LatticeXML/print_license.xml");
- string contentParameter;
- contentParameter = "-sMode=Generate \"-sPDF=" + pdfPath + "\" \"-sLIC=" + xmlFile + "\" \"-oTPF=" + savePath + "\" \"-dType=" + pointType + "\" "; //[-dType=0] [-dPrint=0] [-pStart=0]
-
- #region 制作点阵文件参数说明
-
- //-sMode = Generate - sPDF = d:\l\e.pdf - sLIC = d:\l\a.xml - oPDF = d:\l\11210.pdf"
- //当 - sMode = Generate,参数如下:
- //PrintTool.exe - sMode = Generate - sPDF = -sLIC = < -oPDF =| -oTPF=> [dType=] [-dPrint] [-pStart =]
- //- sPDF 原始 PDF 文件路径
- //- sLIC 点阵资源文件路径
- //- oPDF 生成的点阵 PDF 文件路径,-oPDF 与 - oTPF 至少设置一个。PDF 与 TPF 说明参见 2 生成文件类型说明
- //- oTPF 生成的 TPF 文件,-oPDF 与 - oTPF 至少设置一个。PDF 与 TPF 说明参见 2 生成文件类型说明
- //- dType 0 为方点,1 为圆点;可选参数,默认 0。详细参数区别,参见 3.1 点阵形状
- //- dPrint 0 为激光打印机,1 为印刷机;可选参数,默认 0。详细参数区别,参见 3.2 点阵 PDF 适用打印场景
- //- pStart 整数,选择从哪页点阵资源开始制作点阵文件, 即起始点阵资源页序号;可选参数,默认0。关于 - pStart 的功能说明,参见 1.3.1.高级设置
-
- #endregion
-
- while (PrintProcess != null)
- {
- Thread.Sleep(100);
- }
- Process[] killProcessArray = Process.GetProcessesByName("PrintTool");
- foreach (Process killProcess in killProcessArray)
- {
- killProcess.Kill();
- }
- PrintProcess = new Process();
- _logPath = CreateLog();
- PrintProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath("/PrintTool/PrintTool.exe"); //绝对路径
- PrintProcess.StartInfo.Arguments = contentParameter;
- PrintProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- PrintProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- PrintProcess.StartInfo.RedirectStandardOutput = true;
- PrintProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- PrintProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
- PrintProcess.ErrorDataReceived += Output;
- PrintProcess.Start(); //启动线程
- PrintProcess.WaitForExit(); //阻塞等待进程结束
- standardError = PrintProcess.StandardError.ReadToEnd();
- standardOutput = PrintProcess.StandardOutput.ReadToEnd();
- //PrintProcess.BeginErrorReadLine(); //开始异步读取
- Output("【制作点阵文件】错误:" + standardError);
- Output("【制作点阵文件】返回信息:" + standardOutput);
- printResult = PrintProcess.ExitCode; //打印结果 非0则为打印失败
- Output("【制作点阵文件】" + contentParameter); //记录打印日志
- PrintProcess.Close(); //关闭进程
- PrintProcess.Dispose(); //释放资源
-
- #region 进程是否已经释放
-
- bool isRunning = true;
- while (isRunning)
- {
- isRunning = false;
- Process[] processArray = Process.GetProcessesByName("PrintTool");
- if (processArray.Length > 0)
- {
- isRunning = true;
- Thread.Sleep(100);
- }
- }
-
- #endregion
-
- PrintProcess = null;
- }
-
- /// <summary>
- /// 获取打印机列表
- /// </summary>
- /// <param name="defaultPrinter">默认打印机</param>
- /// <returns>打印机列表</returns>
- public static List<string> GetPrinterList(out string defaultPrinter)
- {
- List<string> printersList = new List<string>();
- defaultPrinter = null;
- try
- {
- PrintDocument print = new PrintDocument();
- defaultPrinter = print.PrinterSettings.PrinterName; //默认打印机名
- foreach (string sPrint in PrinterSettings.InstalledPrinters) //获取所有打印机名称
- {
- printersList.Add(sPrint);
- }
- }
- catch (Exception)
- {
- // ignored
- }
- return printersList;
- }
-
- /// <summary>
- /// 打印TPF点阵文件
- /// </summary>
- /// <param name="tpfPath">TPF路径</param>
- /// <param name="printerNum">打印份数</param>
- /// <param name="printerName">打印机名称</param>
- /// <param name="printResult">输出结果,非0为失败</param>
- /// <param name="standardError">错误信息</param>
- /// <param name="standardOutput">输出信息</param>
- public static void PrinterTpfFile
- (
- string tpfPath,
- int printerNum,
- string printerName,
- out int printResult,
- out string standardError,
- out string standardOutput
- )
- {
- tpfPath = tpfPath.Replace("/", "\\");
- //XML点阵文件位置
- //string XmlFile = FileToolsCommon.GetFileAbsolutePath("/LatticeXML/print_license.xml");
- string contentParameter = " -sMode=Print \"-sTPF=" + tpfPath + "\" \"-sPrinter=" + printerName + "\" \"-dCopy=" + printerNum + "\""; //\"-dPaperSize=9\"
-
- #region 打印参数说明
-
- //-sMode = Print,参数如下:
- //PrintTool.exe - sMode = Print - sTPF = [-dCopy =][-dPaperSize =] - sPrinter =
- //-sTPF 需要打印的 TPF 文件路径
- //- dCopy 整数,可选参数,打印份数,默认 1
- //- dPaperSize 整数,可选参数,设置打印纸张尺寸。不设置此值,系统自动选择匹配纸张打印;A4:"-dPaperSize=9" DMPAPER_A4 9 A4 210 x 297 mm 如
- // 需要手动设置纸张尺寸,不同尺寸纸张对应值,参见 https://docs.microsoft.com/en-us/windows/win32/intl/paper-sizes
- //- sPrinter 打印机名称
- // 打印完成后,如果打印成功,PrintTool.exe 退出值为 0。
- //如果打印失败,程序退出值为非 0。
-
- #endregion
-
- while (PrintProcess != null)
- {
- Thread.Sleep(100);
- }
- Process[] killProcessArray = Process.GetProcessesByName("PrintTool");
- foreach (Process killProcess in killProcessArray)
- {
- killProcess.Kill();
- }
- PrintProcess = new Process();
- _logPath = CreateLog();
- PrintProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath("/PrintTool/PrintTool.exe").Replace("/", "\\");
- PrintProcess.StartInfo.Arguments = contentParameter;
- PrintProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- PrintProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- PrintProcess.StartInfo.RedirectStandardOutput = true;
- PrintProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- PrintProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
- PrintProcess.ErrorDataReceived += Output;
- PrintProcess.Start(); //启动线程
- //PrintProcess.BeginErrorReadLine(); //开始异步读取
- PrintProcess.WaitForExit(); //阻塞等待进程结束
- standardError = PrintProcess.StandardError.ReadToEnd();
- standardOutput = PrintProcess.StandardOutput.ReadToEnd();
- Output("【制作点阵文件】错误:" + standardError);
- Output("【制作点阵文件】返回信息:" + standardOutput);
- printResult = PrintProcess.ExitCode; //打印结果 非0则为打印失败
- Output("【打印TPF文件】" + contentParameter); //记录打印日志
- PrintProcess.Close(); //关闭进程
- PrintProcess.Dispose(); //释放资源
-
- #region 进程是否已经释放
-
- bool isRunning = true;
- while (isRunning)
- {
- isRunning = false;
- Process[] processArray = Process.GetProcessesByName("PrintTool");
- if (processArray.Length > 0)
- {
- isRunning = true;
- Thread.Sleep(100);
- }
- }
-
- #endregion
-
- PrintProcess = null;
- }
-
- /// <summary>
- /// 创建日志文件
- /// </summary>
- /// <returns></returns>
- private static string CreateLog()
- {
- string logFileName = DateTime.Now.ToString("yyyyMMdd");
- string logFilePath = FileToolsCommon.GetFileAbsolutePath("/Log/PrintLog/");
- FileToolsCommon.CreateDirectory(logFilePath);
- string logName = logFilePath + logFileName + ".log";
- try
- {
- if (!FileToolsCommon.IsExistFile(logName))
- {
- FileToolsCommon.CreateFile(logName);
- //FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
- // "****************************************************************************************************\r\n");
- return logName;
- }
- else
- {
- int num = 0;
- while (FileToolsCommon.GetFileSizeByMb(logName) > 2)
- {
- num++;
- logName = logFilePath + logFileName + "_" + num + ".log";
- FileToolsCommon.CreateFile(logName);
- }
- //FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
- // "****************************************************************************************************\r\n");
- return logName;
- }
- }
- catch (Exception)
- {
- return logName;
- }
- }
-
- /// <summary>
- /// 输出日志
- /// </summary>
- /// <param name="message"></param>
- private static void Output(string message)
- {
- if (!string.IsNullOrEmpty(message))
- {
- FileToolsCommon.AppendText(_logPath, message + "\r\n");
- FileToolsCommon.AppendText(_logPath, "\r\n****************************************************************************************************" + "****************************************************************************************************\r\n");
- }
- }
-
- /// <summary>
- /// 输出结果
- /// </summary>
- /// <param name="sendProcess"></param>
- /// <param name="output"></param>
- private static void Output(object sendProcess, DataReceivedEventArgs output)
- {
- Output(output.Data);
- }
- }
- }
|