如何将多个图像保存为pdf文件

时间:2016-08-22 18:06:13

标签: android

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap mImageBitmap = (Bitmap) extras.get("data");

            // img.setImageBitmap(mImageBitmap);

            String fpath = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/sample.pdf";
            File file = new File(fpath);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            Document document = new Document();

            try {
                PdfWriter.getInstance(document, new FileOutputStream(fpath));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (DocumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            document.open();


                ByteArrayOutputStream stream = new ByteArrayOutputStream();
              //  mImageBitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), imagesf[i]);
                mImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

                try {
                    myImg = Image.getInstance(stream.toByteArray());
                } catch (BadElementException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                myImg.setAlignment(Image.MIDDLE);


            try {
                document.add(myImg);
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            document.close();`enter code here`

        }
    }

上面的代码工作正常,可以捕获并保存一个图像到pdf文件,但我的要求是我需要存储4到5个图像捕获,这将首先存储在SD卡中然后到pdf或直接存储在pdf中任何解决方案请帮助。

提前致谢。

0 个答案:

没有答案