简体   繁体   English

如何从sdcard的web视图中打开android中的PDF文件

[英]How to open the PDF file in android in web view from the sdcard

I want to open the pdf file Programmatically without using any support of PDF reader.我想在不使用 PDF 阅读器的任何支持的情况下以编程方式打开 pdf 文件。 Is it possible to open the pdf file with webview,if yes how?是否可以使用 webview 打开 pdf 文件,如果可以,如何?

or if any other possible way is there please suggest me.或者如果有任何其他可能的方式,请建议我。

than you.比你。

kriss克里斯

I want to open the pdf file Programmatically without using any support of PDF reader.我想在不使用 PDF 阅读器的任何支持的情况下以编程方式打开 pdf 文件。

By definition, that is impossible.根据定义,这是不可能的。 That's like saying you want to view HTML without an HTML reader.这就像说您想在没有 HTML 阅读器的情况下查看 HTML。

Is it possible to open the pdf file with webview,if yes how?是否可以使用 webview 打开 pdf 文件,如果可以,如何?

All it will do is try to redirect you to an on-device PDF reader.它所做的只是尝试将您重定向到设备上的 PDF 阅读器。

Please allow the user to view PDFs in the PDF reader of their choice.请允许用户在他们选择的 PDF 阅读器中查看 PDF。 Many Android devices ship with a PDF reader already installed.许多 Android 设备出厂时已经安装了 PDF 读卡器。

you cannot open local PDF in webview but if you want PDF to be opened in your own application then you have to use third party libraries.您无法在 webview 中打开本地 PDF 但如果您希望 PDF 在您自己的应用程序中打开,则必须使用第三方库。 one of the easy to use library is android-pdfview易于使用的库之一是 android-pdfview

you can find jar here check out this question for more details你可以在这里找到 jar 查看这个问题了解更多详情

it says to open PDF all you have to do is:它说要打开 PDF 你所要做的就是:

1) add jar and armeabi-v7a folder in your libs 1)在你的库中添加 jar 和armeabi-v7a文件夹

2) add pdf view in your layout 2) 在布局中添加 pdf 视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.joanzapata.pdfview.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

3) load your PDF 3)加载你的 PDF

PDFView pdfView= (PDFView) findViewById(R.id.pdfView);
pdfView.fromAsset("yourPDF.pdf") //you can also load from File using pdfView.fromFile()
.defaultPage(1)
.load(); 

you can also set onPageChangeListner, onPageLoadListner if you want.如果需要,您还可以设置 onPageChangeListner、onPageLoadListner。

You can only use the installed pdf reader applications in device to open the pdf您只能使用设备中安装的 pdf 读卡器应用程序打开 pdf

You can use the google's document viewer service for viewing the pdf,ppt contents in the webview if your document resource is on online.如果您的文档资源在线,您可以使用google的文档查看器服务查看webview中的pdf,ppt内容。 create following HTML string like:创建以下 HTML 字符串,如:
String googleDocUrl ="<iframe src='http://docs.google.com/gview?url=" + YOUR_ONLINE_PDF_DOC_URL + "&embedded=true' width='100%' height='100%'style='border: none;'></iframe>";


call webviews loadData() method:调用 webviews loadData() 方法:

 mWebView.loadData(googleDocUrl ,"text/html","UTF-8");

thats it.而已。 as this worked for me in my last project.因为这在我的上一个项目中对我有用。
Hope this might help you.希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM