【问题标题】:create an instance of com.lowagie.text.Image from byte[]从 byte[] 创建一个 com.lowagie.text.Image 的实例
【发布时间】:2026-01-26 09:25:02
【问题描述】:

我正在开发一个网络应用程序,它允许用户将一些图片发送到服务器,然后服务器发回一个包含这些图片和一些附加数据的 pdf 文件。我能够在我的服务器端接收图片。基本上我将它们作为字节 []。要创建 pdf 文件,我使用 iText。为了嵌入图片,我必须使用 com.lowagie.text.Image 对象。但是,如何从我拥有的 byte[] 创建 com.lowagie.text.Image 的实例?

【问题讨论】:

  • 在任何人告诉你如何解释数组之前,你需要说明图像是如何编码成字节的。

标签: java jsp itext


【解决方案1】:

在 com.lowagie.text.Image 中有一个方法 getInstance() 被重载以接受不同的输入,包括 String 文件名和 byte[] imgb。 (见http://www.docjar.com/docs/api/com/lowagie/text/Image.html#getInstance%28URL%29

com.lowagie.text.Image image01 = com.lowagie.text.Image.getInstance("test.jpg");
document.add(image01);

byte[] byte_array = .......

com.lowagie.text.Image image02 = com.lowagie.text.Image.getInstance(byte_array);
document.add(image02);

【讨论】: