Ejemplo n.º 1
0
        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;
        }