How to enable pinch zoom on a web view loading a url with pdf(Android)?

Pinch zoom works fine on a webView that loads a regular url, if I enable the settings as below,

myWebView.getSettings().setBuiltInZoomControls(true); //this works fine.

The issue is,

I have a webview that loads a url with PDF. I am using google docs viewer to load this url to avoid downloading the PDF to the device. Something like,

'"http://docs.google.com/gview?embedded=true&url=http://www.abc.com/xxxxxyyyyyxz.pdf"'

loading this URL on the web view shows the pdf with built in zoom option as, button click or double tap to zoom in and button click for zoom out.

This is not a great user experience as compared to pinch zoom.

So far, I have tried many solutions, nothing worked yet. I have tried the following,

  • android-pinch open source library, it works for maps and web view that loads a url with image.
  • This tutorial was helpful but couldn't get it working for my webView.
  • if anyone has anyother useful information for me to tryout, please share.


    PDFs are notoriously difficult to interact with in Android. You could:

    1.) Roll your own implementation of WebView that supports PDF

    I don't recommend doing it this way. My typical logic is that if Google hasn't done it for a sufficiently long period of time, then it's either very difficult to do, or not very battery efficient. In any case, you may have to get your hands dirty with the Android NDK in order to make your implementation fast and efficient.

    http://gurushya.com/customizing-android-webview/

    2.) Convert your PDF to a series of bitmaps

    For this option, you would need to parse the content of the webpage to find the necessary pdf, convert each slide of your PDF to a bitmap, and display each bitmap in a simple ImageSlider. I haven't had the time to try this method, but it seems like it may work (probably not very quickly though). This stackoverflow answer may help:

    Need help to convert a Pdf page into Bitmap in Android Java

    3.) Follow Convention - Recommended

    This is likely your best option. Using Google Docs is somewhat of a hack. When you pass it a PDF, you're essentially opening up a javascript PDF reader with lousy mobile support. The typical way of handling PDF's in Android is by checking if the user has a PDF reader installed and opening the PDF with that if they do. That PDF reader will more than likely have a pinch to zoom functionality. Android is likely implemented this way for performance reasons and to work across a broader scope of devices.

    Keep in mind that while your average iOS user may not be as used to this; Android users are quite comfortable opening files in other apps. Opening your PDF in a PDF reader gives the user the ability to choose which PDF reader to use. By doing so, you give the reader even more options than just pinch to zoom (such as indexed search, highlighting, quick navigation UI) depending on which app they have installed. If they don't have a PDF reader? Suggest a free one and link them to your Android store of choice!

    Render a PDF file using Java on Android


    Old question, but here's a solution I found.

            WebView mWebView = (WebView) findViewById(R.id.webView);
            mWebView.getSettings().setJavaScriptEnabled(true);
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
    

    Hope this helps!


    Well instead of using a WebView to show your PDF to the user, maybe using a PDF-View would make more sense. Here's a library I found after googling for it, but I never worked with it so I can't tell how well it works: https://code.google.com/p/apv/

    Edit: Here's another answer with a list of PDF-libs: https://stackoverflow.com/a/6729135/1219113

    链接地址: http://www.djcxy.com/p/46766.html

    上一篇: 使用android intent从服务器打开pdf文件

    下一篇: 如何在Web视图上启用缩放以使用PDF(Android)加载URL?