我有一個框架,並且上面有幾個組件。我如何使用iText繪製這個JFrame?使用iText將Swing組件導出爲PDF
我寫一些代碼如下(但不工作):
public void PrintFrameToPDF(Component c, File file) {
try {
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(file));
d.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate template = cb.createTemplate(c.getWidth(), c.getHeight());
Graphics2D g2d = template.createGraphics(c.getWidth(), c.getHeight());
c.paintAll(g2d);
c.addNotify();
c.validate();
g2d.dispose();
d.close();
} catch (Exception e) {
//
}
}
「不行」是什麼意思? – Paul 2012-01-04 18:08:15
http://stackoverflow.com/questions/25579480/how-to-export-jpanel-with-scrollable-into-pdf-file兄弟,請看這個解決方案, – 2015-11-21 06:49:38