2013-07-09 25 views
0

我正在使用c#做一個winforms結算系統。我在設置printdocument中字符串的寬度時遇到問題。獲取適合Printdocument行的字符數

如果項目描述的長度超過某個特定的長度,我想將其分解爲多行。數量,速率,金額的寬度是固定的,但是項目描述的寬度可以根據頁面寬度。

**Item Description  Qty Rate Amount** 

    1.Item Description-  1.0 12.00 12.00 
    One 
    2.Item Description-  3.0 20.00 60.00 
    Two 

我怎樣才能得到在特定的矩形區域適合的字符串,以便我可以拆分項目描述。

在此先感謝。

+0

如果您正在使用的PrintDocument控制打印頁面事件中使用PrintPageEventArgs變量來配置矩形(保證金bounds屬性)和頁面設置等... – terrybozzio

+0

terry.my的問題是,讓我不得不顯示從點0(左邊距)到點150的字符串。我想知道如何計算可以適合150點的字符串數量。 –

回答

0

會這樣的伎倆嗎?

const int AllowedCharactersPerLine = 16; 
string example = "this is a very long string that we want to divide to fit to several lines"; 
string StringWithRows = ""; 
int n = 0; 
for(int i = 0; i < example.Length; i++) 
{ 
    StringWithRows += example.Substring(i, 1); 
    n++; 
    if (n == AllowedCharactersPerLine) 
    { 
     n = 0; 
     StringWithRows += Environment.NewLine; 
    } 
}