我有一个带有 jtable、Button 和 Jlabel 的 jframe。我有一个问题当我单击按钮时,似乎保存到 pdf 仅填写表格,但没有列名称。 如果我还想将 jlabel 添加到 pdf 文件中怎么办?
这是脚本:
Private void print(){
Document document = new Document(PageSize.A4.rotate());
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/jTable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
Graphics2D g2 = cb.createGraphicsShapes(500, 500);
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, 500, 500);
table_pdf.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();}
这是截图
它是pdf文件
最佳答案
表格的列标题显示为添加表格的 JScrollPane
的列标题。尝试使用JScrollPane
public JViewport getColumnHeader()
并将其打印在表格上方
关于java - 在Java中从JTable创建pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410033/