我目前正在使用C#生成包含数据表的PDF,但我遇到的问题是我的所有表都没有底部边框。
我非常肯定这里的问题是缺少使用iText7库给出的table.Complete()方法,但使用此方法会返回一个错误,说System.NullReferenceException:'对象引用没有设置为对象的实例。'这让我感到困惑,因为没有这种方法我的表格在pdf中生成完全正常,除了没有底部边框。我试过通过iText7文档查看,我可能只是盲目但我不确定它是代码的问题还是我对iText7中表的使用缺乏了解。
在将其添加到PDF之前生成的示例代码
/* Test Table */
Paragraph glueTitle = new Paragraph("Test");
glueTitle.SetFontSize(15);
glueTitle.SetTextAlignment(TextAlignment.CENTER);
DataTable glueData = sql.loadGlue();
cellArrList = new List<Cell[]>();
headerInfo = new string[]
{
"Sensitive Info Placeholder"
};
Table glueTable = new Table(headerInfo.Length, true);
glueTable.SetTextAlignment(TextAlignment.CENTER);
glueTable.SetFontSize(8);
for (int i = 0; i < headerInfo.Length; i++)
{
Cell cell = new Cell();
cell.SetBackgroundColor(WebColors.GetRGBColor("dodgerblue"));
cell.SetFontColor(WebColors.GetRGBColor("white"));
cell.SetBold();
cell.Add(new Paragraph(headerInfo[i]));
glueTable.AddHeaderCell(cell);
}
foreach (DataRow row in glueData.Rows)
{
Cell[] cellArr = new Cell[headerInfo.Length];
for (int i = 0; i < headerInfo.Length; i++)
{
int x = i;
cellArr[i] = new Cell();
cellArr[i].SetBorderBottom(new SolidBorder(1f));
if (i == 2)
x = 3;
else if (i == 3)
x = 2;
cellArr[i].Add(new Paragraph(row[x + 1].ToString()));
}
if (row[2].ToString().Replace(" ", "") != "")
{
cellArr[1].SetBackgroundColor(ColorPickerPDF(CompareNumbers(length, row[2].ToString(), 1)));
cellArr[2].SetBackgroundColor(ColorPickerPDF(CompareNumbers(length, row[3].ToString(), 0)));
cellArr[3].SetBackgroundColor(ColorPickerPDF(CompareNumbers(width, row[4].ToString(), 1)));
cellArr[4].SetBackgroundColor(ColorPickerPDF(CompareNumbers(width, row[5].ToString(), 0)));
if (selection[1].Equals("1"))
cellArr[6].SetBackgroundColor(ColorPickerPDF(row[7].ToString().Equals("5")));
else
cellArr[6].SetBackgroundColor(WebColors.GetRGBColor("MediumSeaGreen"));
}
cellArrList.Add(cellArr);
}
foreach (Cell[] cellArr in cellArrList)
{
foreach (Cell cell in cellArr)
{
glueTable.AddCell(cell);
}
}
我发现了这个错误,我没有意识到在对它添加任何添加之前需要将我的pdf元素添加到文档元素中。并且返回null的.Complete()是这样的结果。我将在此留下我的问题,以供将来需要参考的用户使用。