确定文本字符串中的行数?

发布于 2024-10-17 20:32:02 字数 368 浏览 4 评论 0原文

作为打印类的一部分,我希望能够在多个页面上打印长字符串,但我不知道如何计算整个字符串的高度,我将通过首先计算字符串中的行数来确定。我知道我可以计算换行符的数量,但我也使用自动换行,因此每当一行超过页面宽度时就会添加换行符。所以我想我可以计算换行符的数量,计算出每行的宽度,并计算出每行是否需要自动换行换行符,但这对于某些东西来说似乎是一个过于复杂的问题我想可以做得更简单。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦归所梦 2024-10-24 20:32:02
int iMaxWidth = 240; // Maximum width, in pixels
string sText = @"blah-blah-blah";
Font objFont = new Font("Times New Roman", 13, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
StringFormat sfFmt = new StringFormat(StringFormatFlags.LineLimit);
Graphics g = Graphics.FromImage(new Bitmap(1, 1));
int iHeight = (int)g.MeasureString(sText, objFont, iMaxWidth, sfFmt).Height;
int iOneLineHeight = (int)g.MeasureString("Z", objFont, iMaxWidth, sfFmt).Height;
int iNumLines = (int)(iHeight/iOneLineHeight);
int iMaxWidth = 240; // Maximum width, in pixels
string sText = @"blah-blah-blah";
Font objFont = new Font("Times New Roman", 13, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
StringFormat sfFmt = new StringFormat(StringFormatFlags.LineLimit);
Graphics g = Graphics.FromImage(new Bitmap(1, 1));
int iHeight = (int)g.MeasureString(sText, objFont, iMaxWidth, sfFmt).Height;
int iOneLineHeight = (int)g.MeasureString("Z", objFont, iMaxWidth, sfFmt).Height;
int iNumLines = (int)(iHeight/iOneLineHeight);
猫卆 2024-10-24 20:32:02

这可以编辑,但您可以使用它作为起始位置吗?

const int PAGE_MAX_CHARS_PER_LINE = 30;
var message = "this is a really long line and it will make your eyes pop out but I don't know how to present a long line differently.  So you will have to stick with\r\nit.   I think the above line should be long enough\r\n\r\n I would love to see how this turns out.";
var lines = message.Split(new string [] {"\r\n"}, StringSplitOptions.None);
var linesCount = lines.Length;

var longLines = lines.Where(i=>i.Length > PAGE_MAX_CHARS_PER_LINE);
foreach(var l in longLines)
{
  int numberOfLines = (int)Math.Ceiling((double)l.Length / PAGE_MAX_CHARS_PER_LINE); /// Will need to embed graphics measurement mechanism here?
  linesCount += numberOfLines - 1;
}

This can be edited, but you can probably use this as starting location?

const int PAGE_MAX_CHARS_PER_LINE = 30;
var message = "this is a really long line and it will make your eyes pop out but I don't know how to present a long line differently.  So you will have to stick with\r\nit.   I think the above line should be long enough\r\n\r\n I would love to see how this turns out.";
var lines = message.Split(new string [] {"\r\n"}, StringSplitOptions.None);
var linesCount = lines.Length;

var longLines = lines.Where(i=>i.Length > PAGE_MAX_CHARS_PER_LINE);
foreach(var l in longLines)
{
  int numberOfLines = (int)Math.Ceiling((double)l.Length / PAGE_MAX_CHARS_PER_LINE); /// Will need to embed graphics measurement mechanism here?
  linesCount += numberOfLines - 1;
}
極樂鬼 2024-10-24 20:32:02

正如 @Alexander 所建议的,一种很好且非常简单的方法就是在不绘图的情况下进行测试。让Graphics为您进行测试。

假设您的最大行长度为wrapWidth(以像素为单位),并且您要检查的字符串是multiPageString,那么您可以使用此函数:(

private int GetNumOfLines(string multiPageString, int wrapWidth, Font fnt)
{
    StringFormat sfFmt = new StringFormat(StringFormatFlags.LineLimit);
    using(Graphics g = Graphics.FromImage(new Bitmap(1, 1)))
    {
        int iHeight = (int)g.MeasureString(multiPageString, fnt, wrapWidth, sfFmt).Height;
        int iOneLineHeight = (int)g.MeasureString("Z", fnt, wrapWidth, sfFmt).Height;
        return (int)(iHeight/iOneLineHeight);
    }
}

此函数源自@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 is multiPageString, then you may use this function:

private int GetNumOfLines(string multiPageString, int wrapWidth, Font fnt)
{
    StringFormat sfFmt = new StringFormat(StringFormatFlags.LineLimit);
    using(Graphics g = Graphics.FromImage(new Bitmap(1, 1)))
    {
        int iHeight = (int)g.MeasureString(multiPageString, fnt, wrapWidth, sfFmt).Height;
        int iOneLineHeight = (int)g.MeasureString("Z", fnt, wrapWidth, sfFmt).Height;
        return (int)(iHeight/iOneLineHeight);
    }
}

(This function is derived from @Alexander answer.)

空宴 2024-10-24 20:32:02

我同意@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!

止于盛夏 2024-10-24 20:32:02

如果要测量字符串的大小,请使用方法 Graphics.MeasureString(string, Font)

graphics.MeasureString("some string", new Font("Arial", 10))

If you want to measure size of the string use method Graphics.MeasureString(string, Font)

graphics.MeasureString("some string", new Font("Arial", 10))
离线来电— 2024-10-24 20:32:02

为什么不将其填充到富文本控件中并打印出来呢?不超过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文