110,536
社区成员
发帖
与我相关
我的任务
分享
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//获得绘制的对象
Graphics g=e.Graphics;
//以页中的行数
float linepage=0;
//待绘制文本的纵向座标
float yposition=0;
//行计数
int count=0;
//左边界
float leftmargin=e.MarginBounds.Left;
//顶边界
float topmargin=e.MarginBounds.Top;
//字符串流
string lines=null;
//根据页面的高度和字体的高度计算一页中可以打印的行数
linepage=e.MarginBounds.Height/this.Font.GetHeight(g);
//每次从字符串流中读取一行并打印
while(count<linepage&&((lines=this.richTextBox1.Text)!=null))
{
//计算这一行显示的位置
yposition=topmargin+(count*this.Font.GetHeight(g));
//绘制文本
g.DrawString(lines,this.Font,System.Drawing.Brushes.Black,leftmargin,yposition,new StringFormat());
count++;
}
//如果有多行则打印多页
if(lines!=null)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
}
}