How Can I Display A Pdf File In An Android Imageview
Is it possible to display huge pdf files in an Android view? I just need to be able to display it, with pinch and zoom and other normal features that are implemented in ImageViews
Solution 1:
There are three open source libraries, which combined, will do what you want. All are licensed with Apache V2 (or the Beer-Ware License)!
Note that V21 or above is required. But you can use an if-statement and an Intent as fallback, if required.
- Subsampling-scale-image-view: "A custom image view for Android, designed for photo galleries and displaying huge images"
- Subsampling-pdf-decoder: "A pdf decoder library for the subsampling scale image view"
- VerticalViewPager: This is optional, but i think you want to scroll up and down and not left and right.
The example how to implement this, is given in the sample of the decoder library.
1.) Gradle dependencies
repositories {
mavenCentral()
jcenter()
//decoder library, will soon be on jcenter
maven { url 'https://dl.bintray.com/n42/maven'}
}
dependencies {
compile'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'compile'com.github.castorflex.verticalviewpager:library:19.0.1'compile'de.number42:subsampling-pdf-decoder:0.1.0@aar'
}
2.) Implement a PagerAdapter, you can use this one
publicclassPDFPagerAdapterextendsPagerAdapter{
/**
* context for the view
*/private Context context;
/**
* pdf file to show
*/private File file;
/**
* file descriptor of the PDF
*/private ParcelFileDescriptor mFileDescriptor;
/**
* this scale sets the size of the {@link PdfRenderer.Page} in the {@link
* PDFRegionDecoder}.
* Since it rescales the picture, it also sets the possible zoom level.
*/privatefloat scale;
/**
* this renderer is only used to count the pages
*/private PdfRenderer renderer;
/**
* @param file the pdf file
*/public PDFPagerAdapter(Context context, File file) {
super();
this.context = context;
this.file = file;
this.scale = 8;
try {
mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
renderer = new PdfRenderer(mFileDescriptor);
} catch (IOException e) {
thrownewRuntimeException(e);
}
}
/**
* Instantiate an item. Therefor a {@link SubsamplingScaleImageView} with special decoders is
* initialized and rendered.
*
* @param container isn't used here
* @param position the current pdf page position
*/publicObject instantiateItem(ViewGroup container, int position) {
SubsamplingScaleImageView imageView = new SubsamplingScaleImageView(context);
// the smaller this number, the smaller the chance to get an "outOfMemoryException"// still, values lower than 100 really do affect the quality of the pdf pictureint minimumTileDpi = 120;
imageView.setMinimumTileDpi(minimumTileDpi);
//sets the PDFDecoder for the imageView
imageView.setBitmapDecoderFactory(() -> new PDFDecoder(position, file, scale));
//sets the PDFRegionDecoder for the imageView
imageView.setRegionDecoderFactory(() -> new PDFRegionDecoder(position, file, scale));
ImageSource source = ImageSource.uri(file.getAbsolutePath());
imageView.setImage(source);
container.addView(imageView);
return imageView;
}
/**
* gets the pdf site count
*
* @return pdf site count
*/publicint getCount() {
return renderer.getPageCount();
}
@Override publicvoid destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
@Override publicboolean isViewFromObject(View view, Objectobject) {
return view == object;
}
}
3.) Initiate and set the adapter for the VerticalViewPager
VerticalViewPagerpager= (VerticalViewPager) findViewById(R.id.pager);
FilepdfFile= ....
PDFPagerAdapterpagerAdapter=newPDFPagerAdapter(this,pdfFile);
pager.setAdapter(pagerAdapter);
Solution 2:
You can use webview for this and it is easy to implement. Do it like this
@SuppressLint("NewApi")
privatevoidstartWebView(String url) {
// Create new webview Client to show progress dialog// When opening a url or click on link// Javascript inabled on webview
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setDisplayZoomControls(false);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebChromeClient(newWebChromeClient());
// web.getSettings().setPluginState(WebSettings.PluginState.ON);
web.setWebViewClient(newWebViewClient() {
ProgressDialog progressDialog;
// If you will not use this method url links are opeen in new brower// not in webviewpublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
returntrue;
}
@SuppressWarnings("deprecation")
@OverridepublicvoidonReceivedError(WebView view, int errorCode, String description, final String failingUrl) {
web.loadUrl("about:blank");
}
// Show loader on url loadpublicvoidonLoadResource(WebView view, String url) {
/* if (progressDialog == null) {
// in standard case YourActivity.this
if (getActivity() != null) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}*/
}
publicvoidonPageFinished(WebView view, String url) {
try {
setProgressBar(false);
} catch (Exception e) {
}
}
});
// Load url in webview
web.loadUrl(url);
}
Post a Comment for "How Can I Display A Pdf File In An Android Imageview"