可能重复:
JTable to PDF converter
我正在尝试将当前表的内容保存为pdf。我的问题是,当我保存输出时,只保存屏幕上当前显示的内容。如果某行不在滚动窗格的当前视图中,则它不会出现在pdf中。我正在使用itextpdf-5.2.1.jar。这是我的代码。
private JPanel contentPane;
private JTable table_1;
private JTable table;
private DefaultTableModel aModel;
private JScrollPane pane;
private String currentTable;
private Edit edit;
public void createPdf() {
Document document = new Document();
try {
PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(
"mytable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(500, 500);
Graphics2D g2;
g2 = tp.createGraphicsShapes(500, 500);
pane.printAll(g2);
g2.dispose();
cb.addTemplate(tp, 30, 300);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}