PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label itext. Show all posts
Showing posts with label itext. Show all posts

Friday, July 29, 2022

[FIXED] How to convert images to pdf using iText

 July 29, 2022     android, image, itext, pdf     No comments   

Issue

Can anyone please show me how to use iText API in android to convert images captured which is already in gallery and save it as pdf document. Help needed as soon as possible. Main objective is to create android application whereby able to get multiple images from the gallery and save it as pdf format.


Solution

To get the images from gallery u'll have to start the startActivityForResult and in onActivityResult u can store the image in the pdf file:-

First call the gallery intent as:-

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

Then in onActivityResult get the bitmap and write it in the 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;
            }  
          }  

    }

Hope this helps..



Answered By - bakriOnFire
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing