Example #1
0
        private void HandlerNormalConfig(string normalConfig)
        {
            List <string>   itemArr = new List <string>();
            MatchCollection mc      = Regex.Matches(normalConfig, @"\[([^\]]*)\]");

            foreach (Match m in mc)
            {
                itemArr.Add(m.Groups[1].Value);
            }
            //处理每一项
            foreach (string str in itemArr)
            {
                if (str.Contains("Line"))
                {
                    foreach (PrintLine line in _curPrintConfig.PrintLines.LineList)
                    {
                        if (line.LineName == str)
                        {
                            int startPX = 0, endPX = 0;
                            if (line.StartPX.IndexOf('%') > 0)
                            {
                                string strAlign = line.StartPX;
                                string number   = strAlign.Substring(0, strAlign.Length - 1);
                                startPX = Convert.ToInt32(float.Parse(number) / 100f * _curPrintConfig.TotalCharNum);
                            }
                            else
                            {
                                startPX = int.Parse(line.StartPX.Trim());
                            }
                            if (line.EndPX.IndexOf('%') > 0)
                            {
                                string strAlign = line.EndPX;
                                string number   = strAlign.Substring(0, strAlign.Length - 1);
                                endPX = Convert.ToInt32(float.Parse(number) / 100f * _curPrintConfig.TotalCharNum);
                            }
                            else
                            {
                                endPX = int.Parse(line.EndPX.Trim());
                            }
                            string text = ArrangeLinePosition(line.LineText, startPX, endPX);
                            //打印设置
                            _printHelper.SetFontSize(0, 0);
                            //_printHelper.SetCharSpacing(0);
                            _printHelper.Write(text);
                            break;
                        }
                    }
                }
                else
                {
                    foreach (NormalConfig item in _curPrintConfig.NormalConfigs.NormalConfigList)
                    {
                        if (item.Name == str)
                        {
                            string itemName = item.ValuePrefix + _printData.GetValue(item.Name);
                            int    width    = 0;
                            if (item.Width.IndexOf('%') > 0)
                            {
                                string strWidth = item.Width;
                                string number   = strWidth.Substring(0, strWidth.Length - 1);
                                width = Convert.ToInt32(float.Parse(number) / 100f * _curPrintConfig.TotalCharNum);
                            }
                            else
                            {
                                width = int.Parse(item.Width.Trim());
                            }
                            TextAlign align;
                            if (item.Align.Equals("Left", StringComparison.InvariantCultureIgnoreCase))
                            {
                                align = TextAlign.Left;
                            }
                            else if (item.Align.Equals("Center", StringComparison.InvariantCultureIgnoreCase))
                            {
                                align = TextAlign.Center;
                            }
                            else if (item.Align.Equals("Right", StringComparison.InvariantCultureIgnoreCase))
                            {
                                align = TextAlign.Right;
                            }
                            else
                            {
                                align = TextAlign.Left;
                            }
                            string[] times       = item.FontSize.Split(',');
                            int      widthTimes  = int.Parse(times[0]);
                            int      heightTimes = int.Parse(times[1]);
                            //打印设置
                            _printHelper.SetFontSize(widthTimes, heightTimes);
                            //_printHelper.SetCharSpacing(0);
                            _printHelper.Write(itemName, align, width);
                            break;
                        }
                    }
                }
            }
            _printHelper.PrintAndFeedLines(1);
        }
Example #2
0
        private void HandlerNormalConfig(string normalConfig)
        {
            List <string>   itemArr = new List <string>();
            MatchCollection mc      = Regex.Matches(normalConfig, @"\[([^\]]*)\]");

            foreach (Match m in mc)
            {
                itemArr.Add(m.Groups[1].Value);
            }
            //处理每一项
            float maxHeight = 0f;
            float px        = MarginLeft;

            foreach (string str in itemArr)
            {
                if (str.Contains("Line"))
                {
                    foreach (PrintLine line in _curPrintConfig.PrintLines.LineList)
                    {
                        if (line.LineName == str)
                        {
                            Font  font = new Font(line.Font.FontName, line.Font.FontSize, line.Font.FontStyle);
                            Color color = Color.FromName(line.Font.ForeColor);
                            Brush brush = new SolidBrush(color);
                            SizeF size = m_Graphics.MeasureString(line.LineText, font);//测量字体的大小
                            float startPX = 0f, endPX = 0f;
                            if (line.StartPX.IndexOf('%') > 0)
                            {
                                string strAlign = line.StartPX;
                                string number   = strAlign.Substring(0, strAlign.Length - 1);
                                startPX = float.Parse(number) / 100 * (m_PageBounds.Width - 2 * MarginLeft);
                            }
                            else
                            {
                                startPX = float.Parse(line.StartPX.Trim());
                            }
                            if (line.EndPX.IndexOf('%') > 0)
                            {
                                string strAlign = line.EndPX;
                                string number   = strAlign.Substring(0, strAlign.Length - 1);
                                endPX = float.Parse(number) / 100 * (m_PageBounds.Width - 2 * MarginLeft);
                            }
                            else
                            {
                                endPX = float.Parse(line.EndPX.Trim());
                            }
                            //计算分割线重复的数量
                            int           count  = Convert.ToInt32((endPX - startPX) / size.Width);
                            StringBuilder sbLine = new StringBuilder(2 * count);
                            for (int i = 0; i < count; i++)
                            {
                                sbLine.Append(line.LineText);
                            }
                            //判断分割线是否符合长度
                            do
                            {
                                sbLine.Append(line.LineText);
                                size = m_Graphics.MeasureString(sbLine.ToString(), font);//测量分割线的大小
                            } while (size.Width < endPX - startPX);
                            m_Graphics.DrawString(sbLine.ToString(), font, brush, MarginLeft + startPX, PY);
                            maxHeight = size.Height;
                            break;
                        }
                    }
                }
                else
                {
                    foreach (NormalConfig item in _curPrintConfig.NormalConfigs.NormalConfigList)
                    {
                        if (item.Name == str)
                        {
                            Font   font      = new Font(item.Font.FontName, item.Font.FontSize, item.Font.FontStyle);
                            Color  color     = Color.FromName(item.Font.ForeColor);
                            Brush  brush     = new SolidBrush(color);
                            string itemName  = item.ValuePrefix + _printData.GetValue(item.Name);
                            SizeF  size      = m_Graphics.MeasureString(itemName, font);//测量字体的大小
                            float  itemWidth = 0f;
                            if (item.Width.IndexOf('%') > 0)
                            {
                                string strWidth = item.Width.Trim();
                                string number   = strWidth.Substring(0, strWidth.Length - 1);
                                itemWidth = float.Parse(number) / 100 * (m_PageBounds.Width - 2 * MarginLeft);
                            }
                            else
                            {
                                itemWidth = float.Parse(item.Width.Trim());
                            }
                            float itemHeight = 0f;
                            if (size.Width > itemWidth)
                            {
                                itemHeight = (float)Math.Ceiling(Convert.ToDecimal(size.Width) / Convert.ToDecimal(itemWidth)) * size.Height;
                            }
                            else
                            {
                                itemHeight = size.Height;
                            }
                            if (itemHeight > maxHeight)
                            {
                                maxHeight = itemHeight;
                            }
                            RectangleF   drawRect = new RectangleF(px, PY, itemWidth, itemHeight);
                            StringFormat sf       = new StringFormat();
                            sf.LineAlignment = StringAlignment.Center;
                            if (item.Align.Equals("Left", StringComparison.InvariantCultureIgnoreCase))
                            {
                                sf.Alignment = StringAlignment.Near;
                            }
                            else if (item.Align.Equals("Center", StringComparison.InvariantCultureIgnoreCase))
                            {
                                sf.Alignment = StringAlignment.Center;
                            }
                            else if (item.Align.Equals("Right", StringComparison.InvariantCultureIgnoreCase))
                            {
                                sf.Alignment = StringAlignment.Far;
                            }
                            m_Graphics.DrawString(itemName, font, brush, drawRect, sf);
                            px += itemWidth;
                            break;
                        }
                    }
                }
            }
            PY += maxHeight;
        }