我在我的控制器中使用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();
}
我們沒有你的數據庫。我們無法分辨是否只有其他危險被填補。因此,請提供可供貴組織以外人員重複使用的樣本,且無需廣泛準備即可重現。 – mkl