我在Java代码中遇到问题,无法将JTable
转换为PDF。它抛出NullPointerException
并且相同的代码在另一个页面中起作用,两者都没有区别。
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);
table.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}