1

I am trying to convert bitmap image to PDF file. I am selecting image from gallery and want to convert that image file into pdf document. I am trying to do so but I am getting empty PDF file.

Below is my code:

public class Convert extends AppCompatActivity {

private static final int REQ_CODE = 99;
Bitmap bitmap = null;
ImageView image;
Button convert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_convert);

    image = findViewById(R.id.image);
    convert = findViewById(R.id.convert);

    openGallery();

    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // create a new document
            PdfDocument document = new PdfDocument();

            // create a page description
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(100, 100, 1).create();

            // start a page
            PdfDocument.Page page = document.startPage(pageInfo);

            // Draw the bitmap onto the page
            Canvas canvas = new Canvas();
            canvas.drawBitmap(bitmap, 0f, 0f, null);
            document.finishPage(page);

            String path = android.os.Environment.getExternalStorageDirectory().toString();
            try {
                document.writeTo(new FileOutputStream(path + "/example.pdf"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            document.close();
        }
    });
}

private void openGallery(){

    int pref = ScanConstants.OPEN_MEDIA;
    Intent in = new Intent(Convert.this, ScanActivity.class);
    in.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE,pref);
    startActivityForResult(in,REQ_CODE);
}

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

    if(requestCode == REQ_CODE && resultCode == Activity.RESULT_OK){

       Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
       try{
           bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
           if(uri != null){
               getContentResolver().delete(uri,null,null);
               image.setImageBitmap(bitmap);
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
     }
  }
} 

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Digvijay
  • 2,887
  • 3
  • 36
  • 86
  • 1
    Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Apr 10 '21 at 17:24

1 Answers1

3

I think you are not writing to PdfDocument.Page canvas object so this is what you should try,

// start a page
PdfDocument.Page page = document.startPage(pageInfo);

// Draw the bitmap onto the page
Canvas canvas = page.getCanvas();
canvas.drawBitmap(bitmap, 0f, 0f, null);
document.finishPage(page);
Nikhil Jain
  • 649
  • 5
  • 11