Exemplo n.º 1
0
        private void setCellStyle(HSSFWorkbook workbook, ISheet sheet, ICell cell, I3PrintArea area, I3ReportCell cellData,
                                  Dictionary <string, HSSFCellStyle> styleDic, Dictionary <string, IFont> fontDic, bool hasReturnInText, bool isLastRow, bool isLastCol,
                                  HSSFCellStyle emptyStyle)
        {
            I3ReportCellStyle cs = null;

            if (cellData.MergState == I3MergeState.Merged)
            {
                cellData = area.ReportData.GetMergedStartedCell(cellData.Row, cellData.Col);
            }
            cs = area.ReportData.GetCellStyle(cellData.StyleName);
            if (cs == null)
            {
                cell.CellStyle = emptyStyle;
                return;
            }

            string        styleKey = getStyleKey(cellData, cs, hasReturnInText, isLastRow, isLastCol);
            HSSFCellStyle style    = null;

            if (styleDic.ContainsKey(styleKey))
            {
                style = styleDic[styleKey];
            }
            else
            {
                style = createStyle(workbook, cellData, cs, fontDic, hasReturnInText, isLastRow, isLastCol);
                styleDic.Add(styleKey, style);
            }

            cell.CellStyle = style;
        }
Exemplo n.º 2
0
        private SizeF CalCellNeedSize(int orgWidth, int orgHeight, I3ReportCellStyle style, I3ReportCell cell, float testFontSize)
        {
            SizeF        sizeF        = new SizeF(orgWidth, orgHeight);
            Bitmap       bm           = new Bitmap(1, 1);
            Graphics     g            = Graphics.FromImage(bm);
            StringFormat stringFormat = StringFormat.GenericDefault;
            Brush        brush        = new SolidBrush(style.FontColor);
            Font         font         = new Font(style.FontName, testFontSize, style.FontStyle, GraphicsUnit.Pixel);

            //TextFormatFlags ff = GetTextFormatFlags(cell, style);

            try
            {
                stringFormat.Alignment     = style.Alignment;
                stringFormat.LineAlignment = style.LineAlignment;
                stringFormat.Trimming      = StringTrimming.None; //不减小内容以测试实际需要的大小
                stringFormat.FormatFlags   = style.WordWrap ? (StringFormatFlags)0 : StringFormatFlags.NoWrap;
                Rectangle rect = new Rectangle(0, 0, orgWidth, orgHeight);
                sizeF = style.WordWrap ? g.MeasureString(cell.Text, font, rect.Width, stringFormat)                            //换行以测试高度  //这里仍然用cell.Text测试,还没有分页
                                                                       : g.MeasureString(cell.Text, font, 1000, stringFormat); //不换行测试宽度,最大宽度1000;
                //sizeF = style.WordWrap ? TextRenderer.MeasureText(g, cell.Text, font, new Size((int)rect.Width, 200), ff)   //换行以测试高度  //这里仍然用cell.Text测试,还没有分页
                //                                                       : TextRenderer.MeasureText(g, cell.Text, font, new Size(1000, 200), ff);  //不换行测试宽度,最大宽度1000;
            }
            finally
            {
                font.Dispose();
                brush.Dispose();
                stringFormat.Dispose();
                g.Dispose();
                bm.Dispose();
            }
            return(sizeF);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 增加单元样式属性
        /// </summary>
        /// <param name="styleName"></param>
        /// <returns></returns>
        public I3ReportCellStyle AddCellStyle(string styleName)
        {
            I3ReportCellStyle style = new I3ReportCellStyle(styleName);

            cellStyles.Add(styleName, style);
            return(style);
        }
Exemplo n.º 4
0
        public override SizeF CalCellNeedSize(int orgWidth, int orgHeight, I3ReportCellStyle style, I3ReportCell cell)
        {
            SizeF             sizeF     = new SizeF();
            I3ReportImageCell imageCell = cell as I3ReportImageCell;

            sizeF.Width  = imageCell.Width;
            sizeF.Height = imageCell.Height;
            return(sizeF);
        }
Exemplo n.º 5
0
        public override void AdjustCellSize(int width, int height, SizeF needSizeF, I3ReportCellStyle style, I3ReportCell cell, I3MergeRange range, I3ReportData reportData, bool prepareNarrow)
        {
            I3ReportImageCell imageCell = cell as I3ReportImageCell;
            int doubleSplit             = ImageSplit * 2;

            if (needSizeF.Width > width - doubleSplit || needSizeF.Height > height - doubleSplit)
            {
                switch (style.AdjustSize)
                {
                case I3AdjustSize.扩大单元格:
                    #region 扩大单元格
                    if (needSizeF.Width > width - doubleSplit)
                    {
                        float pro = (needSizeF.Width + doubleSplit) / width;
                        for (int i = range.StartCol; i <= range.EndCol; i++)
                        {
                            reportData.Cols[i].Width = (int)(reportData.Cols[i].Width * pro);
                        }
                    }
                    if (needSizeF.Height > height - doubleSplit)
                    {
                        float pro = (needSizeF.Height + doubleSplit) / height;
                        for (int i = range.StartRow; i <= range.EndRow; i++)
                        {
                            reportData[i].Height = (int)(reportData[i].Height * pro);
                        }
                    }
                    cell.StringTrimming = StringTrimming.None;
                    #endregion
                    break;

                case I3AdjustSize.缩小内容:
                    #region 缩小内容
                    if (prepareNarrow)
                    {
                        //先使宽度一致
                        imageCell.CalWidth  = width - doubleSplit;
                        imageCell.CalHeight = imageCell.Height * imageCell.CalWidth / imageCell.Width;
                        //如果高度太大,则使高度一致
                        if (imageCell.CalHeight > height - doubleSplit)
                        {
                            imageCell.CalHeight = height - doubleSplit;
                            imageCell.CalWidth  = imageCell.Width * imageCell.CalHeight / imageCell.Height;
                        }
                    }
                    #endregion
                    break;

                default:       //都不变,调整为图片不按比例缩小
                    #region 图片不按比例缩小
                    imageCell.CalWidth  = width - doubleSplit;
                    imageCell.CalHeight = height - doubleSplit;
                    #endregion
                    break;
                }
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// 增加单元样式属性
 /// </summary>
 /// <param name="style"></param>
 /// <returns></returns>
 public I3ReportCellStyle AddCellStyle(I3ReportCellStyle style)
 {
     if (string.IsNullOrEmpty(style.Name))
     {
         throw new Exception("styleName不能为空");
     }
     if (cellStyles.ContainsKey(style.Name))
     {
         cellStyles.Remove(style.Name);
     }
     cellStyles.Add(style.Name, style);
     return(style);
 }
Exemplo n.º 7
0
        //protected virtual TextFormatFlags GetTextFormatFlags(I3ReportCell cell, I3ReportCellStyle style)
        //{
        //    TextFormatFlags sf = TextFormatFlags.Default;

        //    switch (style.Alignment)
        //    {
        //        case StringAlignment.Near:
        //            sf = sf | TextFormatFlags.Left;
        //            break;
        //        case StringAlignment.Center:
        //            sf = sf | TextFormatFlags.HorizontalCenter;
        //            break;
        //        case StringAlignment.Far:
        //            sf = sf | TextFormatFlags.Right;
        //            break;
        //    }
        //    switch (style.LineAlignment)
        //    {
        //        case StringAlignment.Near:
        //            sf = sf | TextFormatFlags.Top;
        //            break;
        //        case StringAlignment.Center:
        //            sf = sf | TextFormatFlags.VerticalCenter;
        //            break;
        //        case StringAlignment.Far:
        //            sf = sf | TextFormatFlags.Bottom;
        //            break;
        //    }
        //    if (!string.IsNullOrEmpty(cell.Text) && cell.Text.IndexOf(NewLineFlag) >= 0)//文本中手工插入了换行符,强制不能进行自动换行
        //    {
        //        //sf.FormatFlags = StringFormatFlags.NoWrap;
        //    }
        //    else
        //    {
        //        if (style.WordWrap)
        //        {
        //            sf = sf | TextFormatFlags.WordBreak;
        //            sf = sf | TextFormatFlags.NoPadding;
        //        }
        //    }

        //    //sf.Trimming = cell.StringTrimming;
        //    //sf.FormatFlags = sf.FormatFlags | StringFormatFlags.NoClip; //上下有显示不全而需要剪切时,指定不剪切
        //    return sf;
        //}

        protected virtual Font GetFont(float scale, I3ReportCell cell, I3ReportCellStyle style)
        {
            float fontSize = 0;

            if (cell.CalFontSize > 0)
            {
                fontSize = cell.CalFontSize;
            }
            else
            {
                fontSize = style.FontSize == 0 ? 13 : style.FontSize;
            }
            fontSize *= scale;
            Font font = new Font(style.FontName, fontSize, style.FontStyle, GraphicsUnit.Pixel);

            return(font);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 重新计算单元格的大小,借以调整行、列的大小
        /// prepareNarrow:是否处理内容缩放
        /// </summary>
        /// <param name="reportData"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        private static void ReCalCellSize(I3ReportData reportData, int row, int col, bool prepareNarrow)
        {
            #region 获取单元格、样式、合并区域对象
            I3ReportCell cell = reportData.GetCellItem(row, col);
            if (cell == null || cell.MergState == I3MergeState.Merged)  //单元格为空,或者是被合并的,不需要重新计算
            {
                return;
            }
            I3ReportCellStyle style = reportData.GetCellStyle(cell.StyleName); //没有样式设置,不用重新计算
            if (style == null)
            {
                return;
            }
            I3MergeRange range = cell.MergState == I3MergeState.FirstCell ? reportData.GetMergeRange(row, col) : null;
            if (range == null)
            {
                range = new I3MergeRange((short)row, (short)col, (short)row, (short)col);
            }
            #endregion

            #region 得到默认宽度、高度
            int width  = 0;
            int height = 0;
            for (int i = range.StartRow; i <= range.EndRow; i++)
            {
                height += reportData[i].Height;
            }
            for (int i = range.StartCol; i <= range.EndCol; i++)
            {
                width += reportData.Cols[i].Width;
            }
            if (width == 0 || height == 0)
            {
                return;
            }
            #endregion


            II3CellRenderer renderer = I3CellRendererBuilder.GetRenderer(reportData[row][col]);
            SizeF           needSize = renderer.CalCellNeedSize(width, height, style, cell);
            if (needSize != SizeF.Empty)
            {
                renderer.AdjustCellSize(width, height, needSize, style, cell, range, reportData, prepareNarrow);
            }
        }
Exemplo n.º 9
0
        protected virtual StringFormat GetStringFormat(I3ReportCell cell, I3ReportCellStyle style)
        {
            StringFormat sf = StringFormat.GenericDefault;

            sf.Alignment     = style.Alignment;
            sf.LineAlignment = style.LineAlignment;
            sf.Trimming      = cell.StringTrimming;
            if (!string.IsNullOrEmpty(cell.Text) && cell.Text.IndexOf(NewLineFlag) >= 0)//文本中手工插入了换行符,强制不能进行自动换行
            {
                sf.FormatFlags = StringFormatFlags.NoWrap;
            }
            else
            {
                sf.FormatFlags = style.WordWrap ? (StringFormatFlags)0 : StringFormatFlags.NoWrap;
            }
            sf.FormatFlags = sf.FormatFlags | StringFormatFlags.NoClip; //上下有显示不全而需要剪切时,指定不剪切
            return(sf);
        }
Exemplo n.º 10
0
        private string getStyleKey(I3ReportCell cellData, I3ReportCellStyle cs, bool hasReturnInText, bool isLastRow, bool isLastCol)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(cs.TopBorder == null ? "nullTopBorder" : cs.TopBorder.ToString());
            sb.Append(cs.LeftBorder == null ? "nullLeftBorder" : cs.LeftBorder.ToString());
            sb.Append(cs.RightBorder == null ? "nullRightBorder" : cs.RightBorder.ToString());
            sb.Append(cs.BottomBorder == null ? "nullBottomBorder" : cs.BottomBorder.ToString());
            sb.Append(cs.Alignment);
            sb.Append(cs.LineAlignment);
            double fontSize = cs.AdjustSize == I3AdjustSize.缩小内容 && cellData.HasCalFontSize ? (double)cellData.CalFontSize : (double)cs.FontSize;

            sb.Append(fontSize);
            sb.Append(cs.FontName);
            sb.Append(cs.FontColor);
            sb.Append(cs.FontStyle);
            sb.Append(cs.WordWrap || hasReturnInText);
            sb.Append(isLastRow);
            sb.Append(isLastCol);
            sb.Append(cs.AdjustSize);
            return(sb.ToString().GetHashCode().ToString());
        }
        /// <summary>
        /// 右上角
        /// </summary>
        /// <param name="text"></param>
        private void DrawString2(string text, Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style, I3PrintArea area)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            StringFormat sf    = GetStringFormat(cell, style);
            Brush        brush = new SolidBrush(style.FontColor);
            Font         font  = GetFont(scale, cell, style);

            try
            {
                #region 计算文本绘制区域
                SizeF      sizeF = g.MeasureString(text, font, (int)rect.Width, sf);
                RectangleF r     = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
                r.X     += r.Width - 3 - sizeF.Width; //居右
                r.Width  = sizeF.Width;
                r.Y     += 5;                         //居上
                r.Height = sizeF.Height;
                #endregion

                if (!r.IsEmpty)
                {
                    g.DrawString(text, font, brush, r, sf);
                }
            }
            finally
            {
                font.Dispose();
                brush.Dispose();
                sf.Dispose();
            }
        }
 public override void AdjustCellSize(int width, int height, SizeF needSizeF, I3ReportCellStyle style, I3ReportCell cell, I3MergeRange range, I3ReportData reportData, bool prepareNarrow)
 {
     //不做处理
 }
 public override SizeF CalCellNeedSize(int orgWidth, int orgHeight, I3ReportCellStyle style, I3ReportCell cell)
 {
     return(SizeF.Empty);  //返回空,表示不需要做大小调整
 }
        /// <summary>
        /// 斜中间
        /// </summary>
        /// <param name="text"></param>
        private void DrawString3(string text, Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style, I3PrintArea area)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            StringFormat sf    = GetStringFormat(cell, style);
            Brush        brush = new SolidBrush(style.FontColor);
            Font         font  = GetFont(scale, cell, style);

            try
            {
                char[] chars       = text.ToCharArray();
                float  posx        = rect.Right - 3;
                float  xsplit      = 1;
                float  posy        = rect.Bottom;
                float  totalHeigth = GetTotalHeight(chars, g, font, sf);
                float  ysplit      = chars.Length == 0 ? 0 : (rect.Height / 2 - totalHeigth) / (chars.Length - 1);

                for (int i = chars.Length - 1; i >= 0; i--)
                {
                    string     str   = chars[i].ToString();
                    SizeF      sizeF = g.MeasureString(str, font, (int)rect.Width, sf);
                    RectangleF r     = new RectangleF(posx - sizeF.Width, posy - sizeF.Height, sizeF.Width, sizeF.Height);

                    if (!r.IsEmpty)
                    {
                        g.DrawString(str, font, brush, r, sf);
                    }

                    posx = posx - sizeF.Width - xsplit;
                    posy = posy - sizeF.Height - ysplit;
                }
            }
            finally
            {
                font.Dispose();
                brush.Dispose();
                sf.Dispose();
            }
        }
        public override RectangleF DrawContent(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style, I3PrintArea area, bool draw)
        {
            string text = GetText(cell, area);

            if (!draw || string.IsNullOrEmpty(text))
            {
                return(RectangleF.Empty);
            }

            string[] values = text.Split(new char[] { '|', ',', ';' });


            if (values.Length > 2)
            {
                DrawLine2(g, rect);
                DrawString1(values[0], g, scale, reportData, cell, rect, style, area);
                DrawString2(values[1], g, scale, reportData, cell, rect, style, area);
                DrawString3(values[2], g, scale, reportData, cell, rect, style, area);
            }
            else
            {
                DrawLine1(g, rect);
                DrawString1(values[0], g, scale, reportData, cell, rect, style, area);
                if (values.Length > 1)
                {
                    DrawString2(values[1], g, scale, reportData, cell, rect, style, area);
                }
            }

            return(RectangleF.Empty);
        }
Exemplo n.º 16
0
        private HSSFCellStyle createStyle(HSSFWorkbook workbook, I3ReportCell cellData, I3ReportCellStyle cs, Dictionary <string, IFont> fontDic, bool hasReturnInText, bool isLastRow, bool isLastCol)
        {
            HSSFCellStyle style = (HSSFCellStyle)workbook.CreateCellStyle();

            //设置边框格式
            //top
            if (cs.TopBorder == null || cs.TopBorder.Width <= 0)
            {
                style.BorderTop = NPOI.SS.UserModel.BorderStyle.None;
            }
            else if (cs.TopBorder.Width >= 2)
            {
                style.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium;//粗实线
            }
            else
            {
                style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;//细实线
            }
            //left
            if (cs.LeftBorder == null || cs.LeftBorder.Width <= 0)
            {
                style.BorderLeft = NPOI.SS.UserModel.BorderStyle.None;
            }
            else if (cs.LeftBorder.Width >= 2)
            {
                style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium;
            }
            else
            {
                style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            }
            //right  //有边框行、列,右、下不用设置
            //if (isLastCol)
            //{
            //    if (cs.RightBorder == null || cs.RightBorder.Width <= 0)
            //    {
            //        style.BorderRight = NPOI.SS.UserModel.BorderStyle.None;
            //    }
            //    else if (cs.RightBorder.Width >= 2)
            //    {
            //        style.BorderRight = NPOI.SS.UserModel.BorderStyle.Medium;
            //    }
            //    else
            //    {
            //        style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //    }
            //}
            //bottom
            //if (isLastRow)
            //{
            //    if (cs.BottomBorder == null || cs.BottomBorder.Width <= 0)
            //    {
            //        style.BorderBottom = NPOI.SS.UserModel.BorderStyle.None;
            //    }
            //    else if (cs.BottomBorder.Width >= 2)
            //    {
            //        style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
            //    }
            //    else
            //    {
            //        style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            //    }
            //}

            //边框颜色
            style.LeftBorderColor   = HSSFColor.Black.Index;
            style.RightBorderColor  = HSSFColor.Black.Index;
            style.BottomBorderColor = HSSFColor.Black.Index;
            style.TopBorderColor    = HSSFColor.Black.Index;

            //居中
            switch (cs.Alignment) //水平
            {
            case StringAlignment.Center:
                style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                break;

            case StringAlignment.Far:
                style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
                break;

            default:
                style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
                break;
            }
            switch (cs.LineAlignment)  //垂直
            {
            case StringAlignment.Center:
                style.VerticalAlignment = VerticalAlignment.Center;
                break;

            case StringAlignment.Far:
                style.VerticalAlignment = VerticalAlignment.Bottom;
                break;

            default:
                style.VerticalAlignment = VerticalAlignment.Top;
                break;
            }

            //字体   //判断了自动缩小
            double fontSize = cs.AdjustSize == I3AdjustSize.缩小内容 && cellData.HasCalFontSize ? (double)cellData.CalFontSize : (double)cs.FontSize;
            var    fontHash = string.Format("{0}{1}{2}{3}", cs.FontName, fontSize, cs.FontColor.ToArgb(), cs.FontStyle).GetHashCode().ToString();

            if (fontDic.ContainsKey(fontHash))
            {
                style.SetFont(fontDic[fontHash]);
            }
            else
            {
                IFont font = workbook.CreateFont();
                font.FontName = cs.FontName;
                fontSize      = fontSize * (double)10 / (double)13;
                //fontSize = cs.FontSize * (double)72 / (double)96;
                font.FontHeightInPoints = (short)fontSize;
                style.SetFont(font);
                fontDic.Add(fontHash, font);
            }

            //自动换行
            style.WrapText = cs.WordWrap || hasReturnInText;

            //锁定
            style.IsLocked = cellData.Lock;

            return(style);
        }
Exemplo n.º 17
0
 public virtual void DrawBackground(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style)
 {
     using (Brush brush = new SolidBrush(style.BackColor))
     {
         g.FillRectangle(brush, rect);
     }
 }
Exemplo n.º 18
0
        public virtual RectangleF DrawContent(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style, I3PrintArea area, bool draw)
        {
            string text = GetText(cell, area);

            if (string.IsNullOrEmpty(text))
            {
                return(RectangleF.Empty);
            }

            StringFormat sf = GetStringFormat(cell, style);
            //TextFormatFlags ff = GetTextFormatFlags(cell, style);
            Brush brush = new SolidBrush(style.FontColor);
            Font  font  = GetFont(scale, cell, style);


            string[] strs = text.Split(new string[] { NewLineFlag }, StringSplitOptions.None);

            try
            {
                //先计算正常位置
                RectangleF        rr       = RectangleF.Empty;
                List <RectangleF> rectList = new List <RectangleF>();
                float             startY   = float.MaxValue; //上线
                float             endY     = float.MinValue; //下线
                foreach (string str in strs)
                {
                    #region 计算文本绘制区域
                    SizeF sizeF = g.MeasureString(str, font, (int)rect.Width, sf);
                    //Size sizeF = TextRenderer.MeasureText(g, str, font, new Size((int)rect.Width, 200), ff);
                    RectangleF r = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
                    if (style.Alignment == StringAlignment.Center)
                    {
                        r.X    += r.Width / 2 - sizeF.Width / 2;
                        r.Width = sizeF.Width;
                    }
                    else if (style.Alignment == StringAlignment.Far)
                    {
                        r.X    += r.Width - sizeF.Width;
                        r.Width = sizeF.Width;
                    }
                    else
                    {
                        r.Width = sizeF.Width;
                    }
                    if (style.LineAlignment == StringAlignment.Center)
                    {
                        r.Y     += r.Height / 2 - sizeF.Height / 2;
                        r.Height = sizeF.Height;
                    }
                    else if (style.LineAlignment == StringAlignment.Far)
                    {
                        r.Y     += r.Height - sizeF.Height;
                        r.Height = sizeF.Height;
                    }
                    else
                    {
                        r.Height = sizeF.Height;
                    }
                    #endregion

                    #region 居中或居下时的处理
                    if (style.LineAlignment == StringAlignment.Center)
                    {
                        //下面会有一点空白区域,下移以便于达到真正的垂直居中
                        //RectangleF stringRect = new RectangleF(r.X, r.Y + r.Height * 0.1F, r.Width, sizeF.Height);
                        //r = stringRect;
                        //2017.10.09  调整了一下偏移量,不然自动换行时,上面有间距而下面没有
                        RectangleF stringRect = new RectangleF(r.X, r.Y + r.Height * 0.05F, r.Width, sizeF.Height);
                        r = stringRect;
                    }
                    else if (style.LineAlignment == StringAlignment.Far)
                    {
                        //下面会有一点空白区域,下移以便于达到真正的垂直居下
                        RectangleF stringRect = new RectangleF(r.X, r.Y + r.Height * 0.2F, r.Width, r.Height);
                        r = stringRect;
                    }
                    #endregion

                    rectList.Add(r);
                    startY = Math.Min(startY, r.Y);
                    endY   = Math.Max(endY, r.Y + r.Height);
                }
                float centerY = startY + (endY - startY) / 2;//中线
                float stepY   = endY - startY;

                //再计算换行引起的位置偏移
                int i = -1;
                foreach (string str in strs)
                {
                    i++;
                    RectangleF r = rectList[i];
                    #region 换行位置调整
                    if (strs.Length > 0)                                   //有多行才做调整
                    {
                        if (style.LineAlignment == StringAlignment.Center) //居中
                        {
                            float totalHeight = strs.Length * stepY;
                            float totalStartY = centerY - totalHeight / 2;
                            r.Y = totalStartY + stepY * i;
                        }
                        else if (style.LineAlignment == StringAlignment.Far)//靠下
                        {
                            float thisEndY = endY - stepY * i;
                            r.Y = thisEndY - r.Height;
                        }
                        else//靠上
                        {
                            r.Y = startY + stepY * i;
                        }
                    }
                    #endregion

                    #region 绘制
                    if (draw && !r.IsEmpty)
                    {
                        ReportCellStringDrawingArgs e = new ReportCellStringDrawingArgs(g, str, font, brush, r, sf);
                        OnReportCellStringDrawing(e);
                        if (!e.Hand)
                        {
                            //try
                            //{

                            //用这句,有的电脑上会报错  A generic error occurred in GDI+.
                            g.DrawString(str, font, brush, r, sf);

                            //用这句,不报错了,但是放大缩小时,有的文字会不显示,而且参数不一样,估计输出结果可能也不一样
                            //Rectangle r2 = new Rectangle((int)Math.Ceiling(r.Left), (int)Math.Ceiling(r.Top), (int)Math.Ceiling(r.Width), (int)Math.Ceiling(r.Height));
                            //TextRenderer.DrawText(g, str, font, r2, style.FontColor, ff);

                            //}
                            //catch (Exception ex)
                            //{
                            //    I3LocalLogUtil.Current.WriteExceptionLog("drawstring error", ex);
                            //    I3LocalLogUtil.Current.WriteInfoLog(str);
                            //    I3LocalLogUtil.Current.WriteInfoLog(font.ToString());
                            //    I3LocalLogUtil.Current.WriteInfoLog(brush.ToString());
                            //    I3LocalLogUtil.Current.WriteInfoLog(r.ToString());
                            //    I3LocalLogUtil.Current.WriteInfoLog(sf.ToString());
                            //    I3LocalLogUtil.Current.CompleteLog();
                            //}
                        }
                    }
                    #endregion

                    #region 相交区域处理
                    if (rr == RectangleF.Empty)
                    {
                        rr = r;
                    }
                    else
                    {
                        rr.Y = Math.Min(rr.Y, r.Y);                              //y取最小值
                        float yEnd = Math.Max(rr.Y + rr.Height, r.Y + r.Height); //yEnd取最大值
                        rr.Height = yEnd - rr.Y;
                        rr.X      = Math.Min(rr.X, r.X);                         //x取最小值
                        float xEnd = Math.Max(rr.X + rr.Width, r.X + r.Width);   //xEnd取最大值
                        rr.Width = xEnd - rr.X;
                    }
                    #endregion
                }

                return(rr);
            }
            finally
            {
                font.Dispose();
                brush.Dispose();
                sf.Dispose();
            }
        }
Exemplo n.º 19
0
        public override RectangleF DrawContent(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, System.Drawing.RectangleF rect, I3ReportCellStyle style, I3PrintArea area, bool draw)
        {
            I3ReportImageCell imageCell = cell as I3ReportImageCell;

            if (imageCell.ImageData == null)
            {
                return(RectangleF.Empty);
            }

            try
            {
                using (MemoryStream stream = new MemoryStream(imageCell.ImageData))
                {
                    using (Bitmap bitmap = new Bitmap(stream))
                    {
                        float width = imageCell.CalWidth > 0 ? imageCell.CalWidth : imageCell.Width;
                        width *= scale;
                        float heigth = imageCell.CalHeight > 0 ? imageCell.CalHeight : imageCell.Height;
                        heigth *= scale;

                        RectangleF destRect = new RectangleF((float)rect.X + (float)rect.Width / 2F - (float)width / 2F,
                                                             (float)rect.Y + (float)rect.Height / 2F - (float)heigth / 2F,
                                                             width,
                                                             heigth);

                        RectangleF srcRect = new RectangleF(0F, 0F, (float)bitmap.Width, (float)bitmap.Height);
                        if (draw && !destRect.IsEmpty)
                        {
                            g.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
                        }

                        return(destRect);
                    }
                }
            }
            catch
            {
                RectangleF destRect = new RectangleF((float)rect.X + (float)rect.Width / 2F - (float)100 / 2F,
                                                     (float)rect.Y + (float)rect.Height / 2F - (float)100 / 2F,
                                                     100,
                                                     100);
                return(destRect);
            }
        }
Exemplo n.º 20
0
        public virtual void DrawCellBorder(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style)
        {
            I3MergeRange range = cell.GetRange_Mode1(reportData);

            if (range == null)
            {
                return;
            }

            if (cell.Row == range.StartRow)
            {
                #region  边框
                if (style.TopBorder != null)
                {
                    using (Brush brush = new SolidBrush(style.TopBorder.Color))
                    {
                        using (Pen pen = new Pen(brush))
                        {
                            //pen.Width = style.TopBorder.Width * scale;  //缩放不影响边框
                            pen.Width = style.TopBorder.Width;
                            if (pen.Width > 0 && pen.Width < 1)
                            {
                                pen.Width = 1;
                            }
                            pen.Color     = style.TopBorder.Color;
                            pen.DashStyle = style.TopBorder.Type;
                            g.DrawLine(pen, new PointF(rect.X, rect.Y), new PointF(rect.X + rect.Width, rect.Y));
                        }
                    }
                }
                #endregion
            }

            if (cell.Col == range.StartCol)
            {
                #region 左边框
                if (style.LeftBorder != null)
                {
                    using (Brush brush = new SolidBrush(style.LeftBorder.Color))
                    {
                        using (Pen pen = new Pen(brush))
                        {
                            //pen.Width = style.LeftBorder.Width * scale;
                            pen.Width = style.LeftBorder.Width;
                            if (pen.Width > 0 && pen.Width < 1)
                            {
                                pen.Width = 1;
                            }
                            pen.Color     = style.LeftBorder.Color;
                            pen.DashStyle = style.LeftBorder.Type;
                            g.DrawLine(pen, new PointF(rect.X, rect.Y), new PointF(rect.X, rect.Y + rect.Height));
                        }
                    }
                }
                #endregion
            }

            if (cell.Row == range.EndRow)
            {
                #region  边框
                if (style.BottomBorder != null)
                {
                    using (Brush brush = new SolidBrush(style.BottomBorder.Color))
                    {
                        using (Pen pen = new Pen(brush))
                        {
                            //pen.Width = style.BottomBorder.Width * scale;
                            pen.Width = style.BottomBorder.Width;
                            if (pen.Width > 0 && pen.Width < 1)
                            {
                                pen.Width = 1;
                            }
                            pen.Color     = style.BottomBorder.Color;
                            pen.DashStyle = style.BottomBorder.Type;
                            g.DrawLine(pen, new PointF(rect.X, rect.Y + rect.Height), new PointF(rect.X + rect.Width, rect.Y + rect.Height));
                        }
                    }
                }
                #endregion
            }

            //if (cell.Col == range.EndCol)
            //如果是除边框空白列外最右侧的列,强制画右边框线(要求表格4周都有单位1的空白行列)
            if (range.EndCol == reportData.Cols[reportData.Cols.Length - 1].Col - 1)
            {
                #region 右边框
                if (style.RightBorder != null)
                {
                    using (Brush brush = new SolidBrush(style.RightBorder.Color))
                    {
                        using (Pen pen = new Pen(brush))
                        {
                            //pen.Width = style.RightBorder.Width * scale;
                            pen.Width = style.RightBorder.Width;
                            if (pen.Width > 0 && pen.Width < 1)
                            {
                                pen.Width = 1;
                            }
                            pen.Color     = style.RightBorder.Color;
                            pen.DashStyle = style.RightBorder.Type;
                            g.DrawLine(pen, new PointF(rect.X + rect.Width, rect.Y), new PointF(rect.X + rect.Width, rect.Y + rect.Height));
                        }
                    }
                }
                #endregion
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// 将单元格输出到画布上
        /// </summary>
        /// <param name="reportData"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        private static void DrawCell(Graphics g, RectangleF fullRect, RectangleF dataRect, RectangleF areaRect, float scale, I3ReportDatas reportDatas, I3ReportCell cell, I3PrintArea area, I3ReportCell mergedCell,
                                     bool drawBackground, bool drawBorder, bool drawContect)
        {
            #region 合并单元格中的格子处理
            if (cell != null && cell.MergState == I3MergeState.Merged)
            {
                //2017.04.26 为加快绘制速度,合并单元格不处理
                return;
                //I3ReportCell firstCell = reportData.GetMergedStartedCell(cell.Row, cell.Col);
                //if (firstCell != null)
                //{
                //    DrawCell(g, fullRect, dataRect, areaRect, scale, reportData, firstCell, area, cell, drawBackground, drawBorder, drawContect);
                //}
                //return;
            }
            #endregion

            #region 绘制区域、剪切区域
            RectangleF cellDrawRect = CalCellDrawRect_Scale(reportDatas, cell, area, scale, dataRect, mergedCell);
            if (cellDrawRect.IsEmpty)
            {
                return;
            }

            //2017.04.26 为加快绘制速度,不做单元格剪切区域计算,直接使用原始的剪切区域
            //I3ReportCell destCell = mergedCell == null ? cell : mergedCell;  //计算剪切区域使用真实单元格
            //RectangleF clipRect = CalCellClipRect_Scale(reportData, destCell, area, scale, dataRect, fullRect, areaRect);
            ////destCell.SetCellRect(graphicsKey, clipRect);
            //clipRect.Intersect(g.ClipBounds);
            //if (clipRect.IsEmpty)
            //{
            //    return;
            //}

            //g.SetClip(clipRect);
            #endregion

            #region 样式
            I3ReportCellStyle style = area.ReportData.GetCellStyle(cell.StyleName);
            if (style == null)
            {
                return;
            }
            #endregion

            II3CellRenderer renderer = I3CellRendererBuilder.GetRenderer(cell);
            RectangleF      oldClip  = g.ClipBounds;

            //背景
            if (drawBackground)
            {
                g.SetClip(oldClip);
                g.FillRectangle(Brushes.White, cellDrawRect);
                renderer.DrawBackground(g, scale, area.ReportData, cell, cellDrawRect, style);
            }

            //边框
            if (drawBorder)
            {
                RectangleF borderClipRect = oldClip;  //扩大避免边框画不全
                borderClipRect.Inflate(1, 1);
                g.SetClip(borderClipRect);
                renderer.DrawCellBorder(g, scale, area.ReportData, cell, cellDrawRect, style);
            }

            //内容
            if (drawContect)
            {
                g.SetClip(oldClip);
                renderer.DrawContent(g, scale, area.ReportData, cell, cellDrawRect, style, area, true);
            }
        }
Exemplo n.º 22
0
        public virtual void AdjustCellSize(int width, int height, SizeF needSizeF, I3ReportCellStyle style, I3ReportCell cell, I3MergeRange range, I3ReportData reportData, bool prepareNarrow)
        {
            int fontSize = style.FontSize == 0 ? 13 : style.FontSize;  //字体默认值13像素

            if (needSizeF.Width > width || needSizeF.Height > height)
            {
                switch (style.AdjustSize)
                {
                case I3AdjustSize.扩大单元格:
                    #region 扩大单元格
                    if (needSizeF.Width > width)
                    {
                        needSizeF.Width += 4F;     //加大一点
                        float pro = needSizeF.Width / width;
                        for (int i = range.StartCol; i <= range.EndCol; i++)
                        {
                            reportData.Cols[i].Width = (int)(reportData.Cols[i].Width * pro);
                        }
                    }
                    if (needSizeF.Height > height)
                    {
                        float pro = needSizeF.Height / height;
                        for (int i = range.StartRow; i <= range.EndRow; i++)
                        {
                            reportData[i].Height = (int)(reportData[i].Height * pro);
                        }
                    }
                    cell.StringTrimming = StringTrimming.None;
                    #endregion
                    break;

                case I3AdjustSize.缩小内容:
                    #region 缩小内容
                    if (prepareNarrow)
                    {
                        //换行时字号递减1进行测试 ,   不换行时根据宽度
                        if (style.WordWrap)
                        {
                            if (needSizeF.Height > height)
                            {
                                float calFontSize = fontSize;
                                while (calFontSize > 5 && needSizeF.Height > height)
                                {
                                    calFontSize--;
                                    needSizeF = CalCellNeedSize(width, height, style, cell, calFontSize);
                                }
                                cell.CalFontSize    = calFontSize;
                                cell.HasCalFontSize = true;
                                cell.StringTrimming = StringTrimming.None;
                            }
                        }
                        else
                        {
                            if (needSizeF.Width > width)
                            {
                                float pro2        = width / needSizeF.Width;
                                float calFontSize = fontSize * pro2;
                                calFontSize         = calFontSize > 5F ? calFontSize : 5F; //最小5号
                                cell.CalFontSize    = calFontSize;
                                cell.StringTrimming = StringTrimming.None;
                            }
                        }
                    }
                    #endregion
                    break;

                default:
                    #region 都不变
                    cell.StringTrimming = StringTrimming.None;
                    #endregion
                    break;
                }
            }
        }
Exemplo n.º 23
0
        public virtual SizeF CalCellNeedSize(int orgWidth, int orgHeight, I3ReportCellStyle style, I3ReportCell cell)
        {
            float testFontSize = style.FontSize == 0 ? 13 : style.FontSize;  //字体默认值13像素

            return(CalCellNeedSize(orgWidth, orgHeight, style, cell, testFontSize));
        }