Forum Xamarin.Forms

View PDF in WebView for Android below API level 19

YogeshPatil.2761YogeshPatil.2761 USMember ✭✭
edited April 2016 in Xamarin.Forms

I am following recipe to view PDF in Android app.
My Xamarin.Forms app supports Android API level 16 and onwards. As per docs:

API levels below 19 are not capable of loading the local PDF file by specifying a ?file= query string to the viewer URL.
Instead, modify the DEFAULT_URL variable in the Assets/pdfjs/web/viewer.js file to reference the local PDF file.

If I change PDF path in viewer.js file it will point to same PDF and wont be dynamic. I want to view PDF from my external storage.
And path to that file will be dynamic. Can anyone tell how to achieve goal?


  • markmunizmarkmuniz USUniversity ✭✭

    Were you able to get around this issue?

  • I have the same issue here. Has anyone found a solution?

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    Have you evaluated

  • Forgive me for late reply. I was not keeping eye on this thread hence missed. I resolved it long back with following logic.

    Copy "viewer.html" and "viewer.js" files to web folder, rename them to "viewerlow.html" and "viewerlow.js" resp. Note do not remove original .html and .js files.

    From "viewerlow.js" file remove below line
    document.addEventListener('DOMContentLoaded', webViewerLoad, true);
    And add following function,

    function webViewerLoadCustom(pdfFilePath) {
          DEFAULT_URL = pdfFilePath;

    From "viewerlow.html" file remove
    <script src="viewer.js"></script>
    and in that place add
    <script src="viewerlow.js"></script>

    In your Activity add similar code like below as per your requirements:

    void InitWebView()
        web = FindViewById<WebView>(Resource.Id.WebPdfView);
        myClient = new PdfViewWebClient(this);
        web.Settings.AllowFileAccessFromFileURLs = true;
        web.Settings.AllowUniversalAccessFromFileURLs = true;
        web.Settings.JavaScriptEnabled = true;
    void ViewLocalPdfFile()
        // Set pdf local path as query parameter.
        string pdfRootPath = worksheetRepoUtil.GetLocalPath(worksheet);
        string urlToLoad = HtmlViewerPath + "?file=" + pdfRootPath;
        // For API level 19 and below.
        if (DeviceUtil.IsAPILessThan19())
            urlToLoad = HtmlViewerPathLow;
    // Class used to register WebView events.
    class PdfViewWebClient : WebViewClient
        WebPdfViewerActivity activity;
        public PdfViewWebClient(WebPdfViewerActivity activity)
            this.activity = activity;
        public override void OnPageFinished(WebView web, string url)
            base.OnPageFinished(web, url);
            if (DeviceUtil.IsAPILessThan19())
                // Set pdf local path as query parameter.
                // Then view PDF from external storage.
                string pdfRootPath = activity.worksheetRepoUtil.GetLocalPath(activity.worksheet);
                web.LoadUrl(string.Format("javascript:webViewerLoadCustom('{0}')", pdfRootPath));
Sign In or Register to comment.