PrintDocument实例的 DefaultPageSettings 属性 可以设置;
之后Print()直接打印

解决方案 »

  1.   

    oooooooooooooooooooooooooooo学而时习之
      

  2.   

    没有报表  我是想把listview里的打出来
      

  3.   

    这个打印方法对你有用。直接传数据和座标值进去就可以了,主要应用是定点打印功能.坐标值以CM为单位(本来是以像数为单位的,转了)using System;
    using System.Configuration;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Forms;namespace WinUI.BusinessManage
    {
    /// <summary>
    /// 打印方法
    /// </summary>
    public class Printf
    {
    public Printf()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }private string[] printString =null;private string imagePath = @"";private  Font _font = new Font("宋体", 10);private int xPoint=0;private int yPoint=0;private float[] x=null;private float[] y=null;private Color  _color= Color.Black;private int pageCount = 1;private int pagePos=0;//分页计数器private int iPos=0;//内容计数器private int iCount=0;//每一页打印内容数// private float _top=0;
    // private float _left=0;
    private float _top=float.Parse(ConfigurationSettings.AppSettings.Get("PrintTop"));
    private float _left=float.Parse(ConfigurationSettings.AppSettings.Get("PrintLeft"));private float drawImageWidth=20;private float drawImageHeight=28;/// <summary>
    /// 图片路径
    /// </summary>
    public string ImagePath
    {
    set{imagePath=value;}
    get{return imagePath;}
    }/// <summary>
    /// 字体
    /// </summary>
    public Font Fonts
    {
    set{_font=value;}
    get{return _font;}
    }/// <summary>
    /// 字体颜色
    /// </summary>
    public string[] PrintString
    {
    set{ printString=value;}
    get{return printString;}
    }/// <summary>
    /// 定点打印X坐标
    /// </summary>
    public float[] X
    {
    set{ x=value;}
    get{return x;}
    }/// <summary>
    /// 定点打印Y坐标
    /// </summary>
    public float[] Y
    {
    set{ y=value;}
    get{return y;}
    }/// <summary>
    /// 字体颜色
    /// </summary>
    public Color FontColor
    {
    set{ _color=value;}
    get{return _color;}
    }/// <summary>
    /// 打印页数
    /// </summary>
    public int PageCount
    {
    set{ pageCount=value;}
    get{return pageCount;}
    }/// <summary>
    /// 上边距
    /// </summary>
    public float Top
    {
    set{ _top=value;}
    get{return _top;}
    }/// <summary>
    /// 左边距
    /// </summary>
    public float Left
    {
    set{ _left=value;}
    get{return _left;}
    }/// <summary>
    /// 打印图片宽度
    /// </summary>
    public float DrawImageWidth
    {
    set{ drawImageWidth=value;}
    get{return drawImageWidth;}
    }/// <summary>
    ///  打印图片高度
    /// </summary>
    public float DrawImageHeight
    {
    set{ drawImageHeight=value;}
    get{return drawImageHeight;}
    }/// <summary>
    /// 定点打印
    /// </summary>
    public void PrintFixedPoint()
    {
    PrintDocument MyPrintDC = new PrintDocument();
    PrintDialog MyPrintDg = new PrintDialog(); //创建打印对话框
    MyPrintDg.Document = MyPrintDC;
    if (MyPrintDg.ShowDialog() == DialogResult.OK)
    {
    try
    {
    iCount=printString.Length/pageCount;MyPrintDC.PrintPage += new PrintPageEventHandler(PrintFixedPointEvent);
    MyPrintDC.Print();
    }
    catch
    { //停止打印
    MyPrintDC.PrintController.OnEndPrint(MyPrintDC, new PrintEventArgs());
    }
    }
    }/// <summary>
    /// 执行定点打印操作
    /// </summary>
    /// <param name="sender"> </param>
    /// <param name="e"> </param>
    private void PrintFixedPointEvent(object sender, PrintPageEventArgs e)
    {
    if(imagePath!="")
    DrawImagePoint(sender, e);
    Graphics MyGraphics = e.Graphics;
    float x, y;
    if((pageCount>=1) && (printString!=null) )
    {
    if(printString.Length>0)
    if(printString[0]!=null)
    {
    pagePos++;for(int i=iPos;i <iCount;i++)
    {
    x = _left*40 + X[i]*40;
    y = _top*40 + Y[i]*40;
    MyGraphics.DrawString(printString[i], _font, new SolidBrush(_color), x, y, new StringFormat());
    }if(pagePos <pageCount)
    {
    iPos=iCount;
    iCount=iCount+(printString.Length/pageCount);
    e.HasMorePages = true;
    }
    else
    e.HasMorePages=false;
    }
    }
    }/// <summary>
    /// 画图事件方法
    /// </summary>
    /// <param name="sender"> </param>
    /// <param name="e"> </param>
    private void DrawImagePoint(object sender, PrintPageEventArgs e)
    {
    if(!System.IO.File.Exists(imagePath))
    return;
    Image newImage = Image.FromFile(imagePath);
    //Point ulCorner = new Point(xPoint, yPoint);
    e.Graphics.DrawImage(newImage, xPoint, yPoint,drawImageWidth*40,drawImageHeight*40);
    }/// <summary>
    ///打印设置
    /// </summary>
    public void PrintSetup()
    {
    PrintDocument MyPrintDC = new PrintDocument();
    PageSetupDialog MyPageSetupDg = new PageSetupDialog();
    MyPageSetupDg.Document = MyPrintDC;
    try
    {
    MyPageSetupDg.ShowDialog();
    }
    catch(Exception ex)
    {
    throw new Exception(ex.Message);
    }
    }}