image - Generate Thumbnail of Pdf in Android -
i want generate image(thumbnail) pdf file done whatsapp shown below
i have tried
- pdfbox (https://github.com/tomroush/pdfbox-android)
- tika (compile 'org.apache.tika:tika-parsers:1.11')
- androidpdfviewer (https://github.com/barteksc/androidpdfviewer)
and still unable find way generate image pdf.
pdfbox:
there github issue deals problem (https://github.com/tomroush/pdfbox-android/issues/3) still unresolved.
note: able extract image pdf using pdfbox
androidpdfviewer:
github issue (https://github.com/barteksc/androidpdfviewer/issues/49)
use pdfiumandroid mentioned barteksc here...
sample code generating pdf thumb
//pdfiumandroid (https://github.com/barteksc/pdfiumandroid) //https://github.com/barteksc/androidpdfviewer/issues/49 void generateimagefrompdf(uri pdfuri) { int pagenumber = 0; pdfiumcore pdfiumcore = new pdfiumcore(this); try { //http://www.programcreek.com/java-api-examples/index.php?api=android.os.parcelfiledescriptor parcelfiledescriptor fd = getcontentresolver().openfiledescriptor(pdfuri, "r"); pdfdocument pdfdocument = pdfiumcore.newdocument(fd); pdfiumcore.openpage(pdfdocument, pagenumber); int width = pdfiumcore.getpagewidthpoint(pdfdocument, pagenumber); int height = pdfiumcore.getpageheightpoint(pdfdocument, pagenumber); bitmap bmp = bitmap.createbitmap(width, height, bitmap.config.argb_8888); pdfiumcore.renderpagebitmap(pdfdocument, bmp, pagenumber, 0, 0, width, height); saveimage(bmp); pdfiumcore.closedocument(pdfdocument); // important! } catch(exception e) { //todo exception } } public final static string folder = environment.getexternalstoragedirectory() + "/pdf"; private void saveimage(bitmap bmp) { fileoutputstream out = null; try { file folder = new file(folder); if(!folder.exists()) folder.mkdirs(); file file = new file(folder, "pdf.png"); out = new fileoutputstream(file); bmp.compress(bitmap.compressformat.png, 100, out); // bmp bitmap instance } catch (exception e) { //todo exception } { try { if (out != null) out.close(); } catch (exception e) { //todo exception } } }
update:
include library in build.gradle
compile 'com.github.barteksc:pdfium-android:1.4.0'
for generating image of pdf page:
call method generateimagefrompdf(uri) passing pdf uri stored in storage.
the method generate pdf.png in pdf folder of storage.
Comments
Post a Comment