在iText PDF文档中拟合JTable

我有一个有四列的JTable 。 我正在使用iText库来打印带有JTable数据的PDF文档。 问题是JTable在PDF中没有正确显示。 我在Google上搜索过,并在这里遇到了同样的情况 。 代码类似于我的以及输出。 我也尝试使用模板这个例子 ,但结果并没有改变。

我们如何解决这个问题? 请协助。 如果代码是必要的,我会发布,但他们是太多的类 – 我正在开发一个大型应用程序。 我想要的概念是使JTable适合文档。

经过长时间的斗争,我设法如下所示。 万一有人遇到这个,这就是救了我的想法:

 public void actionPerformed(ActionEvent e) { try { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("table.pdf")); doc.open(); PdfPTable pdfTable = new PdfPTable(table.getColumnCount()); //adding table headers for (int i = 0; i < table.getColumnCount(); i++) { pdfTable.addCell(table.getColumnName(i)); } //extracting data from the JTable and inserting it to PdfPTable for (int rows = 0; rows < table.getRowCount() - 1; rows++) { for (int cols = 0; cols < table.getColumnCount(); cols++) { pdfTable.addCell(table.getModel().getValueAt(rows, cols).toString()); } } doc.add(pdfTable); doc.close(); System.out.println("done"); } catch (DocumentException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } catch (FileNotFoundException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } } };