1

我正在开发一个报告应用程序,在该应用程序中,我必须基于 JTable 自定义在 pdf 上编写一些自定义。

我正在使用:- iText 5.3.3.jar java jdk 1.6

在这里,我格式化一个单元格以打印下划线和删除线值。我在 cellrenderer 中对此进行了编码。

为了在 pdf 上打印这些值,我直接在 pdf 上编写 JTable。但作为输出,我没有得到任何下划线/删除线。

JTable图像输出

JTable pdf

由于图像上出现删除线和下划线。它没有出现在pdf上。

以下是测试它的示例代码:-

**

  • 表图像.java

**

package com.swing.data;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;



class TableImage {

    public static void main(String[] args) throws Exception {
        Object[][] data = {
            {"Hari", new Integer(23), new Double(78.23), (true)},
            {"James", new Integer(23), new Double(47.64), (false)},
            {"Sally", new Integer(22), new Double(84.81), (true)}
        };
        String[] columns = {"Name", "Age", "GPA", "Pass"};
        JTable table = new JTable(data, columns);
        JScrollPane scroll = new JScrollPane(table);
        JPanel p = new JPanel(new BorderLayout());
        p.add(scroll, BorderLayout.CENTER);
        JOptionPane.showMessageDialog(null, p);
        JTableHeader h = table.getTableHeader();
        int x = table.getWidth();
        int y = table.getHeight();
        table.setIntercellSpacing(new Dimension(0,0));
        table.setDefaultRenderer(Object.class, new ColumnAlignmentRenderer(table.getDefaultRenderer(Object.class)));
        BufferedImage bi = new BufferedImage(
                x, y, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.createGraphics();
        Graphics2D g2 = (Graphics2D) g;
        table.paint(g2);
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
        print(table);
    }
    private static void print(JTable table) {
        Document document = new Document(PageSize.A4.rotate());
        try { 
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F://jTable.pdf")); 

            document.open(); 
            PdfContentByte cb = writer.getDirectContent(); 

            cb.saveState();

            PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
            Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());

            table.print(g2);
            cb.addTemplate(pdfTemplate, 20, 100);
            g2.dispose();
            cb.restoreState();
            } catch (Exception e) { 
            System.err.println(e.getMessage()); 
            } 

        document.close();

      }


    private TableImage() {
    }
}

**

  • 列对齐渲染器

**

package com.swing.data;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class ColumnAlignmentRenderer extends DefaultTableCellRenderer {

    private TableCellRenderer mWrappedRenderer;
    private HashMap objFormatCellMap;

    public ColumnAlignmentRenderer(TableCellRenderer pWrappedRenderer) {
        mWrappedRenderer = pWrappedRenderer;
    }

    public Component getTableCellRendererComponent(JTable pTable,
            Object pValue, boolean pIsSelected, boolean pHasFocus, int pRow,
            int pColumn) {
        int hAlignment = SwingConstants.LEFT;
        int vAlignment = SwingConstants.CENTER;
        String pattern = "##000.0";
        Font font = pTable.getFont();
        Border cellBorder = pTable.getBorder();
        Map fontAttributes = font.getAttributes();
        fontAttributes.put(TextAttribute.STRIKETHROUGH,TextAttribute.STRIKETHROUGH_ON);
        fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        font = font.deriveFont(fontAttributes);
        // Border cellBorder = BorderFactory.createCompoundBorder();
        Color backgroundColor = Color.WHITE;
        Color foregroundColor = Color.BLACK;
        // Use the wrapped renderer
        Component renderedComponent = mWrappedRenderer.getTableCellRendererComponent(pTable, pValue, pIsSelected,
                        pHasFocus, pRow, pColumn);

        int iLeft = 1;
        int iRight = 1;
        int iTop = 1;
        int iBottom = 1;

        if (pRow == 0) {
            ((JLabel) renderedComponent).setFont(font);
        }
        if (pIsSelected) {
            renderedComponent.setBackground(pTable.getSelectionBackground());
        }
        return renderedComponent;
    }

    public void setHashFormatCellData(HashMap hashFormatCellData) {
        this.objFormatCellMap = hashFormatCellData;
    }

}

**

  • 更新1:-

** 你所有的链接真的很有帮助!但是我使用以下代码在 JOPtionPane 上获得了正确的图像:-

    BufferedImage bi = new BufferedImage(
            x, y, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    Graphics2D g2 = (Graphics2D) g;
    table.paint(g2);
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

从上面的代码表图像带有删除线和下划线单元格。但是使用导致pdf输出的代码,显示单元格内容没有删除线和下划线。

 PdfContentByte cb = writer.getDirectContent(); 
            cb.saveState();
            table.addNotify();
            table.doLayout();
            PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
            Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
            table.printAll(g2);
            cb.addTemplate(pdfTemplate, 20, 100);
            g2.dispose();
            cb.restoreState();

请看看上面并提出一些建议。

4

1 回答 1

2

remove 遵循 3. 代码行,因为要创建一个快照,该快照将在来自 repaint() 所需的 Container 或 JComponents 的第一个事件时过期(在 API 中实现)

Graphics g = bi.createGraphics();
Graphics2D g2 = (Graphics2D) g;
table.paint(g2);

请参阅打印教程,尤其是Swing 组件中的打印支持,教程包含可运行的代码示例,

@Andrew Thompson的JTableHeader 和 BufferedImage很常见的问题

于 2012-10-18T12:15:24.590 回答