1. 程式人生 > >Android 載入PDF檔案

Android 載入PDF檔案

      採用第三方開源庫解析,推薦AndroidPdfViewer(github:https://github.com/barteksc/AndroidPdfViewer),它是基於Google的VuDroid類庫來解碼PDF檔案,在APP內直接載入PDF檔案;

首先先新增依賴:

 compile 'com.github.barteksc:android-pdf-viewer:2.7.0'
其次在xml中宣告控制元件:
<com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
最後在程式碼中直接引用:
 pdfView.fromAsset("JavaScript.pdf")
//        pdfView.fromUri(Uri.parse(pdfUrl))
//        pdfView.fromFile(file)
//        pdfView.fromStream(stream)

                // all pages are displayed by default
                .pages(0,2,1,3,3,3) // 載入顯示的頁數,預設載入所有的頁數
                // allows to block changing pages using swipe
                .enableSwipe(true) //是否允許翻頁,預設是允許翻頁

                .swipeHorizontal(false) // true:水平翻頁  false:豎直翻頁

                .enableDoubletap(true)

                .defaultPage(0) //預設載入頁數
                // allows to draw something on the current page, usually visible in the middle of the screen
                .onDraw(onDrawListener) //繪圖監聽
                // allows to draw something on all pages, separately for every page. Called only for visible pages
                .onDrawAll(onDrawListener)
                // called after document is loaded and starts to be rendered
                .onLoad(onLoadCompleteListener)

                .onPageChange(onPageChangeListener)

                .onPageScroll(onPageScrollListener)

                .onError(onErrorListener)
                // called after document is rendered for the first time
                .onRender(onRenderListener)
                // called on single tap, return true if handled, false to toggle scroll handle visibility
                .onTap(onTapListener)
                // render annotations (such as comments, colors or forms)
                .enableAnnotationRendering(true)

                .password(null)//密碼

                .scrollHandle(null)//滾動處理
                // improve rendering a little bit on low-res screens
                .enableAntialiasing(true) //低解析度渲染
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)//頁與頁之間的距離

                .load();//載入


但是,有個弊端就是apk變大了!!!!!