自动换行文本到打印页面?
我有一些打印字符串的代码,但如果字符串是:“Blah blah blah”...并且没有换行符,则文本占据一行。我希望能够塑造字符串的形状,使其自动换行到纸张的尺寸。
private void PrintIt(){
PrintDocument document = new PrintDocument();
document.PrintPage += (sender, e) => Document_PrintText(e, inputString);
document.Print();
}
static private void Document_PrintText(PrintPageEventArgs e, string inputString) {
e.Graphics.DrawString(inputString, new Font("Courier New", 12), Brushes.Black, 0, 0);
}
我想我可以计算出字符的长度,并手动换行文本,但如果有内置的方法可以做到这一点,我宁愿这样做。谢谢!
I have some code that prints a string, but if the string is say: "Blah blah blah"... and there are no line breaks, the text occupies a single line. I would like to be able to shape the string so it word wraps to the dimensions of the paper.
private void PrintIt(){
PrintDocument document = new PrintDocument();
document.PrintPage += (sender, e) => Document_PrintText(e, inputString);
document.Print();
}
static private void Document_PrintText(PrintPageEventArgs e, string inputString) {
e.Graphics.DrawString(inputString, new Font("Courier New", 12), Brushes.Black, 0, 0);
}
I suppose I could figure out the length of a character, and wrap the text manually, but if there is a built in way to do this, I'd rather do that. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,有 DrawString 能够自动换行文本。您可以使用 MeasureString 方法来检查指定的字符串是否可以完全绘制页面与否以及需要多少空间。
还有一个 TextRenderer 专门用于此目的的类。
这是一个示例:
这里我指定了最大 60 像素作为宽度,然后测量字符串将给出绘制该字符串所需的大小。现在,如果您已经有了一个 Size,那么您可以与返回的 Size 进行比较,看看它是否会被正确绘制或截断
Yes there is the DrawString has ability to word wrap the Text automatically. You can use MeasureString method to check wheather the Specified string can completely Drawn on the Page or Not and how much space will be required.
There is also a TextRenderer Class specially for this purpose.
Here is an Example:
Here i have specified maximum of 60 Pixels as width then measure string will give me Size that will be required to Draw this string. Now if you already have a Size then you can compare with returned Size to see if it will be Drawn properly or Truncated
我发现了这个:如何:在 Windows 窗体中打印多页文本文件< /a>
I found this : How to: Print a Multi-Page Text File in Windows Forms
老兄,我正在为 HTML 打印而烦恼,这简直就是噩梦。我想说,在我看来,您应该尝试使用其他东西来打印文本等,将参数传递给报告服务并弹出用户可以打印的 PDF。
或者您可能需要计算字符数并显式指定换行符!
Dude im suffering with printing in HTML, total nightmare. I would say that in my opinion you should try use something else to print out the text etc pass parameters to reporting services and popup a PDF which the user can print off.
Or potentially you might need to count out the number of characters and explicitly specify a line break!