【问题标题】:Display PDF file inside my android application在我的 android 应用程序中显示 PDF 文件
【发布时间】:2012-03-28 18:59:21
【问题描述】:

我不知道如何在 Android 应用程序中显示 PDF 文件。到目前为止,我发现可以启动 Intent 并使用 Android 默认应用程序打开 PDF。但我想直接在我的应用程序中查看 PDF 文件而不退出。我的布局中有页眉和页脚 - 我想在两者之间打开 PDF。我还从 github.com 找到了一个 PdfReader.jar 文件,但它会在新活动中打开 PDF。

【问题讨论】:

  • 您是否尝试使用 googledocs 查看器?
  • 我正在使用 google docs viwer,但它滞后很多并且非常慢:(

标签: android pdf android-activity


【解决方案1】:

你可以从这里下载源码(Display PDF file inside my android application)

在你的 gradle 文件中添加这个依赖:

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'

activity_main.xml

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark"
        android:text="View PDF"
        android:textColor="#ffffff"
        android:id="@+id/tv_header"
        android:textSize="18dp"
        android:gravity="center"></TextView>

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


    </RelativeLayout>

MainActivity.java

package pdfviewer.pdfviewer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.shockwave.pdfium.PdfDocument;

import java.util.List;

public class MainActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener{
    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String SAMPLE_FILE = "android_tutorial.pdf";
    PDFView pdfView;
    Integer pageNumber = 0;
    String pdfFileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        pdfView= (PDFView)findViewById(R.id.pdfView);
        displayFromAsset(SAMPLE_FILE);
    }

    private void displayFromAsset(String assetFileName) {
        pdfFileName = assetFileName;

        pdfView.fromAsset(SAMPLE_FILE)
                .defaultPage(pageNumber)
                .enableSwipe(true)

                .swipeHorizontal(false)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
    }


    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
    }


    @Override
    public void loadComplete(int nbPages) {
        PdfDocument.Meta meta = pdfView.getDocumentMeta();
        printBookmarksTree(pdfView.getTableOfContents(), "-");

    }

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {

            Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }

}

【讨论】:

  • 正如某人@你的博客所说的那样:工作就像一个魅力。 +1 你。感谢那。澄清一下:pdf 必须在 assets 文件夹内,而该文件夹必须在 main 文件夹内(而不是 res)。谢谢。
  • 将您的 pdf 文件保存在您的 Assest 文件夹中(位于主文件夹下)
  • 你们确实意识到添加这个库会使您的 apk 大小增加 16MB。对吧?
  • 我很惊讶没有人像@legalimpurity 提到的那样提出图书馆的规模问题。
  • @sHOLE 我也花了一段时间才弄明白。解决方案是拆分您的 APK,但即使拆分后平均大小增加了 5MB,这不太好。
【解决方案2】:

也许您可以将 MuPdf 集成到您的应用程序中。这是我描述的如何做到这一点:Integrate MuPDF Reader in an app

【讨论】:

  • 我无法理解无法集成 MuPdf,如上所述,我已经下载了这两个东西,但没有 VS :(
  • 如果您使用 Windows 开发应用程序,则需要 VS。如果您使用 Linux,则不需要它。
  • 你说的VS是什么?
  • 这是 Visual Studio 的缩写。
  • @Yury,当我尝试使用 pdf url 而不是 pdf 调用 DocumentActivity 时,会打开带有关闭按钮的空弹出窗口......有什么想法吗?
【解决方案3】:

强烈建议您查看PDF.js,它能够在标准的 WebView 组件中呈现 PDF 文档。

另请参阅https://github.com/loosemoose/androidpdf 以获取此示例实现。

【讨论】:

    【解决方案4】:

    我认为你不能轻易做到这一点。你应该在这里考虑这个答案:

    How can I display a pdf document into a Webview?

    基本上,如果 PDF 是通过谷歌文档在线托管的,你将能够看到它,但如果你的设备中有它,你就不能看到它(你需要一个独立的阅读器)

    【讨论】:

    • 我正在使用 webview,但它非常慢,并且惹恼了客户:(
    【解决方案5】:
     public class MainActivity extends AppCompatActivity {
       WebView webview;
       ProgressBar progressbar;
    
      @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView)findViewById(R.id.webview);
        progressbar = (ProgressBar) findViewById(R.id.progressbar);
        webview.getSettings().setJavaScriptEnabled(true);
        String filename ="http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
        webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);
    
        webview.setWebViewClient(new WebViewClient() {
    
            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                progressbar.setVisibility(View.GONE);
            }
        });
    
    }
    

    }

    【讨论】:

      【解决方案6】:
      【解决方案7】:
      Uri path = Uri.fromFile(file );
      Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
      pdfIntent.setDataAndType(path , "application/pdf");
      pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      try {
          startActivity(pdfIntent ); 
          }
      catch (ActivityNotFoundException e) {
          Toast.makeText(EmptyBlindDocumentShow.this,
                  "No Application available to viewPDF",
                  Toast.LENGTH_SHORT).show();
          }  
      }  
      

      【讨论】:

      • 问题提到“内部”,而不是意图。
      • 这不是这个问题的答案。它在应用程序之外打开文件,并且应该在用户的手机中至少安装一个兼容的应用程序
      • 可能是您错过了在应用程序内部打开文件的要点:)
      【解决方案8】:

      这是在没有任何 3rd 方库的情况下为我工作的完美解决方案。

      Rendering a PDF Document in Android Activity/Fragment (Using PdfRenderer)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 2014-06-30
        • 1970-01-01
        • 2018-12-28
        • 2015-07-08
        • 1970-01-01
        相关资源
        最近更新 更多