【问题标题】:Fitting a JTable in an iText PDF Document在 iText PDF 文档中安装 JTable
【发布时间】:2015-02-11 07:03:49
【问题描述】:

我有一个JTable,它有四列。我正在使用 iText 库来打印带有 JTable 数据的 PDF 文档。问题是 JTable 没有在 PDF 中正确显示。我在谷歌上搜索并遇到了the same situation here。该代码类似于我的以及输出。我也试过this example using Templates,但是结果没有改变。

我们如何解决这个问题?请协助。如果代码是必要的,我会发布,但它们的类太多了——我正在开发一个大型应用程序。 我想要的概念是使 JTable 适合文档。

【问题讨论】:

  • 我正在使用 iTex 库 - 你真的是指 iText 吗?或者是否有其他库,可能是在 TeX 的上下文中,您指的是?
  • @mkl 谢谢我错过了t,让我说吧

标签: java swing pdf jtable itextpdf


【解决方案1】:

经过长时间的奋斗,我设法做到了,如下图所示。如果有人遇到这种情况,这是拯救我的想法:

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);
        }

    }
};

【讨论】:

  • 我试过你的代码。我遇到了一个问题并通过使用 table.getRowCount() 而不是 table.getRowCount()-1 解决了它。
  • @Junaid 好的,很好。当事情无法按照我想要的方式出现时,我编写了该代码。这很难,我是一名 Java 初级 :)-
猜你喜欢
  • 1970-01-01
  • 2014-03-24
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多