int CurrentLine = 0;
int TotalLinesPerPage = 0;
int TotalWidth = 0;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string str = "";
Brush b = Brushes.Black;
Font f = fontDialog1.Font;
TotalWidth = (int)(e.MarginBounds.Width / e.Graphics.MeasureString("sf[CurrentLine]", f).Width);
TotalLinesPerPage = (int)(e.MarginBounds.Height / f.Height);
int y=TotalWidth;
for (int i = 0; i < TotalLinesPerPage && CurrentLine < sf.Length ; i++, CurrentLine++)
{
for (int x = 0; x < TotalWidth; x++)
{
if ((int)e.Graphics.MeasureString("sf[CurrentLine]", f).Width > e.MarginBounds.Width)
{
if (x < TotalWidth)
{
str += sf[CurrentLine].Substring(TotalWidth * x, TotalWidth) + Environment.NewLine;
CurrentLine++;
e.Graphics.DrawString(str, f, b, e.MarginBounds.X, e.MarginBounds.Y + i * f.Height);
}
else
{
str += sf[CurrentLine].Substring(TotalWidth * x);
e.Graphics.DrawString(str, f, b, e.MarginBounds.X, e.MarginBounds.Y + i * f.Height);
CurrentLine++;
}
}
}
} if (CurrentLine == sf.Length)
e.HasMorePages = false;
else
e.HasMorePages = true;
}
求问一下大神们 我想这么写的 先判断第一个循环先判断当前页是否够容纳当前的文本内容
第二层循环判断当前行是否超过了marginbounds的宽度如超过换行
我这么写打印出来的是空白页...不是很懂求指教
int TotalLinesPerPage = 0;
int TotalWidth = 0;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string str = "";
Brush b = Brushes.Black;
Font f = fontDialog1.Font;
TotalWidth = (int)(e.MarginBounds.Width / e.Graphics.MeasureString("sf[CurrentLine]", f).Width);
TotalLinesPerPage = (int)(e.MarginBounds.Height / f.Height);
int y=TotalWidth;
for (int i = 0; i < TotalLinesPerPage && CurrentLine < sf.Length ; i++, CurrentLine++)
{
for (int x = 0; x < TotalWidth; x++)
{
if ((int)e.Graphics.MeasureString("sf[CurrentLine]", f).Width > e.MarginBounds.Width)
{
if (x < TotalWidth)
{
str += sf[CurrentLine].Substring(TotalWidth * x, TotalWidth) + Environment.NewLine;
CurrentLine++;
e.Graphics.DrawString(str, f, b, e.MarginBounds.X, e.MarginBounds.Y + i * f.Height);
}
else
{
str += sf[CurrentLine].Substring(TotalWidth * x);
e.Graphics.DrawString(str, f, b, e.MarginBounds.X, e.MarginBounds.Y + i * f.Height);
CurrentLine++;
}
}
}
} if (CurrentLine == sf.Length)
e.HasMorePages = false;
else
e.HasMorePages = true;
}
求问一下大神们 我想这么写的 先判断第一个循环先判断当前页是否够容纳当前的文本内容
第二层循环判断当前行是否超过了marginbounds的宽度如超过换行
我这么写打印出来的是空白页...不是很懂求指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货