将图像添加到PDF,Android Studio



>我正在使用iTextGAndroid Studio中开发应用程序,您可以在其中填写表单,单击按钮,然后您会收到一个PDF。在文档的末尾,我需要添加一个图像。

按下按钮后,应用程序崩溃,并显示"MyApp已停止"。然而,它创建了PDF文件,但它是空的,我无法打开它。

这是我的代码(在负责创建文档的函数中(:

Paragraph test = new Paragraph();
test.setAlignment(Element.ALIGN_LEFT);
try {
URL logoJock = new URL("https", "www.imgur.com", "/a/AsunJH7");
test.add(new Jpeg(logoJock));
} catch (Exception e) {
e.notify();
}

所以我检查了用法,添加图像的一种方法是通过 URL,所以我将其上传到 imgur 上。

document.open();
document.add(data);
document.add(test);
document.close();

我正在将段落添加到文档中。没有它,我的应用程序将创建包含我打算包含的所有数据的适当文档。唯一的问题是添加图像。

你能帮我解决这个问题吗?

问题解决了,

我试过这个,我试过 image.getInstance(path(,我尝试了其他可能性,最后这个成功了(我必须将我的徽标文件移动到 assets 文件夹(:

document.open();
try {
InputStream ims = getAssets().open("logo.PNG");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.scalePercent(30);
image.setAlignment(Element.ALIGN_LEFT);
document.add(image);
} catch (IOException e) {
e.printStackTrace();
}
document.close()

相关内容

最新更新