Library for displaying PDF documents on Android, with animations
, gestures
, zoom
and double tap
support.
It is based on PdfiumAndroid for decoding PDF files. Works on API 11 and higher.
Licensed under Apache License 2.0.
- update PdfiumAndroid to 1.4.0 with support for rendering annotations
- merge pull request by usef for rendering annotations
- update PdfiumAndroid to 1.3.1 with support for bookmarks, Table Of Contents and documents with password:
- added method
PDFView#getDocumentMeta()
, which returns document metadata - added method
PDFView#getTableOfContents()
, which returns whole tree of bookmarks in PDF document - added method
Configurator#password(String)
- added method
- added horizontal mode to ScrollBar - use
ScrollBar#setHorizontal(true)
orapp:sb_horizontal="true"
in XML - block interaction with
PDFView
when document is not loaded - prevent some exceptions - fix
PDFView
exceptions in layout preview (edit mode)
Next release is coming soon, it will introduce continuous scroll through whole document and some incompatibilities with current API (only few small).
Add to build.gradle:
compile 'com.github.barteksc:android-pdf-viewer:1.3.0'
Library is available in jcenter repository, probably it'll be in Maven Central soon.
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
All available options with default values:
pdfView.fromUri(Uri)
or
pdfView.fromFile(File)
or
pdfView.fromAsset(String)
.pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
.enableSwipe(true)
.enableDoubletap(true)
.swipeVertical(false)
.defaultPage(1)
.showMinimap(false)
.onDraw(onDrawListener)
.onLoad(onLoadCompleteListener)
.onPageChange(onPageChangeListener)
.onError(onErrorListener)
.enableAnnotationRendering(false)
.password(null)
.load();
enableSwipe
is optional, it allows you to block changing pages using swipepages
is optional, it allows you to filter and order the pages of the PDF as you needonDraw
is also optional, and allows you to draw something on a provided canvas, above the current page
Use ScrollBar class to place scrollbar view near PDFView
-
in layout XML (it's important that the parent view is RelativeLayout):
Vertically:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toLeftOf="@+id/scrollBar"/> <com.github.barteksc.pdfviewer.ScrollBar android:id="@+id/scrollBar" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>
Horizontally:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView" android:layout_width="match_parent" android:layout_height="match_parent"/> <com.github.barteksc.pdfviewer.ScrollBar android:id="@+id/scrollBar" android:layout_width="match_parent" android:layout_height="wrap_content" app:sb_horizontal="true" android:layout_alignParentBottom="true" /> </RelativeLayout>
-
in activity or fragment
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... PDFView pdfView = (PDFView) findViewById(R.id.pdfView); ScrollBar scrollBar = (ScrollBar) findViewById(R.id.scrollBar); pdfView.setScrollBar(scrollBar); }
scrollBar.setHorizontal(true);
orapp:sb_horizontal="true"
may be used to set ScrollBar in horizontal mode.
Scrollbar styling:
<com.github.barteksc.pdfviewpager.view.ScrollBar
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:sb_handlerColor="..." <!-- scrollbar handler color -->
app:sb_indicatorColor="..." <!-- background color of current page indicator -->
app:sb_indicatorTextColor="..." <!-- text color of current page indicator -->
app:sb_horizontal="true|false|reference" <!-- whether to set horizontal mode -->
android:background="..." <!-- scrollbar background -->
/>
ScrollBarPageIndicator is added to scrollbar automatically and is shown while dragging scrollbar handler, displaying number of page on current position. Its position is automatically calculated based on ScrollBar's position.
By default, generated bitmaps are compressed with RGB_565
format to reduce memory consumption.
Rendering with ARGB_8888
can be forced by using pdfView.useBestQuality(true)
method.
There are three zoom levels: min (default 1), mid (default 1.75) and max (default 3). On first double tap, view is zoomed to mid level, on second to max level, and on third returns to min level. If you are between mid and max levels, double tapping causes zooming to max and so on.
Zoom levels can be changed using following methods:
void setMinZoom(float zoom);
void setMidZoom(float zoom);
void setMaxZoom(float zoom);
Android PdfViewer depends on PdfiumAndroid, which is set of native libraries (almost 16 MB) for many architectures. Apk must contain all this libraries to run on every device available on market. Fortunately, Google Play allows us to upload multiple apks, e.g. one per every architecture. There is good article on automatically splitting your application into multiple apks, available here. Most important section is Improving multiple APKs creation and versionCode handling with APK Splits, but whole article is worth reading. You only need to do this in your application, no need for forking PdfiumAndroid or so.
If you have any suggestions on making this lib better, write me, create issue or write some code and send pull request.
Created with the help of android-pdfview by Joan Zapata
Copyright 2016 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.