在webform下的打印很容易,可是到了winform下就不会了,这里不能画表格啊,我想做一个打印证件的系统,在窗体上的单元格已经布置好了,可是,该怎么打印到证件上的表格里呢,哎,都闷了一周了还是没有解决,分是少点,以后有分了再给大家补,今天的问题必须解决啊,拜托大家了

解决方案 »

  1.   

    自已写的一个打印类,给你参考一下:using System;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Data;
    using System.Windows.Forms;namespace     CSharpLearn
    {
        public class   TablePrint
    {

    #region Statics and Constants  
    private const  float topMargin=40;
    //private const  float leftMargin=40;    //改为动态确定leftMargin的大小 变为instance Member
    private const  float bottomMargin=80;   //显示提示信息空白区:总页数和当前第几页
    private const  float CellPad=2;           //每个单元格衬底尺寸
    private readonly Font font = new Font("宋体", 10);//正文字体
    private readonly Brush brush = new  SolidBrush(Color.Black);//画刷
    #endregion # region    Struct
    struct Column                      //Private(defalult)
    {
    public string ColumnName;
    public float  LeftLinePosition;       //每一列左线的起始置
    public float  ColumnWidth;
    }
    #endregion  #region Member variables
    Column [] columns;
    float rowHeight;
    private DataTable    dataTable;
    private   float leftMargin;
    private float footerleftPosition;
    private float footertopPosition;
    private int   maxRowNumPerPage;    //包括header,因为,header也占一行.不包括footer
    private PrintDocument printDocument;
    private PageSetupDialog pageSetupDialog;
    private PrintPreviewDialog printPreviewDialog;
    private  int totalPageCount;               //////////////////以下是每页的可变部分的坐标
    private  int currentPageIndex=0;
    private  int currentContentRowNum;          //当前行,真正的数据行指针
    private bool hasBorders=true; //设置是否添加边框,默认为带边框
    #endregion #region  Constructor

    public TablePrint(DataTable dt)
    {            this.dataTable =dt;
    printDocument=new PrintDocument ();
    printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); columns= new  Column[this.dataTable .Columns .Count];
    this.rowHeight =font.Height+2*CellPad;
        

    // pageRowCount=(int)((height-this.topMargin-titleSize-this.headerFont.Height-this.headerHeight-this.buttomMargin)/this.rowGap);

    } #endregion
    #region  Public  Method
    private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    //if语句中的语句为初始化部分,第一次调用该过程时执行,以后不再改变               
    if(this.currentPageIndex ==0)
    {
    int width=e.PageBounds.Width;
    int height=e.PageBounds.Height;
    this.footerleftPosition=400;
    this.footertopPosition=height-bottomMargin+10*CellPad;
    this.maxRowNumPerPage=Convert.ToInt16 (Math.Floor((height-topMargin-bottomMargin)/this.rowHeight))-1;         //不包括了header这一行,需要减1
    this.totalPageCount =this.dataTable .Rows .Count/maxRowNumPerPage;        //不计算header所以要减1;
    if(this.dataTable .Rows .Count%maxRowNumPerPage!=0) this.totalPageCount ++;
    if(totalPageCount==0)   this.totalPageCount =1; leftMargin =(width-GetStringPixelTotalWidth(dataTable,e)-dataTable.Columns .Count*2*CellPad)/2;                  //每个格子左右各2*cellpad
    if(leftMargin<2)
    {
    if(e.PageSettings .Landscape)  leftMargin=2;   //模向打印还不够宽 ,只有设leftMargin=1;这时有重叠现象

    else
    {
                        MessageBox.Show ("请改成模向打印");
    return;
    }
                                   
    }
    for(int i=0;i<this.dataTable .Columns .Count;i++)   
    {
    columns[i].ColumnName=this.dataTable .Columns [i].Caption;
    if(i==0) columns[i].LeftLinePosition = leftMargin;
    else     columns[i].LeftLinePosition =columns[i-1].LeftLinePosition +columns[i-1].ColumnWidth;
    columns[i].ColumnWidth=GetStringPixelWidth(this.dataTable,i,e)+2*CellPad;   //根据每列最大字符数计算列宽 }     }
                //以下是变化部分
    this.currentPageIndex ++;//该过程调用一次即增加一页
    for(int  i=-1;i<this.maxRowNumPerPage&&(currentContentRowNum)!=this.dataTable .Rows.Count;i++)    
    {
    if(i==-1)                 //为-1时显示Header
    {

    for(int j=0; j<this.dataTable.Columns.Count ;j++)
    {
    Cell _cell=new Cell (e,this.columns [j].ColumnName ,this.columns [j].LeftLinePosition,topMargin+(i+1)*this.rowHeight,this.columns [j].LeftLinePosition+this.columns [j].ColumnWidth,topMargin+(i+1)*this.rowHeight+this.rowHeight ,CellPad);
    _cell.CellHasBorders =this.hasBorders ;
    _cell.PrintCell ();
                        }
    }
    else //显示正文
    {
    for(int j=0; j<this.dataTable.Columns.Count ;j++)
    {
    Cell _cell=new Cell(e,this.dataTable.Rows[this.currentContentRowNum][j].ToString (),this.columns [j].LeftLinePosition,topMargin+(i+1)*this.rowHeight,this.columns [j].LeftLinePosition+this.columns [j].ColumnWidth ,topMargin+(i+1)*this.rowHeight+this.rowHeight,CellPad);
    _cell.CellHasBorders =this.hasBorders ;
    _cell.PrintCell ();

    }
    this.currentContentRowNum ++;

    }    } //////////////显示footer部分

    e.Graphics .DrawString ("共"+totalPageCount.ToString()+"页,当前是第"+Convert.ToString(this.currentPageIndex)+"页( 合计"+this.dataTable .Rows .Count .ToString ()+"行)" ,this.font,this.brush,this.footerleftPosition ,this.footertopPosition );     // pageRowCount=(int)((height-this.topMargin-titleSize-this.headerFont.Height-this.headerHeight-this.buttomMargin)/this.rowGap);
                
    if(this.currentPageIndex<this.totalPageCount)
    {
    e.HasMorePages=true;     
    }
    else
    {
    e.HasMorePages=false;
    this.currentPageIndex=0;
        
    } }
    public void Print()
    {
    pageSetupDialog = new PageSetupDialog();
    pageSetupDialog.Document = printDocument;
    pageSetupDialog.ShowDialog();  printPreviewDialog = new PrintPreviewDialog();
    printPreviewDialog.Document = printDocument;
    printPreviewDialog.Height = 600;
    printPreviewDialog.Width = 800;
    printPreviewDialog.ShowDialog();
    }
    #endregion
    #region    Attribute /// <summary>
    ///  设置是否给表添加边框,默认值为True带边框
    /// </summary>
    public  bool  HasBorders
    {
    set
    {
    this.hasBorders =value;
    }

    } #endregion
    #region   Private Method
    private float  GetStringPixelWidth(DataTable dataTable,int columnNum,PrintPageEventArgs p)
    {

    Graphics g=p.Graphics;     
    Font font=new Font("宋体",10);

    float maxWidth=g.MeasureString(dataTable.Columns[columnNum].Caption.Trim(),font).Width;
    for(int i=0;i<dataTable.Rows .Count ;i++)
    {
    float temWidth=g.MeasureString(dataTable.Rows[i][columnNum].ToString ().Trim(),font).Width;
                    if(temWidth>maxWidth)
    maxWidth=temWidth;
    }
    return maxWidth;

    }  private float  GetStringPixelTotalWidth(DataTable dataTable,PrintPageEventArgs p)
    {

    float maxWidth=0;
    Graphics g=p.Graphics;
    float  []  maxWidths=new float [dataTable.Columns .Count];
    for(int i=0;i<dataTable.Columns .Count ;i++)
    {
    float temWidth=g.MeasureString(dataTable.Columns[i].Caption.Trim(),font).Width;
    if(temWidth >maxWidths[i])
    maxWidths[i]=temWidth;
    } for(int i=0;i<maxWidths.Length;i++)
    {
    for(int j=0;j<dataTable.Rows .Count ;j++)
    {
    float temWidth=g.MeasureString(dataTable.Rows [j][i].ToString ().Trim(),font).Width;
    if(temWidth>maxWidths[i])
    maxWidths[i]=temWidth;
    }
    } foreach(float s in maxWidths)
    {
    maxWidth+=s;
    } return maxWidth;

    }  #endregion

    };
      

  2.   

    ////////////接着上面部分 internal class   Cell
    {

    private bool cellHasBorders;
    private float left;
    private float top;
    private float right;
    private float bottom;
    private string content="";
    private float cellPad;
    private PrintPageEventArgs pea;
    private Pen pen=new Pen (new SolidBrush (Color.Black));
            /// <summary>
            ///  设置或获取是否给表格添加边框
            /// </summary>
    public  bool  CellHasBorders
    {
    get
    {
        return this.cellHasBorders;
    }
    set
    {
    this.cellHasBorders =value;
    }
    } public Cell(PrintPageEventArgs pea, string content,float left ,float top, float right,float bottom,float cellPad)
    {
    this.left =left;
    this.top =top;
    this.bottom=bottom;
    this.right =right;
    this.content =content;
    this.cellPad =cellPad;
    this.pea =pea;
    } public void PrintCell()
    {
    if(CellHasBorders)  pea.Graphics.DrawRectangle(pen,this.left ,this.top ,this.right -this.left,this.bottom -this.top);
        PointF pointf=new PointF (this.left +this.cellPad ,this.top +this.cellPad);
    pea.Graphics .DrawString (this.content ,new Font("宋体", 10),new SolidBrush(Color.Black),pointf);
    }
    }}
      

  3.   

    根据你说的情况,在两个Panel里放TextBox并打印,我写了以下的代码.希望对你有帮助:
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    foreach(Control ct1 in panel1.Controls)
    {
    if(ct1.GetType().Name=="TextBox")
    {
    SizeF sf=e.Graphics.MeasureString(ct1.Text,ct1.Font,ct1.Width);
    sf.Width+=ct1.Font.Size;
    PointF pf=new PointF((float)ct1.Left,(float)ct1.Top);
    e.Graphics.DrawString(ct1.Text,ct1.Font,new SolidBrush(ct1.ForeColor),new RectangleF(pf,sf));
    }
    }
    foreach(Control ct2 in panel2.Controls)
    {
    if(ct2.GetType().Name=="TextBox")
    {
    SizeF sf=e.Graphics.MeasureString(ct2.Text,ct2.Font,ct2.Width);
    PointF pf=new PointF((float)(panel2.Left-panel1.Left+ct2.Left),(float)ct2.Top);
    e.Graphics.DrawString(ct2.Text,ct2.Font,new SolidBrush(ct2.ForeColor),new RectangleF(pf,sf));
    }
    }
    }