Android-Code to Implement PDF Viewer

后端 未结 2 1536
旧时难觅i
旧时难觅i 2021-02-04 22:22

Here i need to use Pdf-Viewer in my app.i took many API as a reference but still stucking up in viewing Pdf files from SDCARD. here my code

`First.java<

相关标签:
2条回答
  • 2021-02-04 23:01

    you can open pdf on google doc

        WebView webviewer = new WebView(this);
        webviewer.getSettings().setJavaScriptEnabled(true);
        webviewer.getSettings().setPluginsEnabled(true);
        setContentView(webviewer);
    
        webviewer.loadUrl("http://docs.google.com/gview?embedded=true&url=PDF_url/fileName.pdf");
        setContentView(webviewer);
    

    To open the PDF in application like quick pdf or adobe pdf reader:

        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    
    0 讨论(0)
  • 2021-02-04 23:11

    You can use AndroidPdfViewer library.

    Add this dependency in your Grade:

    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
    

    Add PDFView in activity_main.xml

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

    Add Load a PDF file in MainActivity.java

    pdfView.fromUri(Uri)
    

    or

    pdfView.fromFile(File)
    

    or

    pdfView.fromBytes(byte[])
    

    or

    pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams
    

    or

    pdfView.fromSource(DocumentSource)
    

    or

    pdfView.fromAsset(String)
        .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)
        // allows to draw something on the current page, usually visible in the middle of the screen
        .onDraw(onDrawListener)
        // allows to draw something on all pages, separately for every page. Called only for visible pages
        .onDrawAll(onDrawListener)
        .onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
        .onPageChange(onPageChangeListener)
        .onPageScroll(onPageScrollListener)
        .onError(onErrorListener)
        .onPageError(onPageErrorListener)
        .onRender(onRenderListener) // called after document is rendered for the first time
        // called on single tap, return true if handled, false to toggle scroll handle visibility
        .onTap(onTapListener)
        .onLongPress(onLongPressListener)
        .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
        // spacing between pages in dp. To define spacing color, set view background
        .spacing(0)
        .autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
        .linkHandler(DefaultLinkHandler)
        .pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view
        .fitEachPage(false) // fit each page to the view, else smaller pages are scaled relative to largest page.
        .pageSnap(false) // snap pages to screen boundaries
        .pageFling(false) // make a fling change only a single page like ViewPager
        .nightMode(false) // toggle night mode
        .load();
    

    If you are using ProGuard, don't forget to add below line:

    -keep class com.shockwave.**
    
    0 讨论(0)
提交回复
热议问题