【问题标题】:Display Word/PDF Document inside my Android Application在我的 Android 应用程序中显示 Word/PDF 文档
【发布时间】:2018-06-05 05:24:47
【问题描述】:

我已经查看了所有可能的选项。我需要在我的 android 应用程序中显示 pdf 和 word 文档。出于某些安全原因,我不允许在 3rd 方应用程序中打开文档。但不幸的是,内置 android 没有内置组件来显示 word 文档和默认内置组件来显示来自 API 21 及更高版本的 pdf 支持(我的应用程序从 API 19 开始)。任何人都可以对这个问题有所了解,因为我找不到任何解决方案。请注意,我正在寻找开源解决方案。

【问题讨论】:

标签: android pdf ms-word


【解决方案1】:

在 webview 中打开 pdf/Doc

     String pdf = "http://www.pc-hardware.hu/PDF/konfig.pdf";
   String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
              width='100%' height='100%' 
              style='border: none;'></iframe>";
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.loadUrl(doc);

【讨论】:

  • 您确定可以在webView 中打开扩展名为.doc /.docx 的文件吗?
  • 是的,如果您不想使用任何第三方视图而不是别无选择,它可以正常工作
  • 酷,听起来不错!
  • 感谢您的回复,但对于某些人来说,我收到一条错误消息,提示“拒绝加载无效 URL”
  • 你能把你的 pdf url 过去吗?
【解决方案2】:

您可以使用第三方库在视图中显示它 喜欢this

<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

@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 + "-");
        }
    }
}

【讨论】:

  • 我猜他已经说过他不想使用任何 3rd 方库。
  • 不想在第三方应用程序中打开,但可以使用第三方库在他的应用程序中打开
  • 感谢 Kashif 的回复。我已经检查过了。问题是它只支持 pdf 并且我也有 word 文档。所以我想到了一个通用的解决方案,我可以同时使用它
猜你喜欢
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多