在App中显示PDF文件

时间:2014-08-06 21:59:01

标签: android pdf

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

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

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

  2. 使用extern App打开pdf文件:

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(不过outFile), “应用/ PDF”); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(意图);

  3. 两者都有效。但我的问题是pdf仅供内部使用,在两个示例中,用户都可以下载或将其保存在另一个文件夹中。

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

3 个答案:

答案 0 :(得分:9)

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

您可以找到详细信息here

以下是要从资产文件夹中的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);
    }
}

更新:此代码段来自谷歌开发者提供的样本。

答案 1 :(得分:4)

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

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

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

答案 2 :(得分:1)

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

Tier是您应该关注的几个开源pdf查看器: http://androiddeveloperspot.blogspot.com/2013/05/android-pdf-reader-open-source-code.html

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

相关问题