簡體   English   中英

在iText PDF文檔中擬合JTable

[英]Fitting a JTable in an iText PDF Document

我有一個包含四列的JTable 我正在使用iText庫使用JTable中的數據打印PDF文檔。 問題是JTable在PDF中無法正確顯示。 我在Google上搜索過,在這里遇到了同樣的情況 該代碼與我的代碼以及輸出類似。 我也嘗試過使用Templates這個示例 ,但是結果沒有改變。

我們該如何解決? 請協助。 如果需要這些代碼,我會發布,但是它們的類太多了-我正在開發大型應用程序。 我想要的概念是使JTable適合文檔。

經過長時間的努力,我設法做到了如下所示。 萬一有人遇到這種情況,這就是拯救我的想法:

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

    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM