我正在编写一个报表应用程序,我必须根据JTable自定义在pdf上编写一些自定义。
我正在使用: - iText 5.3.3.jar java jdk 1.6
这里我格式化一个单元格以打印下划线和删除线值。我在cellrenderer中编码。
要在pdf上打印这些值,我直接在pdf上写JTable。 但作为输出,我没有得到任何下划线/删除线。
由于图像上出现了删除线和下划线。它没有出现在pdf上。
以下是测试它的示例代码: -
**
**
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;
}
}
**
** 您的所有链接都非常有用!但是我在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();
请看一下上面的内容并提出建议。
答案 0 :(得分:2)
删除以下3.代码行,因为要创建一个快照,该快照将在Container或JComponents的第一个事件上过期,而repaint()(在API中实现)
Graphics g = bi.createGraphics();
Graphics2D g2 = (Graphics2D) g;
table.paint(g2);
请参阅Printing tutoria l特别是Printing Support in Swing Components,教程包含可运行的代码示例,
@Andrew Thompson与JTableHeader and BufferedImage相当普遍的问题