c# - itextSharp ColumnText 只显示每个其他 foreach 循环短语

标签 c# asp.net-mvc itext

我的 Controller 中有以下代码,如果列表太长,则使用 iTextSharp 显示分为两列的危险列表。

当我运行它时,我只得到文档中显示的所有其他危险。

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();
}

最佳答案

尝试了您的示例代码,使用一组硬编码的“单词”模拟您的数据库调用,并且可以重现相同的问题 - 只有偶数编号的“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);
}

关于c# - itextSharp ColumnText 只显示每个其他 foreach 循环短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33653460/

相关文章:

java - itext7 标记和编辑注释不起作用

c# - 无法使用 itextsharp 垂直对齐表格单元格中的文本

C# 编码(marshal)处理 C DLL struct bool

c# - 将 C++ istringstream 用法转换为 C#

c# - session 对象在 Azure 上变为 null

asp.net-mvc - 没有类型为 'Blah' 的键为 'IEnumerable<SelectListItem>' 的 ViewData 项

c# - 将 MySQL .DMP 文件转换为 MS Sql Server 2005 的 .SQL 文件

c# - SQL Server datetimeoffset 和 json 自动将 datetimeoffset 更改为 datetime

asp.net-mvc - 将 View 数据保留在 RedirectToAction 上

java - 验证时如何将PDF注释与签名连接起来?