How to convert a PDF page to an image in Android?

后端 未结 8 761
说谎
说谎 2020-12-07 19:33

All I need to do is take a (locally saved) PDF-document and convert one or all of it\'s pages to image format like JPG or PNG.

I\'ve tr

8条回答
  •  無奈伤痛
    2020-12-07 19:56

    I will say you a simple trick not a complete solution.Once if you successfully rendered the pdf page you will get its bitmap from screen as follow

    View view = MuPDFActivity.this.getWindow().getDecorView();
    if (false == view.isDrawingCacheEnabled()) {
        view.setDrawingCacheEnabled(true);
    }
    Bitmap bitmap = view.getDrawingCache();
    

    Then you can save this bitmap, that is your pdf page as image in locally

    try {
        new File(Environment.getExternalStorageDirectory()+"/PDF Reader").mkdirs();
        File outputFile = new File(Environment.getExternalStorageDirectory()+"/PDF Reader", System.currentTimeMillis()+"_pdf.jpg");
        FileOutputStream outputStream = new FileOutputStream(outputFile);
    
        // a bit long running
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.close();
    } catch (IOException e) {
        Log.e("During IMAGE formation", e.toString());
    }
    

    that's all, hope you help this.

提交回复
热议问题