经过漫长的斗争,我设法如下所示。如果有人遇到这种情况,这是想法,救了我:
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);
}
}
};
*我使用iTex图书馆* - 你的意思是** iText **?还是有其他一些库,也许在TeX的背景下,你指的是? – mkl 2015-02-11 08:16:53
@mkl谢谢我错过了** t **,让我把它放在 – JWizard 2015-02-11 09:08:38