我正在通過打印文檔對象在c#中打印一系列字符串,並且它工作正常。每個字符串默認打印一個新行。但是如果一個字符串包含的字符數多於一行可以打印的字符數,那麼其餘的字符將被截斷,並且不會出現在下一行。 任何人都可以告訴我如何修復一行的字符數量並在新行上打印超出的字符?如何修復用於打印文檔打印的線寬c#
感謝
我正在通過打印文檔對象在c#中打印一系列字符串,並且它工作正常。每個字符串默認打印一個新行。但是如果一個字符串包含的字符數多於一行可以打印的字符數,那麼其餘的字符將被截斷,並且不會出現在下一行。 任何人都可以告訴我如何修復一行的字符數量並在新行上打印超出的字符?如何修復用於打印文檔打印的線寬c#
感謝
爲了使您的文本換行的每一行的末尾,你需要調用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;
}
這可能不是最好的做法,而是一種選擇是分裂數組,然後它基於字符串是否仍然會加入到一個線串在線路長度限制下。請記住,如果不使用等寬文本,則必須考慮字母寬度。
實施例:
String sentence = "Hello my name is Bob, and I'm testing the line length in this program.";
String[] words = sentence.Split();
//Assigning first word here to avoid begining with a space.
String line = words[0];
//Starting at 1, as 0 has already been assigned
for (int i = 1; i < words.Length; i++)
{
//Test for line length here
if ((line + words[i]).Length < 10)
{
line = line + " " + words[i];
}
else
{
Console.WriteLine(line);
line = words[i];
}
}
Console.WriteLine(line);