4

I am working with pdf files, I want to implement page viewer to my pdf file. My idea is to convert pdf file into bmp images, and then to use viewPager. but I am stuck in converting pdf to bitmap. Any suggestions?

2

2 Answers 2

0

Include dependencies in your gradle

compile 'com.github.barteksc:android-pdf-viewer:2.8.1' 

Use following function to convert PDF page to bitmap image

private Bitmap generateImageFromPdf(String assetFileName, int pageNumber, int width, int height) { PdfiumCore pdfiumCore = new PdfiumCore(mActivity); try { File f = FileUtils.fileFromAsset(mActivity, assetFileName); ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY); 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, filena); pdfiumCore.closeDocument(pdfDocument); return bmp; } catch(Exception e) { //todo with exception } return null; } 
Sign up to request clarification or add additional context in comments.

2 Comments

not working brother.its return empty bitmap
how to pass the password for password protected pdf ?
0

this is a way to render pdf to image its take 20 to 25 seconds to render pdf file to image its working in android 10 , android 9 and all lower versions

private void generateImageFromPdf() { try { PDDocument doc=PDDocument.load(new File(fileurl)); PDFRenderer pdfRenderer = new PDFRenderer(doc); Bitmap bffim = pdfRenderer.renderImageWithDPI(0, 100, Bitmap.Config.RGB_565); String fileName = "image-" + 0 + ".png"; img.setImageBitmap(bffim); } catch (IOException e) { e.printStackTrace(); } } 

1 Comment

how to pass the password for password protected pdf ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.