目前,我正在使用 iText 将 jTable 数据转换为 pdf。
private void print() {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
Graphics2D g2 = cb.createGraphicsShapes(800, 500);
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, 800, 500);
jTable.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
我遇到的问题是没有表格标题,假设表格单元格中的数据显示由于空间不够而不完整,那么在pdf中数据也显示不完整。有没有其他API可以将jTable模型数据转换为pdf?
最佳答案
是的,另一个 API 是 Docmosis 。您可以将模型数据放入 DataProvider 实例中,并使用它来填充模板。该模板将控制表格的外观,Docmosis 将填充它以生成 PDF。您可以将其作为图像来完成,但使用模板来设计表格的外观和感觉会更好。
关于Java JTable 导出为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3167219/