使用 itext 在 PDF 中打印多个 JTable

Printing Multiple JTable in a PDF using itext

我正在尝试在 PDF 文件中打印两个 JTable,这是我尝试过的但只打印了列的 headers,请问我如何添加 table。

try{
 Document document = new Document();
 PdfWriter.getInstance(document,new FileOutputStream("transcript.pdf"));
 document.open();
 document.add(new Paragraph("UNIVERSITY  OF  MAIDUGURI"));
 document.add(new Paragraph("======================================================================="));
 PdfPTable table1 = new PdfPTable(partITable.getColumnCount());
 PdfPTable table2 = new PdfPTable(partITable2.getColumnCount());
 table1.setSpacingAfter(10);
 table1.setSpacingBefore(5);

 for(int i=0;i<partITable.getColumnCount();i++){
 table1.addCell(partITable.getColumnName(i));
 }
 for(int rows=0;rows<partITable.getRowCount()-1;rows++){
     for(int cols=0;cols<partITable.getColumnCount();cols++){
     table1.addCell(partITable.getModel().getValueAt(rows,cols).toString());
     }

 }
 for(int i=0;i<partITable2.getColumnCount();i++){
 table2.addCell(partITable2.getColumnName(i));
 }
 for(int rows=0;rows<partITable2.getRowCount()-1;rows++){
     for(int cols=0;cols<partITable2.getColumnCount();cols++){
     table2.addCell(partITable2.getModel().getValueAt(rows,cols).toString());
     }

 }
 document.add(table1);
 document.add(table2);

 document.close();
 }
 catch(Exception e){
     JOptionPane.showMessageDialog(null, e);
 }

你有一个逻辑错误。我猜你有以下情况:partITable.getRowCount() = partITable2.getRowCount() = 1。因为你设置上限为partITable.getRowCount() - 1 = 0,for循环的条件returnsfalse,这意味着for循环体永远不会执行

总结一下,您必须将上限设置为 partITable.getRowCount()