gpt4 book ai didi

java - 报告 pdf 中未出现删除线和下划线

转载 作者:行者123 更新时间:2023-11-30 04:38:02 31 4
gpt4 key购买 nike

我正在开发一个报告应用程序,其中我必须根据 JTable 自定义在 pdf 上编写一些自定义内容。

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

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

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

JTable image output

JTable pdf

由于图像上出现删除线和下划线。它不会出现在 pdf 中。

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

**

  • TableImage.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();

请看一下上面的内容并提出建议。

最佳答案

删除遵循 3. 代码行,因为要创建一个快照,该快照将在 repaint() 所需的容器或 JComponent 中的第一个事件上过期(在 API 中实现)

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

参见Printing tutoria l 尤其是Printing Support in Swing Components ,教程包含可运行的代码示例,

JTableHeader and BufferedImage 很常见的问题作者:@安德鲁·汤普森

关于java - 报告 pdf 中未出现删除线和下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12954042/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com