PdfViewPager
Android widget to display PDF documents in your Activities or Fragments.
Important note: PDFViewPager uses PdfRenderer class, which works only on API 21 or higher. See Official doc for details.
If you are targeting pre-Lollipop devices, have a look at the legacy sample
Installation
Add this line in your app/build.gradle
compile 'es.voghdev.pdfviewpager:library:1.0.3'
Usage
Use PDFViewPager class to load PDF files from assets or SD card
1.- Copy your assets to cache directory if your PDF is located on assets directory
CopyAsset copyAsset = new CopyAssetThreadImpl(context, new Handler());
copyAsset.copy(asset, new File(getCacheDir(), "sample.pdf").getAbsolutePath());
2a.- Create your PDFViewPager passing your PDF file, located in assets
pdfViewPager = new PDFViewPager(this, "sample.pdf");
2b.- Or directly, declare it on your XML layout
<es.voghdev.pdfviewpager.library.PDFViewPager
android:id="@+id/pdfViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:assetFileName="sample.pdf"/>
It will automatically have zooming and panning capability
3.- Release adapter in onDestroy
@Override
protected void onDestroy() {
super.onDestroy();
((PDFPagerAdapter) pdfViewPager.getAdapter()).close();
}
PDF's on SD card
1.- Create a PDFViewPager object, passing the file location in your SD card
PDFViewPager pdfViewPager = new PDFViewPager(context, getPdfPathOnSDCard());
protected String getPdfPathOnSDCard() {
File f = new File(getExternalFilesDir("pdf"), "adobe.pdf");
return f.getAbsolutePath();
}
2.- Don't forget to release the adapter in onDestroy
@Override
protected void onDestroy() {
super.onDestroy();
((PDFPagerAdapter) pdfViewPager.getAdapter()).close();
}
Remote PDF's from a URL
1.- Add INTERNET
, READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
permissions on your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2.- Make your Activity or Fragment implement DownloadFile.Listener
public class RemotePDFActivity extends AppCompatActivity implements DownloadFile.Listener {
3.- Create a RemotePDFViewPager object
String url = "http://www.cals.uidaho.edu/edComm/curricula/CustRel_curriculum/content/sample.pdf";
RemotePDFViewPager remotePDFViewPager =
new RemotePDFViewPager(context, url, this);
4.- Configure the corresponding callbacks and they will be called on each situation.
@Override
public void onSuccess(String url, String destinationPath) {
// That's the positive case. PDF Download went fine
adapter = new PDFPagerAdapter(this, "AdobeXMLFormsSamples.pdf");
remotePDFViewPager.setAdapter(adapter);
setContentView(remotePDFViewPager);
}
@Override
public void onFailure(Exception e) {
// This will be called if download fails
}
@Override
public void onProgressUpdate(int progress, int total) {
// You will get download progress here
// Always on UI Thread so feel free to update your views here
}
5.- Don't forget to close adapter in onDestroy to release all resources
@Override
protected void onDestroy() {
super.onDestroy();
adapter.close();
}
Usage in Kotlin
As you might figure out, the library is fully usable in Kotlin programming language.
Just import the library as a gradle dependency as you would do in Java.
TODOs
- [X] Make initial Pdf scale setable by code (requested by various users on issues)
- [X] Load PDF documents from SD card
- [X] Make PDF documents zoomable with pinch and double tap (two approaches, ImageViewZoom and photoview)
- [X] Unify all features in only one PDFViewPager and PDFPagerAdapter class
- [X] Support API Levels under 21, by downloading PDF and invoking system native intent.
- [X] UI tests
- [X] Add checkstyle, refactor & improve code quality
- [ ] Boost PDF opening & rendering performance using an asnychronous load