Use pdfium library from AOSP
The demo app (for not modified lib) is here
Forked specially for use with PdfViewPager project.
-
Added method for rendering PDF page on bitmap
void renderPageBitmap(PdfDocument doc, Bitmap bitmap, int pageIndex, int startX, int startY, int drawSizeX, int drawSizeY);
-
Added methods to get width and height of page in points (1/72") (like in
PdfRenderer.Pageclass):int getPageWidthPoint(PdfDocument doc, int index);int getPageHeightPoint(PdfDocument doc, int index);
-
newDocument()throws IOException
API is fully compatible with original version, only additional methods were created.
- probably fixed bug when pdf should open as normal but was throwing exception
- added much more descriptive exception messages
Add to build.gradle:
compile 'com.github.barteksc:pdfium-android:1.0.3'
Library is available in jcenter and Maven Central repositories.
ImageView iv = (ImageView) findViewById(R.id.imageView);
FileDescriptor fd = ...;
int pageNum = 0;
PdfiumCore pdfiumCore = new PdfiumCore(context);
try {
PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
pdfiumCore.openPage(pdfDocument, pageNum);
int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
width, height);
iv.setImageBitmap(bitmap);
pdfiumCore.closeDocument(pdfDocument);
} catch(IOException ex) {
ex.printStackTrace();
}Go to PROJECT_PATH/src/main/jni and run command $ ndk-build.
This step may be executed only once, every future .aar build will use generated libs.