首先 Font.Height 能返回该字体的高度。
再用 Graphics.MeasureString(string, Font);得到字串的长。
用计算来的长度除以打印区域的宽(打印纸的宽度)。
最后,行数乘以第一步的字体高度。应该能够得出Y坐标的值。

解决方案 »

  1.   

    //linesPerPage=e.MarginBounds.Height/printFont.GetHeight(g);
    //其中printFont.GetHeight(g)为字的高度
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    using System.Drawing.Printing;namespace PrintDocumentTest
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.Button button1;
    private System.Drawing.Printing.PrintDocument printDocument1;
    private System.IO.StringReader lineReader;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.printDocument1 = new System.Drawing.Printing.PrintDocument();
    this.SuspendLayout();
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(0, 16);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.Size = new System.Drawing.Size(288, 216);
    this.richTextBox1.TabIndex = 0;
    this.richTextBox1.Text = "richTextBox1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(88, 232);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(104, 32);
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // printDocument1
    // 
    this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.richTextBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    } private void button1_Click(object sender, System.EventArgs e)
    {
    PrintDialog printDialog=new PrintDialog();
    printDialog.Document=printDocument1;
    lineReader=new StringReader(richTextBox1.Text);
    if(printDialog.ShowDialog()==DialogResult.OK)
    {
    try
    {
    printDocument1.Print();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    //printDocument1.PrintController.OnEndPage(printDocument1,new System.Drawing.Printing.PrintEventArgs());
    } }
    } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    Graphics g=e.Graphics;
    float linesPerPage=0;
    float yPosition=0;
    int count=0;
    float leftMargin=e.MarginBounds.Left;
    float topMargin=e.MarginBounds.Top;
    string line=null;
    Font printFont=this.richTextBox1.Font;
    SolidBrush myBrush=new SolidBrush(Color.Black);
    linesPerPage=e.MarginBounds.Height/printFont.GetHeight(g);
    while(count<linesPerPage&&((line=lineReader.ReadLine())!=null))
    {
    yPosition=topMargin+(count*printFont.GetHeight(g));
    g.DrawString(line,printFont,myBrush,leftMargin,yPosition,new StringFormat());
    count++;
    }
    g.DrawString("ksfksakdkksfkaskfsdk",this.Font,new SolidBrush(Color.Black),200,200);
    if(line!=null)
    e.HasMorePages=true;
    else
    e.HasMorePages=false;
    }
    }
    }