2013-05-08 91 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