1. Open Source Project Address:
Https://github.com/JoanZapata/android-pdfview
2. References
compile ‘com.joanzapata.pdfview:android-pdfview:[email protected]
‘
3. layout file
<?xml version="1.0"encoding="Utf-8"? ><relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:fitssystemwindows="true"Tools:context="zyj.com.myapplication.MainActivity"> <Com.joanzapata.pdfview.PDFView Android:id="@+id/pdfview"Android:layout_width="match_parent"Android:layout_height="match_parent"/></relativelayout>
Use
Package Zyj.com.myapplication;import Android.graphics.canvas;import android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.widget.toast;import Com.joanzapata.pdfview.PDFView; Import Com.joanzapata.pdfview.listener.ondrawlistener;import Com.joanzapata.pdfview.listener.onloadcompletelistener;import Com.joanzapata.pdfview.listener.onpagechangelistener;import Java.io.File; Public classMainactivity extends Appcompatactivity implements Onpagechangelistener, Onloadcompletelistener, Ondrawliste NER {PrivatePdfview Pdfview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Pdfview=(Pdfview) Findviewbyid (R.id.pdfview); //read PDF from assets directoryDisplayfromassets ("bao.pdf"); //reading a PDF from a fileDisplayfromfile (NewFile ("FileName")); } Private voiddisplayfromassets (String assetfilename) {pdfview.fromasset (assetfilename)//Set the PDF file address. Defaultpage (6)//set default to show page 1th. Onpagechange ( This)//set page Turn monitoring. OnLoad ( This)//Set Load monitoring. OnDraw ( This)//Drawing Monitoring. Showminimap (false)//when a PDF is zoomed in, does it generate a small map in the upper-right corner of the screen. swipevertical (false)//whether the page of a PDF document is a vertical page, the default is to scroll left and right. Enableswipe (true)//whether to allow page flipping, the default is to allow page flipping//. Pages (2, 3, 4, 5)//filter out 2, 3, 4, 5. Load (); } Private voiddisplayfromfile (file file) {pdfview.fromfile (file)//Set the PDF file address. Defaultpage (6)//set default to show page 1th. Onpagechange ( This)//set page Turn monitoring. OnLoad ( This)//Set Load monitoring. OnDraw ( This)//Drawing Monitoring. Showminimap (false)//when a PDF is zoomed in, does it generate a small map in the upper-right corner of the screen. swipevertical (false)//whether the page of a PDF document is a vertical page, the default is to scroll left and right. Enableswipe (true)//whether to allow page flipping, the default is to allow//. Pages (2, 3, 4, 5)//filter out 2, 3, 4, 5. Load (); } /** * Page Turn callback * @param page * @param pagecount*/@Override Public voidOnpagechanged (intPageintPageCount) {Toast.maketext (mainactivity). This,"page="+ page +"pagecount="+PageCount, Toast.length_short). Show (); } /** * Load Complete callback * @param nbpages total pages*/@Override Public voidLoadComplete (intnbpages) {Toast.maketext (mainactivity). This,"Load Complete"+nbpages, Toast.length_short). Show (); } @Override Public voidOnlayerdrawn (Canvas canvas,floatPageWidth,floatPageHeight,intdisplayedpage) { //Toast.maketext (mainactivity.this, "pagewidth=" + pagewidth + "//pageheight= "+ pageheight +" displayedpage= "+ Displayedpage, Toast.length_short). Show (); }}
4. Project Address (Android Studio Environment)
http://download.csdn.net/detail/yanzi2015/9341525
Android Display PDF File