繁体   English   中英

如何使用itext将图像导入PDF

[英]How can I import an image to my PDF using itext

我已经创建了表格和一些其他信息,以PDF格式。 但是无论如何,我都无法将图像导入到PDF中。 我在素材资源文件夹中有my_thumb.png文件。

在我的MainActivity类中

try {
        inputStream = MainActivity.this.getAssets().open("my_thumb.png");
        Bitmap bmp = BitmapFactory.decodeStream(inputStream);
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
       model.setThumbStream(stream); 
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在我的CreatePDF类别中

Image thumb = Image.getInstance(Model.getThumbStream().toByteArray());
   companyLogo.setAbsolutePosition(document.leftMargin(),121);
 companyLogo.scalePercent(25);

   document.add(thumb); 

问题是

bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

该行返回null。

我想念的是什么?

提前致谢。

您首先将png变为可绘制

Drawable d = Drawable.createFromStream(getAssets().open("my_thumb.png"), null);

然后将drawable转换为位图并获取字节数组:)

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM