PDF查看:Android 用PDFView实现PDF查看器
Posted zhangjin1120
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PDF查看:Android 用PDFView实现PDF查看器相关的知识,希望对你有一定的参考价值。
加载PDF的url链接,上效果:
android PDF 查看器Demo效果
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
- 网络权限
<uses-permission android:name="android.permission.INTERNET" />
- 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
- MainActivity.java测试代码:
public class MainActivity extends AppCompatActivity {
PDFView pdfView;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = findViewById(R.id.pdfView);
dialog = ProgressDialog.show(MainActivity.this, "", "正在加载PDF", true);
new RetrivePDFStream().execute("http://www.fao.fudan.edu.cn/_upload/article/files/f5/53/8b40af524563b9b60049899b2dd3/c9a205b4-4188-4af3-863e-bc4e56872e33.pdf");
}
class RetrivePDFStream extends AsyncTask<String, Void, InputStream> {
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL uri = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream)
.onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
dialog.dismiss();
}
})
.load();
}
}
}
以上是关于PDF查看:Android 用PDFView实现PDF查看器的主要内容,如果未能解决你的问题,请参考以下文章
为 PDFView 设置 RTL 滚动方向 - Android
在 iOS 上使用 PDFKit.PDFView 打开 PDF