问题与添加图像,而在Android的创建动态全文 [英] Issue with Adding Image while Creating Dynamic Pdf in android

查看:96
本文介绍了问题与添加图像,而在Android的创建动态全文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的PDF使用动态低于code和同样使用iText库:

I created pdf dynamically using below code and also using iText library:

尝试{

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();
                PdfWriter.getInstance(document, fos);

                document.open();
                document.add(new Paragraph("Hello World"));
                document.add(new Paragraph(new Date().toString()));

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

比我取得976字节的PDF文件。
当我加入code使用低于code将图片添加到这个PDF文件:

Than i retrieve the pdf file of 976 byte. When i added code for adding image to this pdf file using below code:

    try {

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();
                PdfWriter.getInstance(document, fos);

                document.open();
                document.add(new Paragraph("Hello World, iText"));
                document.add(new Paragraph(new Date().toString()));


                Image image = Image.getInstance("ic_launcher.png");
                document.add(image);

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

比我找回0字节的PDF文件。

Than i retrieve the pdf file of 0 byte.

我不知道是什么问题,如果你有与此相关的,不是请与我分享任何想法。谢谢。

i dont know what is issue, If you have any idea related to it, than please share with me. thanks.

推荐答案

最后,我添加图像到PDF使用低于code:

Finally I added image to pdf Using below code:

尝试{

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();

                PdfWriter.getInstance(document, fos);

                InputStream ims = getAssets().open("ic_launcher.png");
                Bitmap bmp = BitmapFactory.decodeStream(ims);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                Image image = Image.getInstance(stream.toByteArray());

                document.open();
                document.add(new Paragraph("Hello World"));
                document.add(new Paragraph(new Date().toString()));
                document.add(image);

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

使用此code,希望这会帮助你。

Use this code, hope this will help you.

这篇关于问题与添加图像,而在Android的创建动态全文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆