使用 Graphics.DrawString 在 C# 中打印长字符串
Printing long strings in C# with Graphics.DrawString
早上好。
我必须打印一些超出页面边缘的长字符串。我无法自动拆分为多行字符串。
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
...
string line ="Condimentum a a ac aenean parturient risus suscipit et orci scelerisque convallis porttitor enim venenatis viverra.Egestas nibh natoque mus etiam a parturient feugiat hendrerit a sagittis viverra dui ante varius lectus arcu."
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
ev.Graphics.DrawString(line, printFont, Brushes.Black, new RectangleF(leftMargin, yPos, 400.0f, 200.0f));
...
}
我尝试使用带有边界框规范的重载,如您所见,但结果是页面上出现字符堵塞,因为长字符串中的行被打印在自身上。
有没有快速解决这个问题的方法?
谢谢
已解决
我终于解决了我的问题。解释起来并不那么容易,所以我 post 这里有一段代码展示了我所做的事情:
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
float printAreaHeight = ev.MarginBounds.Height;
float printAreaWidth = ev.MarginBounds.Width;
string line = null;
float yPos = topMargin;
int charactersFitted = 0;
int linesFilled = 0;
SizeF theSize = new SizeF();
Font printFont = new Font("Arial", 12, FontStyle.Regular);
SizeF layoutSize = new SizeF(printAreaWidth, printAreaHeight);
// Calculate the number of lines per page.
linesPerPage = printAreaHeight / printFont.GetHeight(ev.Graphics);
// Print each line of the array.
while (count < linesPerPage && lineIdx < linesArray.Count())
{
line = linesArray[lineIdx++];
theSize = ev.Graphics.MeasureString(line, printFont, layoutSize, new StringFormat(), out charactersFitted, out linesFilled);
ev.Graphics.DrawString(line, printFont, Brushes.Black, new RectangleF(50.0F, yPos, theSize.Width, theSize.Height));
yPos += (1 + linesFilled) * printFont.GetHeight(ev.Graphics);
count += linesFilled + 1;
}
// If more lines exist, print another page.
if (count > linesPerPage)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
早上好。 我必须打印一些超出页面边缘的长字符串。我无法自动拆分为多行字符串。
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
...
string line ="Condimentum a a ac aenean parturient risus suscipit et orci scelerisque convallis porttitor enim venenatis viverra.Egestas nibh natoque mus etiam a parturient feugiat hendrerit a sagittis viverra dui ante varius lectus arcu."
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
ev.Graphics.DrawString(line, printFont, Brushes.Black, new RectangleF(leftMargin, yPos, 400.0f, 200.0f));
...
}
我尝试使用带有边界框规范的重载,如您所见,但结果是页面上出现字符堵塞,因为长字符串中的行被打印在自身上。
有没有快速解决这个问题的方法?
谢谢
已解决 我终于解决了我的问题。解释起来并不那么容易,所以我 post 这里有一段代码展示了我所做的事情:
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
float printAreaHeight = ev.MarginBounds.Height;
float printAreaWidth = ev.MarginBounds.Width;
string line = null;
float yPos = topMargin;
int charactersFitted = 0;
int linesFilled = 0;
SizeF theSize = new SizeF();
Font printFont = new Font("Arial", 12, FontStyle.Regular);
SizeF layoutSize = new SizeF(printAreaWidth, printAreaHeight);
// Calculate the number of lines per page.
linesPerPage = printAreaHeight / printFont.GetHeight(ev.Graphics);
// Print each line of the array.
while (count < linesPerPage && lineIdx < linesArray.Count())
{
line = linesArray[lineIdx++];
theSize = ev.Graphics.MeasureString(line, printFont, layoutSize, new StringFormat(), out charactersFitted, out linesFilled);
ev.Graphics.DrawString(line, printFont, Brushes.Black, new RectangleF(50.0F, yPos, theSize.Width, theSize.Height));
yPos += (1 + linesFilled) * printFont.GetHeight(ev.Graphics);
count += linesFilled + 1;
}
// If more lines exist, print another page.
if (count > linesPerPage)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}