110,537
社区成员
发帖
与我相关
我的任务
分享
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印方向(纵/横)
this.printDocument1.DefaultPageSettings.Landscape = false;
//设置纸张类型
PaperSize ps = new PaperSize("A4", (int)(5 * 100), (int)(10 * 100));
printDocument1.DefaultPageSettings.PaperSize = ps;
//边距,上下左右
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
int rightMargin = e.MarginBounds.Right;
int bottomMargin = e.MarginBounds.Bottom;
int nWidth = e.MarginBounds.Width - rightMargin - leftMargin;
int nHeight = e.MarginBounds.Height - topMargin - bottomMargin;
printDocument1.DefaultPageSettings.Margins = new Margins(leftMargin, rightMargin, topMargin, bottomMargin);
float linesPerPage = 0; //记录每页最大行数
float yPos = 0;//记录将要打印的一行数据在垂直方向的位置
int count = 0; //记录每页已打印行数
string line = null;//从RichTextBox中读取一段字符将存到line中
//每页最大行数=一页纸打印区域的高度/一行字符的高度
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics); //如果当前页已打印行数小于每页最大行数而且读出数据不为null,继续打印
while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(e.Graphics)); //yPos为要打印的当前行在垂直方向上的位置
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); //打印,参见第五章
count++;//已打印行数加1
}
if (line != null)//是否需要打印下一页
e.HasMorePages = true;//需要打印下一页
else
e.HasMorePages = false;//不需要打印下一页
}
RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
/*
text1:要绘制的文本
font1:文本的字体
Brushes:笔刷,上面是蓝色的笔刷
rectF1:指定的矩形,RectangleF 类型,四个参数分别是:开始点的横坐标、开始点的纵坐标、宽度、高度。(文本绘制的位置在矩形范围内自动换行)
*/