2013-05-08 82 views
-1

任何人都可以请示范我如何使用iText API在android中转换已经在图库中的图像并将其保存为pdf文档。尽快提供帮助。主要目标是创建android应用程序,从而可以从库中获取多个图像并将其保存为pdf格式。如何将图像转换为PDF使用iText

+0

http://stackoverflow.com/questions/5389994/convert-image-to-pdf-in-android – 2013-05-08 10:23:22

+0

http://stackoverflow.com/questions/15742125/getting-image-from-drawable-and-adding -to-pdf-using-itext – 2013-05-08 10:24:02

回答

1

要想从图库中的图片日子会把必须启动startActivityForResult和onActivityResultü可以存储在PDF文件中的图像: -

第一步拨打画廊意向为: -

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE); 

然后在onActivityResult得到位图,并把它写在PDF

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 


     if (resultCode == RESULT_OK) { 


     switch(requestCode){  

      case SELECT_PICTURE: 
        Uri selectedImageUri = data.getData(); 
        String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
        Cursor cursor = getContentResolver().query(selectedImageUri,filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String picturePath = cursor.getString(columnIndex); 
        cursor.close(); 
        Bitmap bmp = BitmapFactory.decodeFile(picturePath); 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 

       Document document = new Document(); 
       File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf"); 
       PdfWriter.getInstance(document,new FileOutputStream(f)); 
       document.open(); 
       document.add(new Paragraph("Simple Image")); 

       Image image = Image.getInstance(stream.toByteArray()); 
       document.add(image); 
       document.close(); 
       break; 
      } 
      } 

    } 

希望这有助于..

+0

谢谢你的解决方案。 bttw这行代码是干什么的? b.compress(Bitmap.CompressFormat.PNG,100,stream); 什么是b? – chai 2013-05-08 11:20:27

+0

PDF image质量很差任何建议 – 2015-10-19 11:05:47

0

由于我无法对bakriOnFire的回答发表评论,因此我必须针对该主题撰写答案。

谢谢你的解决方案。 bttw是什么是这行代码做b.compress(Bitmap.CompressFormat.PNG,100,流);什么是B? - 柴5月8日在'13 11:20

代码林乐应该是:

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

位图是越来越使用PNG编码压缩并写入到一个ByteArrayOutputStream。 这是必需的,因为Image.getInstance()只能处理ByteArrays。

+0

感谢您的更新..编辑我的答案.. – bakriOnFire 2015-10-19 12:19:58