确定文本字符串中的行数?
作为打印类的一部分,我希望能够在多个页面上打印长字符串,但我不知道如何计算整个字符串的高度,我将通过首先计算字符串中的行数来确定。我知道我可以计算换行符的数量,但我也使用自动换行,因此每当一行超过页面宽度时就会添加换行符。所以我想我可以计算换行符的数量,计算出每行的宽度,并计算出每行是否需要自动换行换行符,但这对于某些东西来说似乎是一个过于复杂的问题我想可以做得更简单。
e.Graphics.DrawString(multiPageString, new Font("Courier New", 12), Brushes.Black, new RectangleF(0, 0, 810, pageHeight));
如果您有任何建议,请告诉我,谢谢!
As part of a printing class, I want to be able to print long strings over multiple pages, but I don't know how to calculate the height of the entire string, which I will determine by first counting the number of lines in my string. I know I can count the number of line breaks, but I am also using word-wrap, so line breaks will be added whenever a line goes on past the width of the page. So I suppose I could count the number of line breaks, and figure out the width of each line, and figure out if there is a need for a word-wrap line break for each line, but this seems like an overly complicated problem for something I imagine can be done more simply.
e.Graphics.DrawString(multiPageString, new Font("Courier New", 12), Brushes.Black, new RectangleF(0, 0, 810, pageHeight));
If you have any advice, please let me know thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可以编辑,但您可以使用它作为起始位置吗?
This can be edited, but you can probably use this as starting location?
正如 @Alexander 所建议的,一种很好且非常简单的方法就是在不绘图的情况下进行测试。让
Graphics
为您进行测试。假设您的最大行长度为
wrapWidth
(以像素为单位),并且您要检查的字符串是multiPageString
,那么您可以使用此函数:(此函数源自@Alexander 答案。)
As @Alexander suggested, one good and pretty simple approach would be just to test it without drawing. let the
Graphics
do the test for you.Suppose you have maximum line length of
wrapWidth
in pixel, and the string you want to check ismultiPageString
, then you may use this function:(This function is derived from @Alexander answer.)
我同意@David Heffernan 提供了一个真正的沼泽标准实现。对于他们要实现的价值来说,或者使用能够产生 100% 可靠的打印结果等报告服务的东西,这会变得很复杂。我感受到你的痛苦,印刷是辛苦的!
I agree with @David Heffernan provide a really bog standard implementation. Its way to complicated for the value they are going to achieve, or go with something that will produce 100% reliable printing results etc Reporting Services. I feel your pain, printing is painstaking!
如果要测量字符串的大小,请使用方法 Graphics.MeasureString(string, Font)
If you want to measure size of the string use method Graphics.MeasureString(string, Font)
为什么不将其填充到富文本控件中并打印出来呢?不超过 10-15 行代码就可以完成它。
Why don't you just stuff it into a rich text control and print that? No more than 10-15 lines of code should get it done.