最近由于项目需要,大致了解了一下关于如何在Android端显示PDF文件的内容,因此在这里进行一下小小的总结,方便今后开发。我目前了解到并成功实现的有三种开源框架分别是:Android PdfViewer、MUpdf以及iText,
Android PdfViewer
MUpdf
iText
其中Android PdfViewer、MUpdf是显示PDF文件中所有文件,而iText则主要是方便用于解析PDF文件中的文本信息,这里主要介绍Android PdfViewer的使用方法:
Android PdfViewer的使用方法最是简单,直接添加依赖即可,而且显示效果也相当好,但功能没有MUpdf全面,且解析速度比MUpdf稍稍慢一点点,特别是在解析大文件的时候更加明显,但就普通的PDF文件显示的话还是完全能够胜任的。
GitHub地址:https://github.com/barteksc/AndroidPdfViewer
其实在这个GitHub地址里已经有详细的说明了,不必下载直接添加依赖
compile 'com.github.barteksc:android-pdf-viewer:2.7.0-beta.1'
添加完依赖后就可以直接使用了,简直不要太方便,下面是布局文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="www.alphamap.cn.test02.MainActivity">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent">
com.github.barteksc.pdfviewer.PDFView>
<Button
android:onClick="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开pdf" />
RelativeLayout>
然后是MainActivity里的,记得要.load():
pdfView.fromFile(new File("文件路径")).load();
或者按照GitHub里介绍的:
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)
.onRender(onRenderListener) // called after document is rendered for the first time
.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)
.load();
记得添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如果系统是Android6.0版本以上的话不要忘了获取动态权限,附带获取动态权限代码:
package www.alphamap.cn.test02;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.File;
public class MainActivity extends Activity {
private PDFView pdfView;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取动态权限
getPermission();
}
public void button(View view){
pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/AlphaMap/58号文.pdf")).load();
}
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* 获取动态权限
*/
public void getPermission(){
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.d("yx","get permission");
ActivityCompat.requestPermissions(MainActivity.this,
PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
Log.d("yx","get permission2");
ActivityCompat.requestPermissions(MainActivity.this,
PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
Log.d("yx","wait for PERMISSION_GRANTED");
while (( ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {
}
Log.d("yx","wait for PERMISSION_GRANTED finish");
}
}