private void cBoxPrint_SelectedIndexChanged(object sender, EventArgs e) { if (cBoxPrint.SelectedItem != null) { string printerName = cBoxPrint.SelectedItem.ToString(); btnPrint.Text = "打印(" + PrinterHelper.GetPrinterStatus(printerName) + ")"; } }
public ActionResult <ExecutePrintResponse> PostExecutePrint(ExecutePrintRequest executePrintRequest) { //更新TodoItem string taskId = executePrintRequest.TaskId; string printName = executePrintRequest.PrintName; string printType = executePrintRequest.PrintType; int printCount = executePrintRequest.PrintCount; string fileName = executePrintRequest.FileName; ExecutePrintResponse executePrintResponse = new ExecutePrintResponse { TaskId = taskId, FileName = fileName, PrintName = printName, PrintType = printType, PrintCount = printCount, Result = true, Msg = "PreLoad Success" }; if (PrinterHelper.GetPrinterStatus(printName) == -1) { executePrintResponse.Result = false; executePrintResponse.Msg = "Not Such Printer In The Server"; } else { TodoItem todoItem = _context.TodoItems.Find(taskId); todoItem.PrintName = printName; todoItem.PrintType = printType; todoItem.PrintCount = printCount; todoItem.FileName = fileName; todoItem.IsComplete = true; _context.Entry(todoItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.SaveChanges(); if (printType == "Bartend") { //生成预加载Bartender任务 string preLoadBartender = executePrintRequest.PreLoadBartender(_hostingEnvironment.WebRootPath, this.TemplateFolder, this.BartendExePath); LogHelper.WriteLog(preLoadBartender, new Exception("PostExecutePrint")); //预加载Bartender //DosCommandOutputHelper.Execute(preLoadBartender, 3000); } else if (printType == "Excel") { Configurator.Put(".xlsx", new WorkbookLoader()); } } //执行打印结果 return(executePrintResponse); }
private void btnPrint_Click(object sender, EventArgs e) { if (!_printDataList.Exists(a => !a.IsPrinted)) { MessageBox.Show("没有需要打印的内容"); return; } if (cBoxPrint.SelectedItem == null) { MessageBox.Show("请选择打印机"); return; } else if (PrinterHelper.GetPrinterStatusInt(cBoxPrint.SelectedItem.ToString()) != 0) { MessageBox.Show(PrinterHelper.GetPrinterStatus(cBoxPrint.SelectedItem.ToString())); return; } //打印机名称 string printerName = ""; PrinterSettings settings = new PrinterSettings(); printerName = cBoxPrint.SelectedItem.ToString(); btnPrint.Enabled = false; foreach (MutltiPrintData printData in _printDataList.Where(a => !a.IsPrinted)) { #region 设置打印参数 DataGridViewRow row = GetRow(printData.ID); row.Cells["colPrintInfo"].Value = "发送打印中..."; //纸张规格 Dictionary <string, string> paperList = Helper.GetPaperSizes(); if (!paperList.ContainsKey(printData.PaperSize)) { row.Cells["colPrintInfo"].Value = "未找到打印纸为" + printData.PaperSize + "的尺寸参数"; continue; } string[] wh = paperList[printData.PaperSize].Split('*'); int paperW = (int)(Helper.MMToInch(Convert.ToInt32(wh[0])) * 100); int paperH = (int)(Helper.MMToInch(Convert.ToInt32(wh[1])) * 100); PaperSize paper = new PaperSize(printData.PaperSize, paperW, paperH); //是否竖打 bool isVertical = Convert.ToBoolean(row.Cells["colIsVertical"].Value); //打印份数 int count = 1; if (!Int32.TryParse(row.Cells["colCount"].Value.ToString(), out count)) { row.Cells["colPrintInfo"].Value = "打印份数格式错误"; continue; } #endregion #region 从文件服务器拷贝源文件至本机 //文件类型判断 string fileName = printData.PdfFile; string fileExt = ""; if (string.IsNullOrEmpty(fileName)) { fileName = printData.PlotFile; } if (!string.IsNullOrEmpty(fileName)) { fileExt = System.IO.Path.GetExtension(fileName); } if (string.IsNullOrEmpty(fileExt)) { row.Cells["colPrintInfo"].Value = "无法判断文件类型"; continue; } else if (fileExt.ToUpper() != ".PDF") { row.Cells["colPrintInfo"].Value = "只支持pdf格式"; continue; } //文件拷贝本地及获取本地路径 string filePath = ""; string FieldID = printData.PdfFile.Split('_')[0]; string errorInfo = Helper.CopyFile(ref filePath, FieldID); if (!string.IsNullOrEmpty(errorInfo)) { row.Cells["colPrintInfo"].Value = errorInfo; continue; } ////test文件拷贝本地及获取本地路径 //string filePath = ""; //string errorInfo = Helper.TestCopyFile(ref filePath, ConfigurationManager.AppSettings["TestPrintFile"]); //if (!string.IsNullOrEmpty(errorInfo)) //{ // row.Cells["colPrintInfo"].Value = errorInfo; // continue; //} //string fileExt = System.IO.Path.GetExtension(ConfigurationManager.AppSettings["TestPrintFile"]); #endregion #region 开始发送打印 string printErrorInfo = PrinterHelper.UsePDFRender4NetToPrintPdf(filePath, printerName, paper, isVertical, count); //删除临时文件 Helper.DelFile(filePath); if (!string.IsNullOrEmpty(printErrorInfo)) { row.Cells["colPrintInfo"].Value = printErrorInfo; continue; } #endregion #region 更新数据库信息 printData.IsPrinted = true; row.Cells["colPrintInfo"].Value = "已发送至打印机"; if (AfterSendToPrinter != null) { AfterSendToPrinter(printData.ID, printData.Count); } #endregion } btnPrint.Enabled = true; }
private void cBoxPrint_SelectedIndexChanged(object sender, EventArgs e) { string state = PrinterHelper.GetPrinterStatus(cBoxPrint.SelectedItem.ToString()); MessageBox.Show(state); }