1

好的,所以我想打印文本框的文本,但是当我有一个太大的行超出页面时我有一个问题我怎么知道一行可以包含多少个字符,请记住大小和字体的变化。

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

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();
        }

提前致谢。

4

2 回答 2

2

您应该阅读有关FontMetrics的信息

一旦您知道如何获取字体度量,您就可以将它与您的绘图区域结合使用来决定您可以放置​​多少个字符。

编辑:您可以获得绘画区域的大小,如下所示:

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

一旦你有了页面的宽度,你就可以从你的字体中得到字体度量,然后将页面宽度除以字体的宽度。取而代之的是,您可以在页面上水平放置多少个字符。确保您使用相同的单位(宽度是默认像素)。如果需要,您可以对垂直执行相同的操作。

于 2012-02-16T19:47:52.787 回答
0

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

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;
}
于 2014-11-11T19:24:26.710 回答