我在c#中通过打印文档对象打印一系列字符串,它工作正常。默认情况下,每个字符串都以新行打印。但是如果一个字符串包含的字符多于一行可以打印的字符,则其余的字符将被截断,并且不会出现在下一行。 任何人都可以告诉我如何修复一行的字符数并在新行上打印超出的字符?
由于
答案 0 :(得分:1)
为了在每行的末尾进行文本换行,需要调用带有DrawString
对象的Rectangle
重载。文本将包含在该矩形内:
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
//This is a very long string that should wrap when printing
var s = new string('a', 2048);
//define a rectangle for the text
var r = new Rectangle(50, 50, 500, 500);
//draw the text into the rectangle. The text will
//wrap when it reaches the edge of the rectangle
e.Graphics.DrawString(s, Me.Font, Brushes.Black, r);
e.HasMorePages = false;
}
答案 1 :(得分:0)
这可能不是最佳做法,但一种选择是拆分数组,然后根据字符串是否仍然在行长度限制下将其添加到行字符串中。请记住,如果不使用等宽文本,则必须考虑字母宽度。
示例:
.button_div