该项目的所有权最近发生了变化,我们正在积极寻找贡献者,以使该项目恢复追踪。请访问
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.