计算打印的最大字符串长度

时间:2012-05-24 20:20:34

标签: c# visual-studio-2010 printing fonts

我正在尝试计算要打印的字符串的最大长度。我是通过使用单倍间距字体来完成的,但这些字体在页面上占据了相当多的水平空间。我真的需要在每行打印更多字符而不将字体大小减小到几乎不可见的级别,所以我想使用sans-serif字体代替。这是我目前的打印功能:

    public static void printPage(ref PrintPageEventArgs e, List<ReportLine> CompanyLetterhead, Queue<ReportLine> reportData)
    {
        int xPosition = e.MarginBounds.X;
        int yPosition = e.MarginBounds.Y;

        int maxCharacters = 0;

        foreach (ReportLine line in CompanyLetterhead)
        {
            maxCharacters = e.MarginBounds.Width / (int)line.selectedFont.Size;

            int position = 0;

            while (line.text.Length - position > maxCharacters)
            {
                e.Graphics.DrawString(line.text.Substring(position, position + maxCharacters), line.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += line.selectedFont.Height;
                position += maxCharacters;
            }
            if (line.text.Length - position > 0)
            {
                e.Graphics.DrawString(line.text.Substring(position), line.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += line.selectedFont.Height;
            }
        }

        while (reportData.Count > 0 && checkLine(yPosition, e.MarginBounds.Bottom, reportData.Peek().selectedFont.Height))
        {
            ReportLine currentLine = reportData.Peek();

            maxCharacters = e.MarginBounds.Width / (int)currentLine.selectedFont.Size;

            if (currentLine.text.Length > maxCharacters)
            {
                string[] words = currentLine.text.Split(new char[] { ' ' , '\t' });
                string printString = "";

                bool endsInSpace = true;

                foreach (string word in words)
                {
                    if (word.Length + printString.Length < maxCharacters)
                    {
                        if (printString.Length > 0)
                        {
                            printString += " ";
                        }
                        printString += word;
                    }
                    else if (printString.Length == 0)
                    {
                        printString += word.Substring(0, maxCharacters);
                        endsInSpace = false;
                    }
                    else
                    {
                        break;
                    }
                }

                e.Graphics.DrawString(printString, currentLine.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += currentLine.selectedFont.Height;
                if (endsInSpace)
                {
                    currentLine.text = currentLine.text.Remove(0, printString.Length + 1);
                }
                else
                {
                    currentLine.text = currentLine.text.Remove(0, printString.Length);
                }
            }
            else
            {
                e.Graphics.DrawString(currentLine.text, currentLine.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += currentLine.selectedFont.Height;
                reportData.Dequeue();
            }
        }

        e.HasMorePages = reportData.Count > 0;
    }

此函数使用ReportLine类对象,其定义如下:

public class ReportLine
{
    public string text;
    public Font selectedFont;

    public ReportLine()
    {
        text = "";
        selectedFont = null;
    }

    public ReportLine(string txt, Font font)
    {
        text = txt;
        selectedFont = font;
    }
}

现在我如何使用不同字符宽度不同的字体来完成这项工作?我知道有一个名为Graphics.MeasureString的函数,但这只会告诉我所选字体的字符串总宽度,所以这意味着如果它确实在页面边缘运行,它只会告诉我如何它将会运行,而不是将运行多少个字符。

1 个答案:

答案 0 :(得分:0)

也许你正在寻找Graphics.MeasureCharacterRanges。您可以通过将字符串拆分为单词来确定要传递给方法的范围。这样你就可以知道每个单词的宽度,包括你喜欢的包装。