通用表格打印1

//
// =================================================
// 通用表格打印
//
// 设计: 陈炎和 2011.03
//==================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Collections;
using System.Data;

namespace cyh.General
{
/// <summary>
/// 通用表格打印
/// 调用时先设置主体,再增加标题和字段
/// </summary>
public class ctlTableReport:PrintDocument,iTableReport
{
internal PrintPreviewDialog previewDialog = new PrintPreviewDialog();

#region 定义属性


/// <summary>
/// 报表主体
/// </summary>
private ReportMain m_rpm = new ReportMain();
/// <summary>
/// 报表标题
/// </summary>
private List<ReportTitle> m_rpt = new List<ReportTitle>();
/// <summary>
/// 报表的字段
/// </summary>
private List<ReportField> m_rpf = new List<ReportField>();
/// <summary>
/// 实际打印时用的报表头
/// </summary>
private ReportTitle[] m_rpt_print ;
/// <summary>
/// 实际打印时用的报表字段
/// </summary>
private ReportField[] m_rpf_print ;
/// <summary>
/// 条码
/// </summary>
private ReportBarcode m_rpb = new ReportBarcode();

/// <summary>
/// 表头坐标
/// </summary>
private Rectangle m_header;
/// <summary>
/// 表格坐标
/// </summary>
private Rectangle m_detail;
/// <summary>
/// 表底坐标
/// </summary>
private Rectangle m_footer;
/// <summary>
/// 打印的边距
/// </summary>
private Margins Margins { get; set; }
/// <summary>
/// 纸张大小
/// </summary>
private PaperSize PaperSize { get; set; }
/// <summary>
/// 条码打印类
/// </summary>
private BarCode128 m_BarCode128 = new BarCode128();
/// <summary>
/// 数据源
/// </summary>
public DataTable DataSource { get; set; }

#endregion

/// <summary>
/// 预留边线的宽度
/// </summary>
private const int c_LineWidth = 3;
private int m_PageIndex,m_PageCount,m_rowindex;


/// <summary>
/// 定义报表主体同时创建报表
/// </summary>
/// <param name="DetailHeight">明细每行高度</param>
/// <param name="DetailRows">每页行数(0:自动计算)</param>
/// <param name="GridLine">表格线的类型 1:三栏表,2:有边框的三栏,4:完全网格</param>
/// <param name="Align">表格对齐方向</param>
/// <param name="Landscape">横向还是纵向打印</param>
/// <param name="Margins">页的边距尺寸</param>
/// <param name="PaperKind">标准的纸张大小(pageSize为空时有效)</param>
/// <param name="pageSize">纸张大小</param>
/// <param name="FromPage">起始页</param>
/// <param name="ToPage">结束页(0:自动到最后一页)</param>
/// <param name="printerName">打印机名(为空时用自动选择)</param>
/// <param name="AutoPageHeight">根据行高度及行数计算纸长,不支持横向打印</param>
/// <param name="Pages">每页的小页数</param>
public void SetReportMain(int DetailHeight, int DetailRows, ReportGridLine GridLine, StringAlignment Align, bool Landscape, System.Drawing.Printing.Margins Margins,
System.Drawing.Printing.PaperKind PaperKind, Size pageSize, int FromPage, int ToPage, string printerName,bool AutoPageHeight,int Pages)
{
if (AutoPageHeight && DetailRows == 0)
throw new Exception("自动页长时每页行数必须大于0");
m_rpm.DetailHeight = DetailHeight;
m_rpm.DetailRows = DetailRows;
m_rpm.Align = Align;
m_rpm.Landscape = Landscape;
m_rpm.Margins = Margins;
m_rpm.PaperKind = PaperKind;
m_rpm.pageSize = pageSize;
m_rpm.BeginPage = FromPage;
m_rpm.EndPage = ToPage;
m_rpm.printerName = printerName;
m_rpm.GridLine = GridLine;
m_rpm.AutoPageHeight = AutoPageHeight;
m_rpm.Pages = Pages<1?1:Pages;
}

public void SetReportMain(int DetailHeight, int DetailRows, ReportGridLine GridLine, Size pageSize, int FromPage, string printerName, int Pages)
{
SetReportMain(DetailHeight, DetailRows,GridLine, StringAlignment.Center, false, new System.Drawing.Printing.Margins(80, 80, 100, 80), System.Drawing.Printing.PaperKind.Custom, pageSize, FromPage, 0, printerName,false, 1);
}
public void SetReportMain(int DetailHeight, ReportGridLine GridLine, Size pageSize, int FromPage, string printerName)
{
SetReportMain(DetailHeight, 0, GridLine, pageSize, FromPage, printerName,1);
}

/// <summary>
/// 定义报表标题
/// </summary>
/// <param name="Caption">标题内容</param>
/// <param name="Header">页头或页脚</param>
/// <param name="Font">字体(宋体加粗四号)</param>
/// <param name="TextAlignment">对齐方式</param>
/// <param name="size"> 标题的高度和宽度,若不定义便检测Caption,否则自换行</param>
public void AddReportTitle(string Caption, bool Header, Font Font, StringAlignment TextAlignment, System.Drawing.Size size)
{
ReportTitle rpt = new ReportTitle();
rpt.Caption = Caption;
rpt.Header = Header;
rpt.TextAlignment = TextAlignment;
rpt.Font = Font == null ? new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))) : Font;
rpt.size = size;
m_rpt.Add(rpt);
}
/// <summary>
/// 定义报表标题
/// </summary>
/// <param name="Caption">标题内容</param>
/// <param name="Header">页头或页脚</param>
public void AddReportTitle(string Caption, bool Header)
{
AddReportTitle(Caption, Header, null, StringAlignment.Near, Size.Empty);
}
/// <summary>
/// 定义报表字段
/// </summary>
/// <param name="Caption">表格头标题</param>
/// <param name="HeaderFont">标题字体(宋体、小四)</param>
/// <param name="Field">绑定字段</param>
/// <param name="TextAlignment">对齐方式</param>
/// <param name="DetailFont"> 表格字体(宋体、小四)</param>
/// <param name="WordWrap">自动换行</param>
/// <param name="Bottom">表格脚内容</param>
/// <param name="Sum">是否合计</param>
/// <param name="FormatString">格式化字符串</param>
/// <param name="HeaderSize">标题的高度和宽度,若不定义便检测Caption,否则自换行并居中</param>
public void AddReportField(string Caption, Font HeaderFont, string Field, StringAlignment TextAlignment, Font DetailFont
, bool WordWrap, string Bottom, bool Sum, string FormatString, Size HeaderSize)
{
ReportField rpf = new ReportField();
rpf.Caption = Caption;
rpf.HeaderFont = HeaderFont;
rpf.Field = Field;
rpf.TextAlignment = TextAlignment;
rpf.DetailFont = DetailFont;
rpf.WordWrap = WordWrap;
rpf.Bottom = Bottom;
rpf.Sum = Sum;
rpf.FormatString = FormatString;
rpf.HeaderSize = HeaderSize;
m_rpf.Add(rpf);
}
/// <summary>
/// 定义报表字段
/// </summary>
/// <param name="Caption">表格头标题</param>
/// <param name="Field">绑定字段</param>
public void AddReportField(string Caption,string Filed)
{
AddReportField(Caption,null,Filed, StringAlignment.Near, null, false, "", false, string.Empty, Size.Empty);
}
/// <summary>
/// 定义条码
/// </summary>
/// <param name="Text">条码内容(A-Z,0-9)</param>
/// <param name="pint">起点</param>
/// <param name="size">大小</param>
/// <param name="showText">是否显示文字</param>
/// <param name="Field">字段名</param>
public void SetReportBarCode(string Text, Point pint, Size size, bool showText,string Field)
{
m_rpb.Text = Text;
m_rpb.pint = pint;
m_rpb.size =size;
m_rpb.showText = showText;
m_rpb.Field = Field;
}
/// <summary>
/// 报表每个单元打印前事件
/// </summary>
public event ReportUnitAfterPrint UnitAfterPrint;

public ctlTableReport()
{
previewDialog.Document = this;
}
/// <summary>
/// 显示打印
/// </summary>
/// <returns></returns>
public void ShowDialog()
{
previewDialog.ShowDialog();
}
/// <summary>
/// 显示打印
/// </summary>
/// <param name="owner"></param>
/// <returns></returns>
public void ShowDialog(IWin32Window owner)
{
previewDialog.ShowDialog(owner);
}

/// <summary>
/// 自动页长时计算每页打印的行数
/// </summary>
/// <param name="rows">总行数</param>
/// <param name="maxrow">充许每页打印最大行数</param>
/// <returns></returns>
public int GetAutoRow(int rows, int maxrow)
{
if (rows % maxrow == 0)
return maxrow;
else
{
//找余数为0或最接近最大行数
List<int> m = new List<int>();
for (int i = maxrow; i >=maxrow * 3 / 4; i--)
{
m.Add(rows % i);
if (rows % i == 0) return i;
}
int k = m.AsEnumerable().Max();
for (int i = 0; i < m.Count(); i++)
{
if (m[i] == k) return maxrow - i;//只能最接近了
}
return maxrow;
}
}

protected override void OnBeginPrint(PrintEventArgs e)
{
m_rpt_print = m_rpt.ToArray();
m_rpf_print = m_rpf.ToArray();
m_PageIndex = 0;
m_rowindex = 0;
//设置页面
if (m_rpm.Margins != null)
{
this.Margins = m_rpm.Margins;
this.DefaultPageSettings.Margins.Left = this.Margins.Left * 100 / 254; this.DefaultPageSettings.Margins.Right = this.Margins.Right * 100 / 254; this.DefaultPageSettings.Margins.Top = this.Margins.Top * 100 / 254; this.DefaultPageSettings.Margins.Bottom = this.Margins.Bottom * 100 / 254;
}
else
this.Margins = new Margins((int)(base.DefaultPageSettings.Margins.Left * 2.54F), (int)(base.DefaultPageSettings.Margins.Right * 2.54F), (int)(base.DefaultPageSettings.Margins.Top * 2.54F), (int)(base.DefaultPageSettings.Margins.Bottom * 2.54F));
if (!m_rpm.pageSize.IsEmpty)
{
this.PaperSize = new PaperSize("x", m_rpm.pageSize.Width, m_rpm.pageSize.Height);
base.DefaultPageSettings.PaperSize = new PaperSize("x", m_rpm.pageSize.Width * 100 / 254, m_rpm.pageSize.Height * 100 / 254);
}
else
{
foreach (PaperSize ps in base.DefaultPageSettings.PrinterSettings.PaperSizes)
if (ps.Kind == m_rpm.PaperKind) base.DefaultPageSettings.PaperSize = ps;
this.PaperSize = new PaperSize("x", (int)(base.DefaultPageSettings.PaperSize.Width * 2.54F),(int)(base.DefaultPageSettings.PaperSize.Height * 2.54F));
}
base.DefaultPageSettings.Landscape = m_rpm.Landscape;
if (!string.IsNullOrEmpty(m_rpm.printerName))
base.PrinterSettings.PrinterName = m_rpm.printerName;

Graphics g = this.PrinterSettings.CreateMeasurementGraphics();
InitPrint(g, ref m_rpt_print, ref m_rpf_print);
//计算总页数
m_PageCount = this.DataSource.Rows.Count / m_rpm.DetailRows;
if (this.DataSource.Rows.Count % m_rpm.DetailRows != 0) m_PageCount++;//总行数 = m_PageCount * m_rpm.DetailRows

base.OnBeginPrint(e);
}
//打印首页时初始化打印设置
private void InitPrint(Graphics g,ref ReportTitle[] rpt,ref ReportField[] rpf)
{
m_header = new Rectangle();
m_header.Y = this.Margins.Top;
int pageFooderHeight = 0;
bool tableFooter = false;//是否要打印表底
g.PageUnit = GraphicsUnit.Millimeter;
for (int i = 0; i < rpt.Length; i++)
{
if (rpt[i].TextAlignment == 0) rpt[i].TextAlignment = StringAlignment.Near;
if (rpt[i].Font == null) rpt[i].Font = new System.Drawing.Font("宋体", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
if (rpt[i].size.IsEmpty)
rpt[i].size = new Size(this.PaperSize.Width / m_rpm.Pages - this.Margins.Left - this.Margins.Right, (int)(g.MeasureString(rpt[i].Caption, rpt[i].Font).Height * 10) + 2);
if (rpt[i].Header) m_header.Y += rpt[i].size.Height;//表格起点
else pageFooderHeight += rpt[i].size.Height;
}
m_header.Y += c_LineWidth;
for (int i = 0; i < rpf.Length; i++)
{
if (rpf[i].TextAlignment == 0) rpf[i].TextAlignment = StringAlignment.Near;
if (rpf[i].HeaderFont == null) rpf[i].HeaderFont = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
if (rpf[i].DetailFont == null) rpf[i].DetailFont = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
if (rpf[i].HeaderSize.IsEmpty)
{
SizeF size = g.MeasureString(rpf[i].Caption, rpf[i].HeaderFont);
size.Height = size.Height < m_rpm.DetailHeight /10F ? m_rpm.DetailHeight/10F : size.Height * 1.1F;
rpf[i].HeaderSize = new Size((int)(size.Width * 10) + (i == 0 ? 3 : 2) * c_LineWidth, (int)(size.Height * 10)); //宽度增加2个线位,防止自动换行
}
m_header.Height = m_header.Height < rpf[i].HeaderSize.Height ? rpf[i].HeaderSize.Height : m_header.Height;
m_header.Width += rpf[i].HeaderSize.Width; //表格宽度
if (!string.IsNullOrEmpty(rpf[i].Bottom) || rpf[i].Sum) tableFooter = true;
}
switch (m_rpm.Align)
{
case StringAlignment.Center:
m_header.X = (this.PaperSize.Width/m_rpm.Pages - this.Margins.Left - this.Margins.Right - m_header.Width) / 2;
break;
case StringAlignment.Far:
m_header.X = this.PaperSize.Width/m_rpm.Pages - this.Margins.Left - this.Margins.Right - m_header.Width - c_LineWidth;
break;
default:
m_header.X = this.Margins.Left ;
break;
}
// 检测行数和行高
int h = m_header.Height + m_header.Y + 2 * c_LineWidth + (tableFooter ? m_rpm.DetailHeight + c_LineWidth : 0)+pageFooderHeight+2*c_LineWidth;
//自动页长=页头+页脚+每页行数 * 每行高度
if (m_rpm.AutoPageHeight && m_rpm.DetailRows >0 )
{
int pH = h + m_rpm.DetailRows * m_rpm.DetailHeight + this.Margins.Bottom;
this.PaperSize = new PaperSize("x", this.PaperSize.Width, pH);
base.DefaultPageSettings.PaperSize = new PaperSize("x", this.PaperSize.Width * 100 / 254, this.PaperSize.Height * 100 / 254);
}

h = this.PaperSize.Height - this.Margins.Bottom - h;
if (m_rpm.DetailRows == 0) //计算扣除页头页脚及表头表底后行,计算每页行数
{
m_rpm.DetailRows = h / m_rpm.DetailHeight;
if (m_rpm.DetailRows <= 0) throw new Exception("PageHeight不够大,请重新定义纸张大小,或减少DetailHeight");
}
if (m_rpm.DetailRows * m_rpm.DetailHeight > h) throw new Exception("PageHeight不够大,请重新定义纸张大小,或减少DetailRows");

m_detail = new Rectangle(m_header.X, m_header.Y + m_header.Height + c_LineWidth, m_header.Width, m_rpm.DetailHeight * m_rpm.DetailRows);
if (tableFooter) m_footer = new Rectangle(m_detail.X, m_detail.Y + m_detail.Height + c_LineWidth, m_detail.Width, m_rpm.DetailHeight);
else m_footer = new Rectangle();

}

protected override void OnPrintPage(PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
if (m_rpm.BeginPage > 0 && m_PageIndex < m_rpm.BeginPage - 1) m_PageIndex = m_rpm.BeginPage - 1;
if (m_PageIndex >= m_PageCount)
{
base.OnPrintPage(e);
return;
}
int xMove = 0; //每页左移距离
for (int p = 1; p <= m_rpm.Pages ; p++)
{
xMove = (this.PaperSize.Width / m_rpm.Pages) * (p - 1);
CreateBarCode(m_rpb, e.Graphics,xMove);
CreateTitleHeader(m_rpt_print, e.Graphics, this.Margins.Left + xMove , this.Margins.Top, true);
CreateTableHeader(m_rpf_print, e.Graphics, m_header.X + xMove , m_header.Y);
m_rowindex = m_PageIndex * m_rpm.DetailRows;
DataRow drSum = this.DataSource.NewRow(); //用于本页合计
for (int i = 0; i < m_rpm.DetailRows; i++)
{
CreateTableDetail(m_rpf_print, e.Graphics, m_detail.X+xMove, m_detail.Y + i * m_rpm.DetailHeight, this.DataSource.Rows[m_rowindex], m_rpm.DetailHeight, drSum);
m_rowindex++;
if (m_rowindex == this.DataSource.Rows.Count) break;
}
//表底
if (!m_footer.IsEmpty)
{
CreateTableFooder(m_rpf_print, e.Graphics, m_footer.X+xMove, m_footer.Y, drSum, m_rpm.DetailHeight);
CreateTitleHeader(m_rpt_print, e.Graphics, Margins.Left+xMove, m_footer.Y + m_footer.Height + c_LineWidth, false);
}
else
CreateTitleHeader(m_rpt_print, e.Graphics, Margins.Left+xMove, m_detail.Y + m_detail.Height + c_LineWidth, false);
//画线
CreateLine(e.Graphics,xMove);
m_PageIndex++;
if ((m_PageIndex < m_PageCount) && !(m_rpm.EndPage > 0 && m_PageIndex >= m_rpm.EndPage))
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
break;
}
}
base.OnPrintPage(e);
}

/// <summary>
///打印表底
/// </summary>
private void CreateTableFooder(ReportField[] rpf, Graphics g, int x,int y , DataRow dr, int rowHeight)
{
foreach (ReportField obj in rpf)
{
string value = obj.Bottom;
if (obj.Sum) //合计
if (!string.IsNullOrEmpty(obj.Field))
{
if (dr[obj.Field] != DBNull.Value && !string.IsNullOrEmpty(obj.FormatString))
value = string.Format(obj.FormatString, dr[obj.Field]);
else if (dr[obj.Field] != DBNull.Value)
value = dr[obj.Field].ToString();
else value = string.Empty;
}
if (UnitAfterPrint != null) UnitAfterPrint(this,ReportBandType.TableFooter, m_PageIndex, m_PageIndex * m_rpm.DetailRows, obj.Caption, obj.Field, ref value, new EventArgs());
StringFormat format1 = new StringFormat();
format1.Alignment = obj.TextAlignment;
if (!obj.WordWrap) format1.FormatFlags = StringFormatFlags.NoWrap;
g.DrawString(value, obj.HeaderFont, Brushes.Black, new RectangleF(x/10F, y/10F, obj.HeaderSize.Width/10F, rowHeight/10), format1);
x = x + obj.HeaderSize.Width + c_LineWidth;
}
return ;

}
/// <summary>
///打印一行(按行的高度不留线宽)
/// </summary>
private void CreateTableDetail(ReportField[] rpf, Graphics g, int x,int y, DataRow dr,int rowHeight,DataRow Sum)
{
foreach (ReportField obj in rpf)
{
string value = obj.Caption;
if (! string.IsNullOrEmpty(obj.Field))
{
if (dr[obj.Field] != DBNull.Value && !string.IsNullOrEmpty(obj.FormatString))
value = string.Format(obj.FormatString, dr[obj.Field]);
else if (dr[obj.Field] != DBNull.Value)
value = dr[obj.Field].ToString();
else value = string.Empty;
if (obj.Sum) //合计
{
double d = Sum[obj.Field] == DBNull.Value ? 0 : Convert.ToDouble(Sum[obj.Field]);
Sum[obj.Field] = d +(dr[obj.Field] == DBNull.Value ? 0 : Convert.ToDouble(dr[obj.Field])); //数据类型??
}
}
if (UnitAfterPrint != null) UnitAfterPrint(this,ReportBandType.TableDetail, m_PageIndex, m_rowindex, obj.Caption, obj.Field, ref value, new EventArgs());
StringFormat format1 = new StringFormat();
format1.Alignment = obj.TextAlignment;
if (!obj.WordWrap) format1.FormatFlags = StringFormatFlags.NoWrap ;
g.DrawString(value, obj.HeaderFont, Brushes.Black, new RectangleF(x/10F, y/10F, obj.HeaderSize.Width/10F, rowHeight/10F), format1);
x+=obj.HeaderSize.Width;
}
return ;
}

/// <summary>
///打印眉栏(返回眉栏高度)
/// </summary>
private void CreateTableHeader(ReportField[] rpf, Graphics g, int x,int y)
{
foreach (ReportField obj in rpf)
{
string value = obj.Caption ;
if (UnitAfterPrint != null) UnitAfterPrint(this , ReportBandType.TableHeader, m_PageIndex, m_PageIndex * m_rpm.DetailRows, obj.Caption,obj.Field, ref value, new EventArgs());
StringFormat format1 = new StringFormat();
format1.Alignment = obj.TextAlignment;
//format1.FormatFlags = StringFormatFlags.NoWrap;
g.DrawString(value, obj.HeaderFont, Brushes.Black, new RectangleF(x / 10F, y / 10F, obj.HeaderSize.Width / 10F, obj.HeaderSize.Height / 10F),format1);
x += obj.HeaderSize.Width;
}
return ;
}


/// <summary>
/// 打印表题
/// </summary>
private void CreateTitleHeader(ReportTitle[] rpt, Graphics g, int x,int y,bool Header)
{
int height = 0;
foreach (ReportTitle obj in rpt)
{
string value = obj.Caption ;
if (obj.Header == Header)
{
if (UnitAfterPrint != null) UnitAfterPrint(this, obj.Header ? ReportBandType.PageHeader :ReportBandType.PageFooder, m_PageIndex,m_PageIndex * m_rpm.DetailRows, obj.Caption, "", ref value, new EventArgs());
StringFormat format1 = new StringFormat();
format1.Alignment = obj.TextAlignment;
format1.FormatFlags = StringFormatFlags.NoWrap;
g.DrawString(value, obj.Font, Brushes.Black, new RectangleF(x/10F, y/10F+height/10F, obj.size.Width/10F, obj.size.Height/10F), format1);
height =height + obj.size.Height;
}
}
return ;
}

private void CreateLine(Graphics g,int xMove)
{
if (m_rpm.GridLine == ReportGridLine.Nothing) return;
Pen pen = new Pen(Brushes.Black,c_LineWidth/10F);
//水平线
CreateLineH(g,pen,m_header.X+xMove,m_header.Y,m_header.Width);
for (int i = 0; i < m_rpm.DetailRows; i++)
{
if (m_rpm.GridLine==ReportGridLine.Full|| i==0 ) CreateLineH(g, pen, m_detail.X+xMove, m_detail.Y + m_rpm.DetailHeight * i, m_detail.Width);
}
CreateLineH(g, pen, m_detail.X+xMove, m_detail.Y+m_detail.Height, m_detail.Width);
if (!m_footer.IsEmpty && m_rpm.GridLine==ReportGridLine.Full)
{
CreateLineH(g, pen, m_footer.X+xMove, m_footer.Y + m_footer.Height + c_LineWidth, m_footer.Width);
}
//垂直
if (m_rpm.GridLine == ReportGridLine.Three) return;
int x = m_header.X+xMove;
int h = m_detail.Height + m_header.Height + c_LineWidth + (m_rpm.GridLine==ReportGridLine.Full? m_footer.Height:0);
CreateLineV(g, pen, x, m_header.Y, h);
foreach (ReportField obj in m_rpf_print)
{
x += obj.HeaderSize.Width;
if (m_rpm.GridLine==ReportGridLine.Full || obj.Equals(m_rpf_print[m_rpf_print.Length-1])) CreateLineV(g, pen, x, m_header.Y, h);
}
}

//垂直(向上移一个线宽位)
private void CreateLineV(Graphics g, Pen p, int x, int y, int h)
{
g.DrawLine(p, x / 10F, (y-c_LineWidth) / 10F, x / 10F, (y + h) / 10F);
}

//水平(向上移一个线宽位)
private void CreateLineH(Graphics g, Pen p, int x, int y, int w)
{
g.DrawLine(p, (x-c_LineWidth/2) / 10F, (y-c_LineWidth) / 10F, (x+w + c_LineWidth/2) / 10F, (y-c_LineWidth) / 10F);
}

/// <summary>
/// 绘制条码
/// </summary>
private void CreateBarCode(ReportBarcode rpb, Graphics g,int xMove)
{
if ( string.IsNullOrEmpty(rpb.Text) && string.IsNullOrEmpty( rpb.Field)) return;
string value = rpb.Text;
if (!string.IsNullOrEmpty(rpb.Field))
{
DataRow dr = DataSource.Rows[m_PageIndex * m_rpm.DetailRows];
if (dr[rpb.Field] != DBNull.Value) value = dr[rpb.Field].ToString();
else value = string.Empty;
}
if (UnitAfterPrint != null) UnitAfterPrint(this,ReportBandType.BarCode, m_PageIndex, m_PageIndex * m_rpm.DetailRows, rpb.Text,rpb.Field, ref value, new EventArgs());
if (string.IsNullOrEmpty(value)) return;
int f = 1;//每0.1毫米显示像素
Image img = m_BarCode128.EncodeBarcode(value, rpb.size.Width * f , rpb.size.Height * f, rpb.showText);
g.DrawImage(img, rpb.pint.X / 10F + (this.Margins.Left+xMove) / 10F, rpb.pint.Y / 10F + this.Margins.Top / 10F, rpb.size.Width / 10F, rpb.size.Height / 10F);
}
}
}

posted @ 2012-10-28 09:47  lhj588  阅读(435)  评论(0编辑  收藏  举报