在iText PDF文档中拟合JTable [英] Fitting a JTable in an iText PDF Document
问题描述
我有一个 JTable
,它有四列。我正在使用iText库来打印带有JTable数据的PDF文档。问题是JTable在PDF中没有正确显示。我在谷歌搜索过,遇到了这里的情况相同。代码类似于我的以及输出。我也试过这个例子但是,使用模板,结果不会改变。
I have a JTable
that has four columns. I am using iText libraries to print PDF documents with data from the JTable. The problem is that the JTable is not showing properly in the PDF. I have searched on Google and came across the same situation here. The code is similar to mine as well as the output. I have also tried this example using Templates, however, the result is not changing.
我们如何解决这个问题?请协助。如果代码是必要的,我会发布,但他们是太多的类 - 我正在开发一个大型应用程序。 我想要的概念是让JTable适合文档。
How do we solve this? Please assist. If the codes are necessary, i will post but they are too many classes -i am working on a big application. The concept I would want is to make the JTable fit on the document.
推荐答案
经过很长时间斗争,我设法如下所示。万一有人遇到这个,这就是救了我的想法:
After a long struggle, I managed to make it as shown below. In case someone encounters this, here is the idea that saved me:
public void actionPerformed(ActionEvent e) {
try {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("table.pdf"));
doc.open();
PdfPTable pdfTable = new PdfPTable(table.getColumnCount());
//adding table headers
for (int i = 0; i < table.getColumnCount(); i++) {
pdfTable.addCell(table.getColumnName(i));
}
//extracting data from the JTable and inserting it to PdfPTable
for (int rows = 0; rows < table.getRowCount() - 1; rows++) {
for (int cols = 0; cols < table.getColumnCount(); cols++) {
pdfTable.addCell(table.getModel().getValueAt(rows, cols).toString());
}
}
doc.add(pdfTable);
doc.close();
System.out.println("done");
} catch (DocumentException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
这篇关于在iText PDF文档中拟合JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!