一行可以包含多少个字符?(C# 打印文本)

发布于 2025-01-05 23:17:07 字数 1586 浏览 0 评论 0原文

好的,我想打印文本框的文本,但是当我有一行太大它超出页面时我遇到一个问题我如何知道一行可以在边界内包含多少个字符,请记住大小和字体发生变化。

我已经从网络上的某个地方获得了这段代码,所以你知道我想要什么。

private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;

            Font printFont = txtMain.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            // Work out the number of lines per page, using the MarginBounds.
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            // Iterate over the string using the StringReader, printing each line.
            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                // calculate the next line position based on the height of the font according to the printing device
                yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                // draw the next line in the rich edit control
                e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }
            // If there are more lines, print another page.
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            myBrush.Dispose();
        }

提前致谢。

Ok so I want to print the text of a text box but I have one problem when I have a line that is too big it goes out of the page how can I know how many chars a line can contains within the bounds, keep in mind that the size and font change.

I already have this code that I got from the web somewhere so you know what i want.

private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;

            Font printFont = txtMain.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            // Work out the number of lines per page, using the MarginBounds.
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            // Iterate over the string using the StringReader, printing each line.
            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                // calculate the next line position based on the height of the font according to the printing device
                yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                // draw the next line in the rich edit control
                e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }
            // If there are more lines, print another page.
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            myBrush.Dispose();
        }

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心凉怎暖 2025-01-12 23:17:07

您应该阅读FontMetrics

一旦您知道如何获取字体规格,您可以将其与绘图区域结合使用来决定可以放置多少个字符。

编辑:
您可以按如下方式获取绘画区域的大小:

//This gives you a rectangle object with a length and width.
Rectangle bounds = e.MarginBounds;

一旦获得页面的宽度,就可以从字体中获取字体度量,然后将页面宽度除以字体的宽度。以此为基础,这就是您可以在页面上水平放置的字符数。确保您使用相同的单位(宽度是默认像素)。如果需要,您可以对垂直方向执行相同的操作。

You should read about FontMetrics

Once you know how to get the font metric, you can use that in combination with your drawing area to decide how many characters you can place.

EDIT:
You can get the size of the painting area as follows:

//This gives you a rectangle object with a length and width.
Rectangle bounds = e.MarginBounds;

Once you have the width of the page, you get the font metric from your font, then divide the page width by the font's width. Take the floor of that, and that is how many characters you can put horizontally on the page. Make sure that you are using the same units (width is default pixels). You can do the same thing for vertical if needed.

小鸟爱天空丶 2025-01-12 23:17:07

我使用这段代码可能会对您有所帮助。

private string txt(string yourtext)
{
    string[] text = new string[200];

    text = yourtext.ToLower().Split(' ');
    string b = "";
    int i = 1;

    foreach (string s in text)
    {
        if (b.Length < i * 100)
            b += s + " ";
        else
        {
            b += "\n";
            i++;
        }
    }

    return b;
}

I use this code that may help you.

private string txt(string yourtext)
{
    string[] text = new string[200];

    text = yourtext.ToLower().Split(' ');
    string b = "";
    int i = 1;

    foreach (string s in text)
    {
        if (b.Length < i * 100)
            b += s + " ";
        else
        {
            b += "\n";
            i++;
        }
    }

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