为什么第2行table不写?
Why the 2nd row of table won't be written?
我正在尝试在我的 PDF 文档顶部创建 4 列 2 行的无边框 table。问题是第二行不会被写入。这是我的代码:
float[] columnWidths = { 2, 1, 1, 1};
PdfPTable table = new PdfPTable(columnWidths);
table.WidthPercentage = 100;
if (...) //true
{
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("AAA:_______________",infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 1st col,1st row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("BBB:_____", infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 2nd col,1st row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("CCC:_____", infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 3rd col,1st row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("DDD:_____", infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 4th col,1st row
}
}
if (...) //true
{
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("EEE: " + eee));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 1st col,2nd row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("FFF: " + fff));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 2nd col,2nd row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("GGG: " + ggg));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 3rd col,2nd row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("HHH:___________________"));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 4th col,2nd row
}
}
document.Add(table);
我该如何处理?第二个问题:我能否为每个 if 条件设置固定位置(检查代码中的注释),因此当第一行中的一个 if 条件不为真时,该单元格应该为空?
我假设您已将代码简化到您共享的代码段不再与您自己的代码一致的程度。您正在创建一个包含 4 列的 table。如果添加 4 个单元格,将呈现一行。如果添加 8 个单元格,将呈现两行。但是:如果您只添加 7 个单元格,则会添加一行。不完整行中的 3 个单元格将被省略,因为 iText 只呈现完整行。
另见 How to generate pdf if our column less than the declared table column and ItextSharp, number of Cells not dividable by the length of the row and Odd Numbered Cell Not Added To Pdf and PdfTable: last cell is not visible 和 ...
这解释了为什么没有显示第二行。添加以下行以查看是否可以解决问题:
table.CompleteRow();
至于你的另一个问题:你总是可以像这样添加一个空单元格:
PdfPCell cell = new PdfPCell();
if (someCondition) {
cell.addElement(new Paragraph("AAA"));
}
table.addCell(cell);
最后,您的代码中还有一个错误。这没有任何意义:
p.BorderWidth = 0;
边框宽度为 0 并不意味着不显示边框。正如之前多次解释的那样,ISO-32000-1 将宽度为 0 的线定义为宽度等于设备可以显示的最小宽度的线。如果您不想要任何边框,请使用:
p.Border = PdfPCell.NO_BORDER;
最后,我需要请您帮个忙:我们重新设计了 iText 网站,并在感恩节发布了它。我们现在注意到,我们的访问量不如更改前那么多。考虑到您需要的所有信息都可以在联机文档中找到,并且考虑到您仍然需要提出问题,我们想知道网站出了什么问题。我们可以做些什么来改进内容吗?驱使人们离开我们网站的原因可能是什么?你为什么要问这么多官方文档中已经回答的问题?我们现在的内容是不是太多了?
我正在尝试在我的 PDF 文档顶部创建 4 列 2 行的无边框 table。问题是第二行不会被写入。这是我的代码:
float[] columnWidths = { 2, 1, 1, 1};
PdfPTable table = new PdfPTable(columnWidths);
table.WidthPercentage = 100;
if (...) //true
{
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("AAA:_______________",infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 1st col,1st row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("BBB:_____", infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 2nd col,1st row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("CCC:_____", infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 3rd col,1st row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("DDD:_____", infoFont));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 4th col,1st row
}
}
if (...) //true
{
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("EEE: " + eee));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 1st col,2nd row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("FFF: " + fff));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 2nd col,2nd row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("GGG: " + ggg));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 3rd col,2nd row
}
if (...) //true
{
PdfPCell p = new PdfPCell(new Phrase("HHH:___________________"));
p.BorderWidth = 0;
table.AddCell(p); // fixed pos. 4th col,2nd row
}
}
document.Add(table);
我该如何处理?第二个问题:我能否为每个 if 条件设置固定位置(检查代码中的注释),因此当第一行中的一个 if 条件不为真时,该单元格应该为空?
我假设您已将代码简化到您共享的代码段不再与您自己的代码一致的程度。您正在创建一个包含 4 列的 table。如果添加 4 个单元格,将呈现一行。如果添加 8 个单元格,将呈现两行。但是:如果您只添加 7 个单元格,则会添加一行。不完整行中的 3 个单元格将被省略,因为 iText 只呈现完整行。
另见 How to generate pdf if our column less than the declared table column and ItextSharp, number of Cells not dividable by the length of the row and Odd Numbered Cell Not Added To Pdf and PdfTable: last cell is not visible 和 ...
这解释了为什么没有显示第二行。添加以下行以查看是否可以解决问题:
table.CompleteRow();
至于你的另一个问题:你总是可以像这样添加一个空单元格:
PdfPCell cell = new PdfPCell();
if (someCondition) {
cell.addElement(new Paragraph("AAA"));
}
table.addCell(cell);
最后,您的代码中还有一个错误。这没有任何意义:
p.BorderWidth = 0;
边框宽度为 0 并不意味着不显示边框。正如之前多次解释的那样,ISO-32000-1 将宽度为 0 的线定义为宽度等于设备可以显示的最小宽度的线。如果您不想要任何边框,请使用:
p.Border = PdfPCell.NO_BORDER;
最后,我需要请您帮个忙:我们重新设计了 iText 网站,并在感恩节发布了它。我们现在注意到,我们的访问量不如更改前那么多。考虑到您需要的所有信息都可以在联机文档中找到,并且考虑到您仍然需要提出问题,我们想知道网站出了什么问题。我们可以做些什么来改进内容吗?驱使人们离开我们网站的原因可能是什么?你为什么要问这么多官方文档中已经回答的问题?我们现在的内容是不是太多了?