public static string PrintBarCode(string barCode, string codeType, SortedList parameters) { string result = string.Empty; DataRow[] printTemplate = CnasBaseData.SystemBaseData.Select(string.Format("type_code='HCS_barcode_type' and key_code='{0}'", codeType)); if (printTemplate.Length > 0 && printTemplate[0]["other_code"] != null) { string template = printTemplate[0]["other_code"].ToString().Trim(); if (!string.IsNullOrEmpty(template)) { if (!parameters.ContainsKey("BarcodeValue")) { parameters.Add("BarcodeValue", barCode); } BarCodeParameterHelper helper = new BarCodeParameterHelper(); helper.GetParameters(template, barCode, parameters); Barcode_print barCodePrinter = new Barcode_print(template, parameters); AdoptPrinter adopter = new AdoptPrinter(barCodePrinter.PrintDialog); adopter.PrintDPI = barCodePrinter.DesignFormData01.DPI; adopter.GetPrintSetting(CnasBaseData.MacAddress, 1); if (adopter.PrinterDialogResult == System.Windows.Forms.DialogResult.OK) { barCodePrinter.ShowDialog(); adopter.SetBackToSystemDefaultPrint(); } } else { result = "对不起!没有设置好条码的打印模版,请联系管理员"; } } else { result = "对不起!没有设置好条码的打印模版,请联系管理员"; } return(result); }
public void Print_DataGridView(DataGridView dgv_01, string inXML, string barCode, string[] concent) { barCodeValue = ""; _adopter.GetPrintSetting(CnasBaseData.MacAddress); if (concent == null) { concent = printContentValue; } try { if (dgv_01.Rows.Count == 0) { MessageBox.Show("没有需要打印的数据", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (_adopter.PrinterDialogResult != DialogResult.OK) { return; } // Getting DataGridView object to print dgv = dgv_01; // Getting all Coulmns Names in the DataGridView AvailableColumns.Clear(); foreach (DataGridViewColumn c in dgv.Columns) { if (!c.Visible) { continue; } AvailableColumns.Add(c.HeaderText); } // Showing the PrintOption Form //PrintOptions dlg = new PrintOptions(AvailableColumns, titleList); #region 获取XML XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(inXML); XmlElement printTitleValueXML = xmldoc["PrintData"]["Title"]["value"]; XmlElement printTitleSizeXML = xmldoc["PrintData"]["Attribute"]["titlesize"]; XmlElement printTitleColorXML = xmldoc["PrintData"]["Attribute"]["titlecolor"]; XmlElement printTitleStyleXML = xmldoc["PrintData"]["Attribute"]["titlestyle"]; XmlElement printTitleFontXML = xmldoc["PrintData"]["Attribute"]["titlefont"]; XmlElement printconSizeXML = xmldoc["PrintData"]["Attribute"]["consize"]; XmlElement printconColorXML = xmldoc["PrintData"]["Attribute"]["concolor"]; XmlElement printconStyleXML = xmldoc["PrintData"]["Attribute"]["constyle"]; XmlElement printconFontXML = xmldoc["PrintData"]["Attribute"]["confont"]; XmlElement printdgvSizeXML = xmldoc["PrintData"]["DataGrid"]["dgvfontSize"]; XmlElement printdgvFontXML = xmldoc["PrintData"]["DataGrid"]["dgvfont"]; XmlElement printdgvStyleXML = xmldoc["PrintData"]["DataGrid"]["dgvfontstyle"]; XmlElement printContentDescriptionXML = xmldoc["PrintData"]["Content"]["description"]; XmlElement printContentKeyXML = xmldoc["PrintData"]["Content"]["key"]; XmlElement printContentValueXML = xmldoc["PrintData"]["Content"]["value"]; string printTitleValue = printTitleValueXML != null ? printTitleValueXML.InnerXml : string.Empty; //标题 string printTitleSize = printTitleSizeXML != null ? printTitleSizeXML.InnerXml : "10"; //标题字体大小 string printTitleColor = printTitleColorXML != null ? printTitleColorXML.InnerXml : Color.Black.ToString(); //标题字体颜色 string printTitleStyle = printTitleStyleXML != null ? printTitleStyleXML.InnerXml : FontStyle.Regular.ToString(); //标题字体风格 string printTitleFont = printTitleFontXML != null ? printTitleFontXML.InnerXml : "新宋体"; //标题字体 string printconSize = printconSizeXML != null ? printconSizeXML.InnerXml : "9"; //内容字体大小 string printconColor = printconColorXML != null ? printconColorXML.InnerXml : Color.Black.ToString(); //内容字体颜色 string printconStyle = printconStyleXML != null ? printconStyleXML.InnerXml : FontStyle.Regular.ToString(); //内容字体风格 string printconFont = printconFontXML != null ? printconFontXML.InnerXml : string.Empty; //内容字体 string printdgvSize = printdgvSizeXML != null ? printdgvSizeXML.InnerXml : "9"; //Dgv字体大小 string printdgvFont = printdgvFontXML != null ? printdgvFontXML.InnerXml : "新宋体"; //Dgv字体 string printdgvStyle = printdgvStyleXML != null ? printdgvStyleXML.InnerXml : FontStyle.Regular.ToString(); //Dgv字体风格 string printContentDescription = printContentDescriptionXML.InnerXml; //列表的基本信息 string[] printContentKey = printContentKeyXML.InnerXml.Split(','); //列表的键 string[] printContentValue = printContentValueXML.InnerXml.Split(','); //列表的值 #endregion for (int i = 0; i < printContentKey.Length; i++) { if (concent == null || concent.Length == 0) { if (i < printContentValue.Length) { printContentDescription = printContentDescription.Replace(printContentKey[i], printContentValue[i]); } else { printContentDescription = printContentDescription.Replace(printContentKey[i], string.Empty); } } else { if (i >= concent.Length) { if (i < printContentValue.Length) { printContentDescription = printContentDescription.Replace(printContentKey[i], printContentValue[i]); } else { printContentDescription = printContentDescription.Replace(printContentKey[i], string.Empty); } } else { printContentDescription = printContentDescription.Replace(printContentKey[i], concent[i]); } } } #region 字体属性 //标题字体属性 FontColor = new SolidBrush(Color.FromName(printTitleColor)); string titlefont = "新宋体"; if (!string.IsNullOrEmpty(null)) { titlefont = "新宋体"; } else { titlefont = printTitleFont; } float titlesize = 20; float.TryParse(printTitleSize, out titlesize); if (titlesize == 0) { titlesize = 20; } FontStyle titleStyle = FontStyle.Bold; Enum.TryParse(printTitleStyle, out titleStyle); TitleFont = new Font(titlefont, titlesize, titleStyle); ConFontColor = new SolidBrush(Color.FromName(printconColor)); //内容字体属性 string confont = "新宋体"; if (!string.IsNullOrEmpty(null)) { confont = "新宋体"; } else { confont = printconFont; } FontStyle contentFontStyle = FontStyle.Regular; Enum.TryParse(printconStyle, out contentFontStyle); float consize = 12; float.TryParse(printconSize, out consize); if (consize == 0) { consize = 12; } ConFont = new Font(confont, consize, contentFontStyle); //dgv字体属性 string dgvfont = "新宋体"; if (!string.IsNullOrEmpty(null)) { dgvfont = "宋体"; } else { dgvfont = printdgvFont; } FontStyle dgvFontStyle = FontStyle.Regular; Enum.TryParse(printdgvStyle, out dgvFontStyle); float dgvsize = 9; float.TryParse(printdgvSize, out dgvsize); if (dgvsize == 0) { dgvsize = 9; } DgvFont = new Font(dgvfont, dgvsize, dgvFontStyle); #endregion PrintTitle = printTitleValue; //标题的值 // 判断用户是否有传入 barCode if (!string.IsNullOrEmpty(barCode)) { barCodeValue = barCode; } PrintContent = printContentDescription; //内容的值 PrintAllRows = true; //所有行 FitToPageWidth = true; //页宽 SelectedColumns = AvailableColumns; //打印的列 // PrintTitle += Printlbl; _printPreviewDialog = new PrintPreviewDialog(); _printPreviewDialog.Icon = new Icon(ResourcesImageHelper.Instance.GetResourcesStream("Common.icon", "MainIco", EnumImageType.ICO)); _printPreviewDialog.Document = printDoc; //获取预览文档 printDoc.PrinterSettings = _printDialog.PrinterSettings; // 展现每一页打印内容 printDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint); printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); _printPreviewDialog.FormClosed += OnPrintPreviewDialogClosed; if (_printPreviewDialog.ShowDialog() != DialogResult.OK) { printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint); printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); return; } //开始打印 printDoc.Print(); printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint); printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); printDoc.EndPrint += printDoc_EndPrint; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } }