2016-04-24 23 views
0

我想在PDF文件中打印两个JTable,这里是我试过但打印的只是列的标题,请如何添加表格的内容。使用itext在PDF中打印多个JTable

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

回答

0

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

总结您必须将上限设定为partITable.getRowCount()

+0

感谢Ayuba,它的工作原理 –