/// <summary> /// 指定したマージンでテキストを描画する /// </summary> /// <param name="g">グラフィックス</param> /// <param name="text">描画するテキスト</param> /// <param name="font">描画するフォント</param> /// <param name="brush">色など</param> /// <param name="left">左位置</param> /// <param name="right">右位置</param> /// <param name="top">上位置</param> /// <param name="height">高さ</param> /// <param name="vmargin">配置(横)</param> /// <param name="hmargin">配置(縦)</param> protected virtual void DrawString(Graphics g, string text, Font font, Brush brush, float left, float right, float top, float height, PrintMargin vmargin, PrintMargin hmargin) { System.Drawing.SizeF size = g.MeasureString(text, font); float newTop = top; switch (hmargin) { case PrintMargin.MARGIN_CENTER: newTop += (height - size.Height) / 2; break; case PrintMargin.MARGIN_BOTTOM: newTop += (height - size.Height); break; } DrawString(g, text, font, brush, left, right, newTop, vmargin); }
private void dealWithPageInfor() { int Resolution = showReport.getResolution(); PaperSize localPaperSize = this.reportSettings.PaperSize; PrintMargin localMargin = this.reportSettings.Margin; double d1, d2; if (this.reportSettings.Orientation == PrintOrientation.Portrait) { d1 = Math.Min(localPaperSize.Width, localPaperSize.Height); d2 = Math.Max(localPaperSize.Width, localPaperSize.Height); } else { d1 = Math.Max(localPaperSize.Width, localPaperSize.Height); d2 = Math.Min(localPaperSize.Width, localPaperSize.Height); } int InnerWidth = (int)((d1 - localMargin.Left - localMargin.Right) * Resolution / 100.0); int InnerHeight = (int)((d2 - localMargin.Top - localMargin.Bottom - localMargin.Header - localMargin.Footer) * Resolution / 100.0); int ColumnCount = this.showReport.ToWorkSheet().GetLastNonEmptyColumn(NonEmptyItemFlag.Data) + 1; int RowCount = this.showReport.ToWorkSheet().GetLastNonEmptyRow(NonEmptyItemFlag.Data) + 1; SheetView report = this.showReport.ToWorkSheet(); int cCount = report.Columns.Count; int rCount = report.Rows.Count; for (int i = 0; i < rCount; i++) { for (int j = 0; j < cCount; j++) { if (report.Cells[i, j].Tag is GridElement) { GridElement Element = report.Cells[i, j].Tag as GridElement; ColumnCount = Math.Max(ColumnCount, report.Cells[i, j].Column.Index + report.Cells[i, j].ColumnSpan); RowCount = Math.Max(RowCount, report.Cells[i, j].Row.Index + report.Cells[i, j].RowSpan); } } } List <int> rowPageBreaks_Object = new List <int>(); List <int> columnPageBreaks_Object = new List <int>(); for (int i = 0; i < RowCount; i++) { for (int j = 0; j < ColumnCount; j++) { if (report.Cells[i, j].Tag is GridElement) { GridElement Element = report.Cells[i, j].Tag as GridElement; if (Element.PageBreak.IsAfterRow) { rowPageBreaks_Object.Add(report.Cells[i, j].Row.Index + report.Cells[i, j].RowSpan); } } } } PrintInfo printInfo = this.showReport.ToWorkSheet().PrintInfo; if (printInfo == null) { this.pageColumnIndexList.Add(new IdxHeaderFooter(0)); int totalWidth = 0; int columnWidth = 0; for (int i = 0; i < ColumnCount; i++) { totalWidth += (columnWidth = this.columnWidthList.get(i)); if (totalWidth <= InnerWidth && !columnPageBreaks_Object.Contains(i)) { continue; } if (((IdxHeaderFooter)this.pageColumnIndexList[this.pageColumnIndexList.Count - 1]).getIndex() == i) { throw new DeathCycleException("分页处理死循环出现在列:" + i); } this.pageColumnIndexList.Add(new IdxHeaderFooter(i)); totalWidth = columnWidth; } this.pageColumnIndexList.Add(new IdxHeaderFooter(ColumnCount)); this.pageRowIndexList.Add(new IdxHeaderFooter(0)); totalWidth = 0; columnWidth = 0; for (int i = 0; i < RowCount; i++) { totalWidth += (columnWidth = this.rowHeightList.get(i)); if (totalWidth <= InnerHeight && !rowPageBreaks_Object.Contains(i)) { continue; } if (((IdxHeaderFooter)this.pageRowIndexList[this.pageRowIndexList.Count - 1]).getIndex() == i) { throw new DeathCycleException("分页处理死循环出现在行:" + i); } this.pageRowIndexList.Add(new IdxHeaderFooter(i)); totalWidth = columnWidth; } this.pageRowIndexList.Add(new IdxHeaderFooter(RowCount)); } else { List <int> headerRepeatCols_Object = new List <int>(); List <int> footerRepeatCols_Object = new List <int>(); int totalWidth = 0; List <int> footerRepeatCols = null; for (int i = printInfo.ColStart; i <= printInfo.ColEnd; i++) { for (int j = 0; j < ColumnCount; j++) { if (this.showReport.getColumnOIIntList()[j] != i) { continue; } totalWidth += this.columnWidthList.get(j); if (footerRepeatCols == null) { footerRepeatCols = new List <int>(); } footerRepeatCols.Add(j); footerRepeatCols_Object.Add(i); break; } } this.pageColumnIndexList.Add(new IdxHeaderFooter(0, null, footerRepeatCols)); if (totalWidth > InnerWidth) { throw new ApplicationException("重复列宽超过了页面宽度。"); } for (int i = 0; i < ColumnCount; i++) { if (footerRepeatCols_Object.Contains(this.showReport.getColumnOIIntList()[i]) && !columnPageBreaks_Object.Contains(i)) { List <int> footerRepeatList = ((IdxHeaderFooter)this.pageColumnIndexList[this.pageColumnIndexList.Count - 1]).getFooterRepeatList(); if (footerRepeatList == null) { continue; } int Index = footerRepeatList.IndexOf(i); if (Index == -1) { continue; } footerRepeatList.Remove(Index); } else { totalWidth += this.columnWidthList.get(i); if ((totalWidth <= InnerWidth) && !columnPageBreaks_Object.Contains(i)) { continue; } if (((IdxHeaderFooter)this.pageColumnIndexList[this.pageColumnIndexList.Count - 1]).getIndex() == i) { throw new DeathCycleException("分页处理死循环出现在列:" + i); } headerRepeatCols_Object.Clear(); footerRepeatCols_Object.Clear(); totalWidth = 0; int columnPageBreakIndex = i; List <int> headerRepeatList = null; List <int> footerRepeatList = null; while ((i < ColumnCount) && printInfo.RepeatColStart <= this.showReport.getColumnOIIntList()[i] && this.showReport.getColumnOIIntList()[i] <= printInfo.RepeatColEnd) { totalWidth += this.columnWidthList.get(i); headerRepeatCols_Object.Add(this.showReport.getColumnOIIntList()[i]); i++; } if (i >= ColumnCount) { if (totalWidth > InnerWidth) { throw new ApplicationException("重复列宽超过了页面宽度。"); } this.pageColumnIndexList.Add(new IdxHeaderFooter(columnPageBreakIndex, headerRepeatList, footerRepeatList)); break; } for (int k = printInfo.RepeatColStart; k <= printInfo.RepeatColEnd; k++) { for (int l = columnPageBreakIndex - 1; l >= 0; l--) { if (this.showReport.getColumnOIIntList()[l] != k) { continue; } if (headerRepeatCols_Object.Contains(k)) { break; } totalWidth += this.columnWidthList.get(l); if (headerRepeatList == null) { headerRepeatList = new List <int>(); } headerRepeatList.Add(l); break; } } for (int k = printInfo.ColStart; k <= printInfo.ColEnd; k++) { for (int l = i + 1; l < ColumnCount; l++) { if (this.showReport.getColumnOIIntList()[l] != k) { continue; } totalWidth += this.columnWidthList.get(l); if (footerRepeatList == null) { footerRepeatList = new List <int>(); } footerRepeatList.Add(l); footerRepeatCols_Object.Add(k); break; } } this.pageColumnIndexList.Add(new IdxHeaderFooter(columnPageBreakIndex, headerRepeatList, footerRepeatList)); totalWidth += this.columnWidthList.get(i); if (totalWidth <= InnerWidth) { continue; } throw new ApplicationException("重复列宽超过了页面宽度。"); } } if (this.pageColumnIndexList.Count == 0 || ((IdxHeaderFooter)this.pageColumnIndexList[this.pageColumnIndexList.Count - 1]).getIndex() < ColumnCount) { this.pageColumnIndexList.Add(new IdxHeaderFooter(ColumnCount)); } List <int> headerRepeatRows_Object = new List <int>(); List <int> footerRepeatRows_Object = new List <int>(); int totalHeight = 0; List <int> footerRepeatRows = null; for (int i = printInfo.RowStart; i <= printInfo.RowEnd; i++) { for (int j = 0; j < RowCount; j++) { if (this.showReport.getRowOIIntList()[j] != i) { continue; } totalHeight += this.rowHeightList.get(j); if (footerRepeatRows == null) { footerRepeatRows = new List <int>(); } footerRepeatRows.Add(j); footerRepeatRows_Object.Add(i); break; } } if (totalHeight > InnerHeight) { throw new ApplicationException("重复行高超过了页面高度。"); } this.pageRowIndexList.Add(new IdxHeaderFooter(0, null, footerRepeatCols)); for (int i = 0; i < RowCount; i++) { if ((((List <int>)footerRepeatRows_Object).Contains(this.showReport.getRowOIIntList()[i])) && (!((List <int>)rowPageBreaks_Object).Contains(i))) { List <int> footerRepeatList = ((IdxHeaderFooter)this.pageRowIndexList[this.pageRowIndexList.Count - 1]).getFooterRepeatList(); if (footerRepeatList == null) { continue; } int Index = footerRepeatList.IndexOf(i); if (Index == -1) { continue; } footerRepeatList.Remove(Index); } else { totalHeight += this.rowHeightList.get(i); if ((totalHeight <= InnerHeight) && (!((List <int>)rowPageBreaks_Object).Contains(i))) { continue; } if (((IdxHeaderFooter)this.pageRowIndexList[this.pageRowIndexList.Count - 1]).getIndex() == i) { throw new DeathCycleException("分页处理死循环出现在行:" + i); } headerRepeatRows_Object.Clear(); footerRepeatRows_Object.Clear(); totalHeight = 0; int rowPageBreakIndex = i; List <int> headerRepeatList = null; List <int> footerRepeatList = null; while ((i < RowCount) && printInfo.RepeatRowStart <= this.showReport.getRowOIIntList()[i] && (this.showReport.getRowOIIntList()[i] <= printInfo.RepeatRowEnd)) { totalHeight += this.rowHeightList.get(i); headerRepeatRows_Object.Add(this.showReport.getRowOIIntList()[i]); i++; } if (i >= RowCount) { if (totalHeight > InnerHeight) { throw new ApplicationException("重复的行高超过了页面高度。"); } this.pageRowIndexList.Add(new IdxHeaderFooter(rowPageBreakIndex, headerRepeatList, footerRepeatList)); break; } for (int k = ((PrintInfo)printInfo).RepeatRowStart; k <= ((PrintInfo)printInfo).RepeatRowEnd; k++) { for (int l = rowPageBreakIndex - 1; l >= 0; l--) { if (this.showReport.getRowOIIntList()[l] != k) { continue; } if (headerRepeatRows_Object.Contains(k)) { break; } totalHeight += this.rowHeightList.get(l); if (headerRepeatList == null) { headerRepeatList = new List <int>(); } headerRepeatList.Add(l); break; } } for (int k = printInfo.RowStart; k <= printInfo.RowEnd; k++) { for (int l = i + 1; l < RowCount; l++) { if (this.showReport.getRowOIIntList()[l] != k) { continue; } totalHeight += this.rowHeightList.get(l); if (footerRepeatList == null) { footerRepeatList = new List <int>(); } footerRepeatList.Add(l); footerRepeatRows_Object.Add(k); break; } } this.pageRowIndexList.Add(new IdxHeaderFooter(rowPageBreakIndex, headerRepeatList, footerRepeatList)); totalHeight += this.rowHeightList.get(i); if (totalHeight <= InnerHeight) { continue; } throw new ApplicationException("重复的行高超过了页面高度。"); } } } if (this.pageRowIndexList.Count == 0 || ((IdxHeaderFooter)this.pageRowIndexList[this.pageRowIndexList.Count - 1]).getIndex() < RowCount) { this.pageRowIndexList.Add(new IdxHeaderFooter(RowCount)); } this.pageCount = ((this.pageColumnIndexList.Count - 1) * (this.pageRowIndexList.Count - 1)); }
/// <summary> /// 指定したマージンでテキストを描画する /// </summary> /// <param name="g">グラフィックス</param> /// <param name="text">描画するテキスト</param> /// <param name="font">描画するフォント</param> /// <param name="brush">色など</param> /// <param name="left">左位置</param> /// <param name="right">右位置</param> /// <param name="top">上位置</param> /// <param name="margin">配置</param> protected virtual void DrawString(Graphics g, string text, Font font, Brush brush, float left, float right, float top, PrintMargin margin) { System.Drawing.SizeF size = g.MeasureString(text, font); float TextWidth = size.Width; if (TextWidth > right - left) { // 長すぎるので縮めて再実行 DrawString(g, text.Substring(0, text.Length - 1), font, brush, left, right, top, margin); } else { // float newleft = left; switch (margin) { case PrintMargin.MARGIN_LEFT: break; case PrintMargin.MARGIN_CENTER: newleft += ((right - left) - size.Width) / 2; break; case PrintMargin.MARGIN_RIGHT: newleft += (right - left) - size.Width; break; } g.DrawString(text, font, brush, newleft, top); } }