iText:PDFPTable - 无法在特定列中插入数据
iText : PDFPTable - Not able to insert data in specific column
我必须使用 iText PDF 库创建 table。 table 包含 3 列,可以有多行。在任何行中,任何字段都可以为空或可以有值。
我无法在 iText 中找到在特定列中创建单元格的方法。所以发生的事情是,如果任何条目为空,我下一列的数据就会出现在第一列。
代码片段 -
//Printing First column data
if (!attributes.getJSONObject(i).isNull("First")) {
PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("First"), h4));
table.addCell(cell);
}
//Printing Second column data
if (!attributes.getJSONObject(i).isNull("Second ")) {
PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("Second "), h4));
table.addCell(cell);
}
//Printing Third column data
if (!attributes.getJSONObject(i).isNull("Third")) {
PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("Third"), h4));
table.addCell(cell);
}
如何在不明确检查所有可能情况的情况下处理这个问题?
为了使用 Apache POI 生成 excel table,我可以很容易地做到这一点,因为我可以为一行中的特定列插入数据。我完成的 excel 的代码片段 -
//Printing First column data
if (!attributes.getJSONObject(i).isNull("First")) {
row.createCell(1); //This will insert in first column
row.getCell(1).setCellValue(attributes.getJSONObject(i).getString("First"));
}
//Printing Second column data
if(!attributes.getJSONObject(i).isNull("Second")) {
row.createCell(2); //This will insert in second column
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
}
//Printing Third column data
if(!attributes.getJSONObject(i).isNull("Third")) {
row.createCell(3); //This will insert in third column
row.getCell(3).setCellValue(attributes.getJSONObject(i).getString("Third"));
}
iText 中有什么方法可以实现这一点吗? (在特定列中插入数据)
IText 用单元格来填充 table 个单元格。因此,您不能跳过空单元格。但为什么不在这种情况下简单地添加 Cell
个内容为空的实例呢?例如。仅代替
if(!attributes.getJSONObject(i).isNull("Second")) {
row.createCell(2); //This will insert in second column
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
}
你可以做到
row.createCell(2); //This will insert in second column
if(!attributes.getJSONObject(i).isNull("Second")) {
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
}
或者可能
row.createCell(2); //This will insert in second column
if(!attributes.getJSONObject(i).isNull("Second")) {
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
} else {
row.getCell(2).setCellValue("");
}
我必须使用 iText PDF 库创建 table。 table 包含 3 列,可以有多行。在任何行中,任何字段都可以为空或可以有值。 我无法在 iText 中找到在特定列中创建单元格的方法。所以发生的事情是,如果任何条目为空,我下一列的数据就会出现在第一列。 代码片段 -
//Printing First column data
if (!attributes.getJSONObject(i).isNull("First")) {
PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("First"), h4));
table.addCell(cell);
}
//Printing Second column data
if (!attributes.getJSONObject(i).isNull("Second ")) {
PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("Second "), h4));
table.addCell(cell);
}
//Printing Third column data
if (!attributes.getJSONObject(i).isNull("Third")) {
PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("Third"), h4));
table.addCell(cell);
}
如何在不明确检查所有可能情况的情况下处理这个问题?
为了使用 Apache POI 生成 excel table,我可以很容易地做到这一点,因为我可以为一行中的特定列插入数据。我完成的 excel 的代码片段 -
//Printing First column data
if (!attributes.getJSONObject(i).isNull("First")) {
row.createCell(1); //This will insert in first column
row.getCell(1).setCellValue(attributes.getJSONObject(i).getString("First"));
}
//Printing Second column data
if(!attributes.getJSONObject(i).isNull("Second")) {
row.createCell(2); //This will insert in second column
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
}
//Printing Third column data
if(!attributes.getJSONObject(i).isNull("Third")) {
row.createCell(3); //This will insert in third column
row.getCell(3).setCellValue(attributes.getJSONObject(i).getString("Third"));
}
iText 中有什么方法可以实现这一点吗? (在特定列中插入数据)
IText 用单元格来填充 table 个单元格。因此,您不能跳过空单元格。但为什么不在这种情况下简单地添加 Cell
个内容为空的实例呢?例如。仅代替
if(!attributes.getJSONObject(i).isNull("Second")) {
row.createCell(2); //This will insert in second column
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
}
你可以做到
row.createCell(2); //This will insert in second column
if(!attributes.getJSONObject(i).isNull("Second")) {
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
}
或者可能
row.createCell(2); //This will insert in second column
if(!attributes.getJSONObject(i).isNull("Second")) {
row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second"));
} else {
row.getCell(2).setCellValue("");
}