gpt4 book ai didi

java - 如何在pdf上打印bufferedImage

转载 作者:行者123 更新时间:2023-12-04 19:13:09 25 4
gpt4 key购买 nike

我正在处理 pdf 报告应用程序。使用

itext 5.3.3.jar
JDK 1.6

我正在尝试在 pdf 上打印 JTable。我正确获得了 JTable 图像,但无法获得 pdf 格式的图像。无论如何我可以在pdf上打印BufferedImage。我使用了以下代码:-

如果可能,尝试在 pdf 上写一个 bufferedImage。

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.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() {
}
}

谢谢!

最佳答案

您可以简单地创建 com.itextpdf.text.Image来自 java.awt.image.BufferedImage然后使用标准的 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 Image Examples更多细节。

关于java - 如何在pdf上打印bufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12956844/

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