2015-11-11 35 views
1

我在我的控制器中使用iTextSharp顯示列表中的危險列表分裂兩列,如果列表太長,一個以下代碼。itextSharp ColumnText只顯示其他每個foreach循環短語

當我運行它時,我只能看到文檔中顯示的其他危險。

int hazardsids擁有全部28個,但只有14個顯示,並且檢查了結果,它只顯示ID爲偶數的危險。

ColumnText ct = new ColumnText(cb); 
ct.Alignment = Element.ALIGN_JUSTIFIED; 

Paragraph hazardTitle = new Paragraph("Hazards", bold); 
hazardTitle.Alignment = Element.ALIGN_CENTER; 
doc.Add(hazardTitle); 
//Get vertical position of hazardTitle 
var pos = pdfWriter.GetVerticalPosition(false); 
int[] hazardIds = db.RiskAssessmentHazards 
        .Where(x => x.RiskAssessmentId == raId) 
        .OrderBy(x => x.HazardId) 
        .Select(x => x.HazardId)         
        .ToArray(); 
foreach (int HazardIds in hazardIds) 
{ 
    ct.AddText(new Phrase("- " + db.Hazards.FirstOrDefault(x => x.HazardId == HazardIds).Name + "\n")); 
} 

float gutter = 15f; 

float colwidth = (doc.Right - doc.Left - gutter)/2;    

float[] left = { doc.Left , pos, 
    doc.Left , doc.Bottom }; 

float[] right = { doc.Left + colwidth, doc.Top - 80f, 
    doc.Left + colwidth, doc.Bottom }; 

float[] left2 = { doc.Right - colwidth, pos, 
    doc.Right - colwidth, doc.Bottom }; 

float[] right2 = {doc.Right, doc.Top - 80f, 
    doc.Right, doc.Bottom }; 

int status = 0; 
int i = 0; 
//Checks the value of status to determine if there is more text 
//If there is, status is 2, which is the value of NO_MORE_COLUMN 
while (ColumnText.HasMoreText(status)) 
{ 
    if (i == 0) 
    { 
     //Writing the first column 
     ct.SetColumns(left, right); 
     i++; 
    } 
    else 
    { 
     //write the second column 
     ct.SetColumns(left2, right2); 
    } 

    //Needs to be here to prevent app from hanging 
    ct.YLine = pos; 
    //Commit the content of the ColumnText to the document 
    //ColumnText.Go() returns NO_MORE_TEXT (1) and/or NO_MORE_COLUMN (2) 
    //In other words, it fills the column until it has either run out of column, or text, or both 
    status = ct.Go(); 
} 
+0

我們沒有你的數據庫。我們無法分辨是否只有其他危險被填補。因此,請提供可供貴組織以外人員重複使用的樣本,且無需廣泛準備即可重現。 – mkl

回答

3

想你的示例代碼,用硬編碼的「單詞」數組模擬數據庫調用,並能複製同樣的問題 - 只有偶數「HazardIds」被寫入PDF。只用了快看,所以不能完全確定是否是問題,你要調用Go()或別的東西,但這裏有一個簡單的工作示例已經做了你在找什麼:

Font font = FontFactory.GetFont("Arial", 20); 
string word = "word word word word"; 
StringBuilder sb = new StringBuilder(); 

PdfContentByte cb = writer.DirectContent; 
ColumnText ct = new ColumnText(cb); 
ct.Alignment = Element.ALIGN_JUSTIFIED; 

Paragraph hazardTitle = new Paragraph("Hazards", font); 
hazardTitle.Alignment = Element.ALIGN_CENTER; 

var pos = writer.GetVerticalPosition(false) - 40; 
float gutter = 15f; 
float colwidth = (doc.Right - doc.Left - gutter)/2; 
float col0right = doc.Left + colwidth; 
float col1left = col0right + gutter; 
float col1right = col1left + colwidth; 
float[][] COLUMNS = 
{ 
    new float[] { doc.Left, doc.Bottom, col0right, pos }, 
    new float[] { col1left, doc.Bottom, col1right, pos } 
}; 

for (int i = 1; i <= 40; ++i) 
{ 
    ct.AddText(new Phrase(string.Format(
     "- [{0}] {1}", 
     i, sb.Append(word).ToString() 
    ))); 
    ct.AddText(Chunk.NEWLINE); 
} 

int status = 0; 
int column = 0; 
while (ColumnText.HasMoreText(status)) 
{ 
    ct.SetSimpleColumn(
     COLUMNS[column][0], COLUMNS[column][1], 
     COLUMNS[column][2], COLUMNS[column][3] 
    ); 
    status = ct.Go(); 
    column = Math.Abs(column - 1); 
    if (column == 0) 
    { 
     doc.Add(hazardTitle); 
     doc.NewPage(); 
    } 
} 
// add last title if first column still has space 
if (column != 0) 
{ 
    doc.Add(hazardTitle); 
} 
+0

謝謝@kuujinbo多數民衆贊成在固定的問題,感謝您抽出時間來使我的不完整的職位 –