PDF view Android App Create in 4 minutes || PDF Show App || for beginners #pdf #android #pdfview
Android PdfViewer
AndroidPdfViewer 1.x is available on AndroidPdfViewerV1 repo, where can be developed independently. Version 1.x uses different engine for drawing document on canvas, so if you don't like 2.x version, try 1.x.
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 (Android 3.0) and higher. Licensed under Apache License 2.0.
What's new in 3.2.0-beta.1?
Merge PR #714 with optimized page load
Merge PR #776 with fix for max & min zoom level
Merge PR #722 with fix for showing right position when view size changed
Merge PR #703 with fix for too many threads
Merge PR #702 with fix for memory leak
Merge PR #689 with possibility to disable long click
Merge PR #628 with fix for hiding scroll handle
Merge PR #627 with fitEachPage option
Merge PR #638 and #406 with fixed NPE
Merge PR #780 with README fix
Update compile SDK and support library to 28
Update Gradle and Gradle Plugin
Changes in 3.0 API
Replaced Contants.PRELOAD_COUNT with PRELOAD_OFFSET
Removed PDFView#fitToWidth() (variant without arguments)
Removed Configurator#invalidPageColor(int) method as invalid pages are not rendered
Removed page size parameters from OnRenderListener#onInitiallyRendered(int) method, as document may have different page sizes
Removed PDFView#setSwipeVertical() method
Installation
Add to build.gradle:
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
or if you want to use more stable version:
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
Library is available in jcenter repository, probably it'll be in Maven Central soon.
ProGuard
If you are using ProGuard, add following rule to proguard config file:
-keep class com.shockwave.**
Include PDFView in your layout
com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
Load a PDF file
All available options with default values:
pdfView.fromUri(Uri)
or
pdfView.fromFile(File)
or
png
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);
Possible questions
Why resulting apk is so big?
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.
Why I cannot open PDF from URL?
Downloading files is long running process which must be aware of Activity lifecycle, must support some configuration, data cleanup and caching, so creating such module will probably end up as new library.
How can I show last opened page after configuration change?
You have to store current page number and then set it with pdfView.defaultPage(page), refer to sample app
How can I fit document to screen width (eg. on orientation change)?
Use FitPolicy.WIDTH policy or add following snippet when you want to fit desired page in document with different () {
@Overridees, float pageWidth, float pageHeight) {
pdfView.fitToWidth(pageIndex);
Тэги:
##android ##pdf