Android库中没有PDF支持。有什么方法可以在Android应用程序中呈现PDF文件吗?
自API Level 21(Lollipop)起,Android提供了PdfRenderer类:
// create a new renderer PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor()); // let us just render all pages final int pageCount = renderer.getPageCount(); for (int i = 0; i < pageCount; i++) { Page page = renderer.openPage(i); // say we render for showing on the screen page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY); // do stuff with the bitmap // close the page page.close(); } // close the renderer renderer.close();
对于较旧的API,我建议使用Android PdfViewer库,该库非常快速且易于使用,并根据Apache License 2.0授权:
pdfView.fromAsset(String) .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default .enableSwipe(true) .swipeHorizontal(false) .enableDoubletap(true) .defaultPage(0) .onDraw(onDrawListener) .onLoad(onLoadCompleteListener) .onPageChange(onPageChangeListener) .onPageScroll(onPageScrollListener) .onError(onErrorListener) .enableAnnotationRendering(false) .password(null) .scrollHandle(null) .load();