iText7 生成的 PDF 表格缺少底部边框
iText7 generated PDF tables missing bottom border
我目前正在使用 C# 生成一个包含 tables 数据的 PDF,但我遇到了一个问题,我的所有 tables 都没有底部边框。
例如
我很确定这里的问题是没有使用 iText7 库给出的 table.Complete() 方法但是使用这个方法 returns 一个错误说
System.NullReferenceException: 'Object reference not set to an instance of an object.' 这让我感到困惑,因为如果没有这种方法,我的 table 在 pdf 中生成得非常好,除了没有底部边框。我已经尝试查看 iText7 文档,但我可能只是瞎了眼,但我不确定这是代码问题还是我对 iText7 中 tables 的使用缺乏理解。
添加到 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 元素添加到文档元素中,而 .Complete() 返回 null 就是这个结果。我会在这里留下我的问题,以供将来需要参考的用户使用。
我目前正在使用 C# 生成一个包含 tables 数据的 PDF,但我遇到了一个问题,我的所有 tables 都没有底部边框。
例如
我很确定这里的问题是没有使用 iText7 库给出的 table.Complete() 方法但是使用这个方法 returns 一个错误说 System.NullReferenceException: 'Object reference not set to an instance of an object.' 这让我感到困惑,因为如果没有这种方法,我的 table 在 pdf 中生成得非常好,除了没有底部边框。我已经尝试查看 iText7 文档,但我可能只是瞎了眼,但我不确定这是代码问题还是我对 iText7 中 tables 的使用缺乏理解。
添加到 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 元素添加到文档元素中,而 .Complete() 返回 null 就是这个结果。我会在这里留下我的问题,以供将来需要参考的用户使用。