How to render a PDF file in Android

后端 未结 9 1057
猫巷女王i
猫巷女王i 2020-11-22 02:09

Android does not have PDF support in its libraries. Is there any way to render PDF files in the Android applications?

9条回答
  •  一向
    一向 (楼主)
    2020-11-22 02:21

    I finally was able to modify butelo's code to open any PDF file in the Android filesystem using pdf.js. The code can be found on my GitHub

    What I did was modified the pdffile.js to read HTML argument file like this:

    var url = getURLParameter('file');
    
    function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null}
    

    So what you need to do is just append the file path after the index.html like this:

    Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");
    webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);
    

    Update the path variable to point to a valid PDF in the Adroid filesystem.

提交回复
热议问题