我正在处理PDF报告应用程序。使用
itext 5.3.3.jarJDK 1.6
我试图在PDF上打印JTable。我正确地获得了JTable图像,但无法在PDF上获取图像。无论如何,我可以在PDF上打印BufferedImage。我使用以下代码: -
如果可能的话,尝试在PDF上写一个缓冲图。
private static void print(BufferedImage bi,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();
PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
Image image = Image.getInstance(bi , null);
image.scaleToFit(table.getWidth(), table.getHeight());
table.addNotify();
table.validate();
Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
table.print(g2);
pdfTemplate.addImage(image);
/*g2.setColor(Color.BLACK);
g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/
//table.print(g2);
cb.addTemplate(pdfTemplate, 0,20);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
,但它向我展示了以下类似: -
图像必须具有绝对定位。线程" main" com.itextpdf.text.exceptions.illegalpdfsyntaxexception中的异常:不平衡保存/还原状态运营商。 在com.itextpdf.text.pdf.pdf.pdfcontentbyte.sanitycheck(pdfcontentbyte.java:3083) 在com.itextpdf.text.pdf.pdfcontentbyte.topdf(pdfcontentbyte.java:248) 在com.itextpdf.text.pdf.pdfformxobject。(pdfformxobject.java:88) 在com.itextpdf.text.pdf.pdftemplate.getformxobject(pdftemplate.java:247) 在com.itextpdf.text.pdf.pdfwriter.addxformstobody(pdfwriter.java:1293) 在com.itextpdf.text.pdf.pdfwriter.addsharedObjectStobody(pdfwriter.java:1304) 在com.itextpdf.text.pdf.pdfwriter.close(pdfwriter.java:1209) 在com.itextpdf.text.pdf.pdfdocument.close(pdfdocument.java:768) 在com.itextpdf.text.document.close(document.java:398) 在com.swing.data.tableimage.print(tableimage.java:102) 在com.swing.data.tableimage.main(tableimage.java:52)
以下是测试的代码: -
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.Image;
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 {
BufferedImage bi = null;
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(bi,table);
//createPdf(true,table);
}
private static void print(BufferedImage bi,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();
PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
Image image = Image.getInstance(bi , null);
image.scaleToFit(table.getWidth(), table.getHeight());
table.addNotify();
table.validate();
Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
table.print(g2);
pdfTemplate.addImage(image);
/*g2.setColor(Color.BLACK);
g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/
//table.print(g2);
cb.addTemplate(pdfTemplate, 0,20);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
private TableImage() {
}
}
谢谢!
您可以简单地从java.awt.image.bufferedimage创建com.itextpdf.text.image,然后使用标准iText方法进行渲染:
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import com.itextpdf.text.Image;
BufferedImage bufferedImage = ...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
Image iTextImage = Image.getInstance(baos.toByteArray());
有关更多详细信息,请参见Itext图像示例。
我找到了一个解决方案:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("Image.pdf"));
document.open();
PdfContentByte pdfCB = new PdfContentByte(writer);
Image image = Image.getInstance(pdfCB, bufferedImage, 1);
我找到了这个。似乎可以解释如何做绝对定位。我想您需要这样做。