我为 pdf 文件创建了 android 应用程序,但它只显示简单视图。实际上我想在我的 android pdf 视图中添加翻页。我的 PDFViewer 代码是:并且我还想添加搜索页码的功能(通过搜索转到特定页面)。
MainActivity.java
package com.example.mnaum.quranasaanurdutrjuma;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
import org.apache.commons.io.IOUtils;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView=(PDFView)findViewById(R.id.pdfView);
pdfView.fromAsset("parah1.pdf").load();
new RetrievePDFStream().execute("http://ancestralauthor.com/download/sample.pdf");
}
class RetrievePDFStream extends AsyncTask<String,Void,InputStream>{
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream=null;
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
if (urlConnection.getResponseCode()==200){
inputStream=new BufferedInputStream(urlConnection.getInputStream());
}
}
catch (IOException e){
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}
}
这是我的主要活动的 XML 文件
activity_main.xml
<?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">
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pdfView"/>
</RelativeLayout>
构建.gradle
compile 'com.github.barteksc:android-pdf-viewer:2.3.0'
compile 'org.apache.commons:commons-io:1.3.2'