文档:查看pdf文档,使用第三方android-pdf-viewer

柯立果
2023-12-01

使用pdfview第三方控件

学习网址:
1.依赖库原址------------GitHub - barteksc/AndroidPdfViewer: Android view for displaying PDFs rendered with PdfiumAndroid https://github.com/barteksc/AndroidPdfViewer
2.基础的使用方法------------第五章 Android加载PDF文件的使用 - 简书 https://www.jianshu.com/p/608b270a1ab8
3.在线加载pdf文件------------(Android)不用下载,在线加载pdf文件 - CSDN博客 http://blog.csdn.net/zumpling/article/details/55252870

一.准备工作

添加依赖:

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

xml布局:

<com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdf_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

二.加载本地pdf文件:

currentPageIndex = 0;
 File pdfFile = new File("本地pdf文件绝对路径");
        if (pdfFile.exists()) {
            //pdfView.fromFile(pdfFile).pages(0).load();//默认显示第一张图片

            pdfView.fromFile(pdfFile)//设置pdf文件地址
                    .onLoad(new OnLoadCompleteListener() {
                        @Override
                        public void loadComplete(int nbPages) {
                            sumPageIndex = pdfView.getPageCount();
                            currentPageIndex = pdfView.getCurrentPage();
                        }
                    })
                    .onPageChange(new OnPageChangeListener() {
                        @Override
                        public void onPageChanged(int page, int pageCount) {
                            tvCurrentIndex.setText((page+1) + "/" + pageCount);
                            currentPageIndex = page;
                        }
                    })//设置翻页监听
                    .onRender(new OnRenderListener() {
                        @Override
                        public void onInitiallyRendered(int nbPages, float pageWidth, float pageHeight) {
                            pdfView.fitToWidth();
                        }
                    })//设置每一页适应屏幕宽,默认适应屏幕高
                    .swipeHorizontal(false)//设置不可水平滑动
                    .load();        
        }

跳转到指定页:

pdfView.jumpTo(currentPageIndex,true);

三.在线加载pdf文件

private Handler handler = new Handler();
new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(IpAddress.getUrl(pathStrings[currentFileIndex]));
                        HttpURLConnection connection = (HttpURLConnection)
                                url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setDoInput(true);
                        connection.setConnectTimeout(10000);
                        connection.setReadTimeout(10000);
                        connection.connect();
                        if (connection.getResponseCode() == 200) {
                            final InputStream is = connection.getInputStream();
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    pdfView.fromStream(is)
                                            .onPageChange(new OnPageChangeListener() {
                                                @Override
                                                public void onPageChanged(int page, int pageCount) {
                                                    tvCurrentIndex.setText("页数:" + (page+1) + "/" + pageCount);
                                                }
                                            })//设置翻页监听
                                            .onRender(new OnRenderListener() {
                                                @Override
                                                public void onInitiallyRendered(int nbPages, float pageWidth, float pageHeight) {
                                                    pdfView.fitToWidth();
                                                }
                                            })//设置每一页适应屏幕宽,默认适应屏幕高
                                            .swipeHorizontal(false)//设置不可水平滑动
                                            .onLoad(new OnLoadCompleteListener() {
                                                @Override
                                                public void loadComplete(int nbPages) {
                                                    
                                                }
                                            })
                                            .load();
                                }
                            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();                       
                    }
                }
            }).start();
 类似资料: