1. AndroidPdfViewer
导入AndroidPdfViewer
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
在repositories
里面添加库
maven { url 'https://jcenter.bintray.com' }
布局文件引用PDFView
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdf_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PdfViewer
为我们提供了以下几种读取文件的方法:
pdfView.fromUri(Uri)
pdfView.fromFile(File)
pdfView.fromBytes(byte[])
pdfView.fromStream(InputStream)
pdfView.fromSource(DocumentSource)
pdfView.fromAsset(String)
Configurator
主要方法
pages(int... pageNumbers) // 指定显示页面,默认全部显示
defaultPage(int defaultPage) // 默认实现页面
enableSwipe(boolean enableSwipe) // 控制屏幕滑动,默认为true能够滑动
enableDoubletap(boolean enableDoubletap) // 禁用双击,默认为true,可以双击
swipeHorizontal(boolean swipeHorizontal) // 开启横向滑动,默认竖向
enableAnnotationRendering(boolean annotationRendering) // 支持电子签
spacing(int spacing) // 页面之间空隙,单位是dp
onLoad(OnLoadCompleteListener onLoadCompleteListener) // 加载结束,开始绘制
onPageScroll(OnPageScrollListener onPageScrollListener) // 监听页面滚动
onPageChange(OnPageChangeListener onPageChangeListener) // 监听页面变化
load() // 最后调用,使配置生效
PDFView
其他方法
jumpTo(int page, boolean withAnimation) // 跳转到指定页面
jumpTo(int page)
getPageCount() // 获取总页面数
getCurrentPage() // 获取当前页面
enableAntialiasing(boolean enableAntialiasing) // 改善渲染效果,默认是false
useBestQuality(boolean bestQuality) // 提高pdf清晰度,把图片格式从RGB_565提高到ARGB_8888,默认是false
canScrollVertically(int direction) // 是否可以纵向滚动,direction大于0,向下滚动,direction小于0,向上滚动
canScrollHorizontally(int direction) // 是否可以横向滚动,direction大于0,向右滚动,direction小于0,向左滚动
github地址: GitHub - barteksc/AndroidPdfViewer: Android view for displaying PDFs rendered with PdfiumAndroid