該項目的所有權最近發生了變化,我們正在積極尋找貢獻者,以使該項目恢復追踪。請訪問
AndroidPDFViewer 1.x可在AndroidPDFViewerv1 repo上獲得,可以獨立開發。版本1.x使用不同的引擎在畫布上繪製文檔,因此,如果您不喜歡2.x版本,請嘗試1.x。
用於在Android上顯示PDF文檔的庫,並具有animations
, gestures
, zoom
和double tap
支持。它基於用於解碼PDF文件的PDFiumandroid。在API 11(Android 3.0)及更高的工作上工作。根據Apache許可證2.0許可。
fitEachPage
選項Contants.PRELOAD_COUNT
用PRELOAD_OFFSET
PDFView#fitToWidth()
(沒有參數的變體)Configurator#invalidPageColor(int)
方法是無效的頁面OnRenderListener#onInitiallyRendered(int)
方法中刪除了頁面大小參數,因為文檔可能具有不同的頁面尺寸PDFView#setSwipeVertical()
方法添加到build.gradle :
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
或者,如果您想使用更穩定的版本:
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
圖書館可在Jcenter存儲庫中找到,可能很快就會在Maven Central。
如果您使用的是proguard,請添加以下規則到proguard配置文件:
-keep class com.shockwave.**
< com .github.barteksc.pdfviewer.PDFView
android : id = " @+id/pdfView "
android : layout_width = " match_parent "
android : layout_height = " match_parent " />
所有可默認值的可用選項:
pdfView . fromUri ( Uri )
or
pdfView . fromFile ( File )
or
pdfView . fromBytes ( byte [])
or
pdfView . fromStream ( InputStream ) // stream is written to bytearray - native code cannot use Java Streams
or
pdfView . fromSource ( DocumentSource )
or
pdfView . fromAsset ( String )
. pages ( 0 , 2 , 1 , 3 , 3 , 3 ) // all pages are displayed by default
. enableSwipe ( true ) // allows to block changing pages using swipe
. swipeHorizontal ( 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 )
. onLoad ( onLoadCompleteListener ) // called after document is loaded and starts to be rendered
. onPageChange ( onPageChangeListener )
. onPageScroll ( onPageScrollListener )
. onError ( onErrorListener )
. onPageError ( onPageErrorListener )
. onRender ( onRenderListener ) // called after document is rendered for the first time
// called on single tap, return true if handled, false to toggle scroll handle visibility
. onTap ( onTapListener )
. onLongPress ( onLongPressListener )
. enableAnnotationRendering ( false ) // render annotations (such as comments, colors or forms)
. password ( null )
. scrollHandle ( null )
. enableAntialiasing ( true ) // improve rendering a little bit on low-res screens
// spacing between pages in dp. To define spacing color, set view background
. spacing ( 0 )
. autoSpacing ( false ) // add dynamic spacing to fit each page on its own on the screen
. linkHandler ( DefaultLinkHandler )
. pageFitPolicy ( FitPolicy . WIDTH ) // mode to fit pages in the view
. fitEachPage ( false ) // fit each page to the view, else smaller pages are scaled relative to largest page.
. pageSnap ( false ) // snap pages to screen boundaries
. pageFling ( false ) // make a fling change only a single page like ViewPager
. nightMode ( false ) // toggle night mode
. load ();
pages
是可選的,它允許您根據需要過濾和訂購PDF的頁面滾動手柄是從1.x分支中替換滾動條的。
從2.1.0版本將pdfview放入RelativElayout中不需要使用scrollHandle ,您可以使用任何佈局。
要使用滾動句柄,只需使用Method Configurator#scrollHandle()
註冊它。此方法接受捲軸接口的實現。
使用AndroidPDFViewer運送了默認的實現,您可以將其與.scrollHandle(new DefaultScrollHandle(this))
一起使用。 DefaultScrollHandle放在右側(垂直滾動時)或底部(水平滾動時)。通過將構造函數與第二個參數( new DefaultScrollHandle(this, true)
)一起使用,可以將句柄放置在左上或頂部。
您還可以創建自定義滾動手柄,只需實現捲軸接口即可。所有方法都記錄在接口源上的Javadoc註釋。
版本2.3.0引入了文檔資源,它只是PDF文檔的提供商。每個提供商都會實現文檔程序界面。預定義的提供程序可在com.github.barteksc.pdfviewer.source軟件包中找到,可以用作創建自定義的示例。
預定義的提供商可以與速記方法一起使用:
pdfView.fromUri(Uri)
pdfView.fromFile(File)
pdfView.fromBytes(byte[])
pdfView.fromStream(InputStream)
pdfView.fromAsset(String)
自定義提供商可以與pdfView.fromSource(DocumentSource)
方法一起使用。
版本3.0.0引入了對PDF文檔中鏈接的支持。默認情況下,使用DefaultLinkHandler ,然後單擊同一文檔中引用頁面的鏈接,原因是跳轉到目標頁面,然後單擊針對某些URI導致在默認應用程序中打開它的鏈接。
您還可以創建自定義鏈接處理程序,只需實現linkHandler接口,然後使用Configurator#linkHandler(LinkHandler)
方法設置它。查看DefaultLinkHandler來實現自定義行為。
由於版本3.0.0,圖書館以3種模式支持貼合頁面:
除了選定的策略外,每個頁面都具有相對於其他頁面的規模。
可以使用Configurator#pageFitPolicy(FitPolicy)
設置FIT策略。默認策略是寬度。
默認情況下,生成的位圖用RGB_565
格式壓縮,以減少內存消耗。可以使用pdfView.useBestQuality(true)
方法來強制使用ARGB_8888
渲染。
有三個縮放級別:最小(默認為1),中間(默認為1.75)和最大值(默認3)。在第一次雙擊時,視圖將縮放到中間級別,第二至最高級別,並在第三次返回到最小級別。如果您在中間和最大級別之間,請雙敲擊會導致變焦到最大等等。
可以使用以下方法更改縮放級別:
void setMinZoom ( float zoom );
void setMidZoom ( float zoom );
void setMaxZoom ( float zoom );
Android PDFViewer取決於PDFiumAndroid,該PDFiumandroid是許多架構的本機庫(幾乎16 MB)的集合。 APK必須包含所有這些庫才能在市場上可用的每個設備上運行。幸運的是,Google Play允許我們上傳多個APK,例如每個體系結構一個。有關於將您的應用程序自動分為多個APK的好文章,可在此處找到。最重要的部分是用APK分割改善多個APK的創建和版本碼處理,但整篇文章值得一讀。您只需要在您的應用程序中執行此操作,無需分叉pdfiumandroid左右即可。
下載文件是長期運行的過程,必須了解活動生命週期,必須支持某些配置,數據清理和緩存,因此創建此類模塊可能最終成為新庫。
您必須存儲當前頁碼,然後使用pdfView.defaultPage(page)
設置它,請參閱示例應用程序
使用FitPolicy.WIDTH
策略或在要在文檔中適合具有不同頁面尺寸的文檔中的所需頁面時添加以下片段:
Configurator . onRender ( new OnRenderListener () {
@ Override
public void onInitiallyRendered ( int pages , float pageWidth , float pageHeight ) {
pdfView . fitToWidth ( pageIndex );
}
});
您可以使用以下設置的組合來獲得類似於ViewPager的滾動和彈性行為:
. swipeHorizontal ( true )
. pageSnap ( true )
. autoSpacing ( true )
. pageFling ( true )
如果您對使此lib變得更好,請寫信,創建問題或編寫一些代碼並發送拉動請求。
在瓊·扎帕塔(Joan Zapata)的Android-pdfview的幫助下創建
Copyright 2017 Bartosz Schiller
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.