English 中文(简体)
计算用于打印的最大字符串长度
原标题:Calculating maximum string length for printing

我试图计算要打印的字符串的最大长度。 我通过使用单空格字体这样做, 但是这些字体在页面上占用了如此多的水平间距。 我确实需要打印每行更多的字符, 而不将字体缩到几乎看不见的水平, 所以我要使用一个 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[] {     ,  	  });
                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;
    }

此函数使用“报告”分类对象,这些对象的定义如下:

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

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

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

现在,我如何用对不同字符有不同宽度的字体来做这个工作? 我知道有一个函数叫做图形。MeaureString, 但这只会告诉我所选字体的字符串总宽度, 所以这意味着如果它确实在页面边缘运行, 它只会告诉我它会运行多远, 而不是有多少字符会运行过 。

最佳回答

也许您正在寻找 < a href=> http://msdn.microsoft.com/ en- us/library/ system.drawing.graphics. 度量字符数%28v=vs. 90%29.aspx" rel="nofollow">Graphics. MeasureCharacterRanges 。 您可以通过将字符串分割成单词来决定该方法的传递范围。 这样您就可以知道每个单词的宽度, 无论您喜欢怎样包装 。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签