我尝试使用PrintDocuments Control制作客户收据,现在我想在客户收据上显示页脚。页脚文本存储在字符串类型变量:String Footertex="ABC DEF GHIJK LMNOP QRSTUVWXYZ AAA BBBBBBB ABCD";
中,但我使用热敏打印机作为客户收据,并且只在页脚中打印以下文本:
ABC DEF GHIJK LMNOP
现在我希望它自动断行,并在下一行显示剩余的文本,如下所示:
ABC DEF GHIJK LMNOP
QRSTUVWXYZ AAA
BBBBBBB ABCD
我只是希望如果文本的字符数大于19,则会打破该行并在下一行中打印剩余的文本。提前致谢。
string item = (_List[i].Qty + " x " + _List[i].Name);
int nInterval1 = 18;
string res1 = String.Concat(item.Select((c, j) => i > 0 && (i % nInterval1) == 0 ? c.ToString() + Environment.NewLine : c.ToString()));
我已经尝试了上面的代码,但它在18个字符后打破了字符串,但在某些情况下,在18个字符后断行是没有意义的,因为它会破坏任何单词。我想在完整的一句话之后打破界限。
你可以使用以下代码(我的评论)来做到这一点:
var item = "Hide Copy Code QRSTUVWXYZ AAA BBBBBBB ABCD";
const int interval = 18;
var words = item.Split();
var result = "";
var symbolsInCurrentLine = 0;
foreach (var word in words)
{
if (word.Length + symbolsInCurrentLine > interval) // after adding word line will be longer than required
{
result += "\n";
symbolsInCurrentLine = 0;
}
else
{
if (symbolsInCurrentLine > 0) // add space after previous word if needed
{
result += " ";
symbolsInCurrentLine++;
}
result += word; // append word
symbolsInCurrentLine += word.Length;
}
}