Show PDF file in App

后端 未结 3 1386
日久生厌
日久生厌 2021-02-03 12:03

I found this two possibilities to show a pdf file.

  1. Open a webView with:

    webView.loadUrl(\"https://docs.google.com/gview?embedded=true&url=\"+

相关标签:
3条回答
  • 2021-02-03 12:26

    Many libraries are available for showing pdfs within your own app.

    • Android PDF Viewer
    • VuDroid
    • APDFViewer
    • droidreader
    • android-pdf
    • mupdf
    • android-pdfView

    For a working example using android-pdfView, see this blog post. It demonstrates the basic usage of the library to display pdf onto the view with vertical and horizontal swipe.

    pdfView = (PDFView) findViewById(R.id.pdfView);
    pdfView.fromFile(new File("/storage/sdcard0/Download/pdf.pdf")).defaultPage(1).enableSwipe(true).onPageChange(this).load();
    
    0 讨论(0)
  • 2021-02-03 12:39

    You can show the PDF in your own viewer

    Tier are few open source pdf viewers you should look at: http://androiddeveloperspot.blogspot.com/2013/05/android-pdf-reader-open-source-code.html

    You can encrypt the pdf and make sure that your viewer only will be able to decrypt it.

    0 讨论(0)
  • 2021-02-03 12:40

    Android provides PDF API now with which it is easy to present pdf content inside application.

    you can find details here

    Below is the sample snippet to render from a pdf file in assets folder.

        private void openRenderer(Context context) throws IOException {
        // In this sample, we read a PDF from the assets directory.
        File file = new File(context.getCacheDir(), FILENAME);
        if (!file.exists()) {
            // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
            // the cache directory.
            InputStream asset = context.getAssets().open(FILENAME);
            FileOutputStream output = new FileOutputStream(file);
            final byte[] buffer = new byte[1024];
            int size;
            while ((size = asset.read(buffer)) != -1) {
                output.write(buffer, 0, size);
            }
            asset.close();
            output.close();
        }
        mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        // This is the PdfRenderer we use to render the PDF.
        if (mFileDescriptor != null) {
            mPdfRenderer = new PdfRenderer(mFileDescriptor);
        }
    }
    

    update: This snippet is from google developers provided samples.

    0 讨论(0)
提交回复
热议问题