how to display online pdf file within app

user7824858 picture user7824858 · May 10, 2017 · Viewed 7.8k times · Source

i need to display online pdf file in my app. i am using third party api but problem is that it's not displaying online file here is my code

compile 'com.github.barteksc:android-pdf-viewer:2.5.1'
compile 'org.apache.commons:commons-io:1.3.2'

my xml file

    <com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

and here is my final code to render online pdf file in PDFview

    Uri uri = Uri.parse("http://www.pdf995.com/samples/pdf.pdf");
    mypdfview =(PDFView)findViewById(R.id.pdfView);


    mypdfview.fromUri(uri)
            .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
            .enableSwipe(true) // allows to block changing pages using swipe
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .defaultPage(0)
            .onDraw(this) // allows to draw something on a provided canvas, above the current page
            .onLoad(this) // called after document is loaded and starts to be rendered
            .onPageChange(this)
            .onPageScroll(this)
            .onError(this)
            .onRender(this) // called after document is rendered for the first time
            .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
            .password(null)
            .scrollHandle(null)
            .enableAntialiasing(true) // improve rendering a little bit on low-res screens
            .load();

i am facing problem that i can display file from asset folder. but when i passed uri then it not showing anything. i also given internet permission and external data read permission in menifest.

Answer

Alternatively you can use Webview to load pdf

webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" +pdfUrl);

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" +pdfUrl);