Android PDF开发:android-pdfview

来源:互联网 发布:类似photosynth的软件 编辑:程序博客网 时间:2024/06/06 05:10
Android PDF开发:android-pdfview


Android平台自身没有直接可以阅读和处理pdf的方案,在github上面有一个第三方开源的pdf开发SDK,其主页地址是:

https://github.com/JoanZapata/android-pdfview 

android-pdfview使用比较简单,关键的地方是PDFView,将PDFView作为像Android的ImageView或者TextView一样写进xml布局文件:
<FrameLayout 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" >    <com.joanzapata.pdfview.PDFView        android:id="@+id/pdfView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></FrameLayout>



然后在Java上层代码直接加载pdf文件资源装载进去即可:

package zhangphil.pdfview;import com.joanzapata.pdfview.PDFView;import com.joanzapata.pdfview.listener.OnPageChangeListener;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);PDFView pdfView = (PDFView) findViewById(R.id.pdfView);// 在我这个测试例子中,事先准备一个叫做sample.pdf的pdf大文件放到assets目录下。// 从assets文件目录下读取名为 sample.pdf的文件,缺省把该pdf定位到第一页。pdfView.fromAsset("sample.pdf").defaultPage(1).onPageChange(new OnPageChangeListener() {@Overridepublic void onPageChanged(int page, int pageCount) {// 当用户在翻页时候将回调。Toast.makeText(getApplicationContext(), page + " / " + pageCount, Toast.LENGTH_SHORT).show();}}).load();}}



我把该第三方开源库整理成Eclipse下可用的lib,上传到github上,地址链接:
https://github.com/zhangphil/android-pdfview-lib-for-eclipse 
使用时候,下载该lib,导入到eclipse作为lib,然后在项目中直接引用即可。
0 0