2012-05-24 3 views
0

인쇄 할 문자열의 최대 길이를 계산하려고합니다. 나는 모노 간격 폰트를 사용하여 그렇게했지만, 이것들은 페이지의 수평 공간을 많이 차지한다. 거의 보이지 않는 수준의 글꼴 크기를 줄이지 않으면 서 줄당 문자를 더 많이 인쇄해야하므로 대신 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이라는 함수가 있다는 것을 알고 있습니다. 그러나 이것은 선택한 글꼴에서 문자열의 전체 너비만을 나타냅니다. 따라서 페이지의 가장자리를 따라 실행하면 이것이 어떻게 나에게 표시되는지 알 수 있습니다. 얼마나 많은 인물들이 달려들 지 않을지라도 그것은 끝날 것이다.

답변

0

어쩌면 당신은 Graphics.MeasureCharacterRanges을 찾고 있습니다. 문자열을 단어로 분리하여 메서드에 전달할 범위를 결정할 수 있습니다. 그렇게하면 각 단어가 얼마나 넓은지를 알 수 있습니다.