Java iText:PDFPTable-无法在特定列中插入数据

Java iText:PDFPTable-无法在特定列中插入数据,java,pdf,itext,apache-poi,pdf-generation,Java,Pdf,Itext,Apache Poi,Pdf Generation,我必须使用iText PDF库创建一个表。该表包含3列,可以有多行。在任何行中,任何字段都可以为空,也可以有值。 我无法在iText中找到在特定列中创建单元格的方法。所以发生的事情是,如果任何一个条目为空,我下一列的数据将进入第一列。 代码片段- //Printing First column data if (!attributes.getJSONObject(i).isNull("First")) { PdfPCell cell = new PdfPCell(new

我必须使用iText PDF库创建一个表。该表包含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);
    }
在没有明确检查所有可能的情况下,如何处理此问题

为了使用ApachePOI生成excel表,我可以很容易地做到这一点,因为我可以在一行中插入特定列的数据。我已完成的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用单元格填充表格单元格。因此,不能跳过空单元格。但在这种情况下,为什么不简单地添加内容为空的
单元格
实例呢?例如,代替仅仅

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