1

我正在使用iTextG在Android 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();

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

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

4

1 回答 1

4

问题解决了,

我尝试了这个,我尝试了 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()
于 2018-06-20T08:04:25.110 回答