Android 在应用程序中显示 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25170975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:09:54  来源:igfitidea点击:

Show PDF file in App

androidpdf

提问by Laire

I found this two possibilities to show a pdf file.

我发现这两种可能性来显示 pdf 文件。

  1. Open a webView with:

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

  2. Open the pdf File with a extern App:

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(outFile),"application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent);

  1. 使用以下命令打开 webView:

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

  2. 使用外部应用程序打开 pdf 文件:

    意图意图=新意图(Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(outFile),"application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 开始活动(意图);

Both of them work. But my problem is that the pdf is for internal use only and in both examples the user could download it or save it in another folder.

他们两个都工作。但我的问题是 pdf 仅供内部使用,在这两个示例中,用户都可以下载它或将其保存在另一个文件夹中。

I know frameworks for this in iOS dev, I looking for a solution that works with Android.

我知道 iOS 开发中的框架,我正在寻找适用于 Android 的解决方案。

回答by GvSharma

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

Android 现在提供了 PDF API,可以很容易地在应用程序中呈现 pdf 内容。

you can find details here

你可以在这里找到详细信息

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

下面是从 assets 文件夹中的 pdf 文件呈现的示例片段。

    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.

更新:此代码段来自谷歌开发人员提供的示例。

回答by Jaydev

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

许多库可用于在您自己的应用程序中显示 pdf。

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.

有关使用的工作示例android-pdfView,请参阅此博客文章。它演示了库的基本用法,通过垂直和水平滑动将 pdf 显示到视图上。

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(new File("/storage/sdcard0/Download/pdf.pdf")).defaultPage(1).enableSwipe(true).onPageChange(this).load();

回答by AnkitSomani

You can show the PDF in your own viewer

您可以在自己的查看器中显示 PDF

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

您应该查看一些开源 pdf 查看器: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.

您可以加密 pdf 并确保您的查看者只能解密它。