从该网站 Xamarin android 中的链接将 pdf 显示到 webview [英] Show a pdf into a webview from a link inside that Website Xamarin android
问题描述
在我的应用程序中,我实现了一个 webview 来显示指向该 webview 的网站链接.现在该网站有一个包含 pdf 文件链接的按钮.如果我点击网站上的那个按钮,它会在网上显示一个 pdf 文件.但是,如果我尝试在我的应用程序中将其打开到 webview 中,则没有任何反应.我对 Xamarin android 很陌生.我找不到任何合适的方法.这是我将网站显示到 webview 中的代码.
In my app I have implemented a webview to show a website link into that webview. Now that website has a button that contains a pdf file link. If I click on that button on website it shows a pdf file on the web. But if I try to open it into the webview in my app, nothing happens. I am very new in Xamarin android. I could not find any suitable way to that. Here is my code to show the website into the webview.
我想在点击网站链接时重新发布 pdf.但结果和之前一样
修改后的代码
namespace Xamarin.PDFView
{
[Activity (Label = "PDFView", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private string _documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
private string _pdfPath;
private string _pdfFileName = "thePDFDocument.pdf";
private string _pdfFilePath;
private WebView _webView;
private string _webUrl = "https://Link of thewebsite";
private string _pdfURL = "https://Link of thewebsite/25052016.pdf";
private WebClient _webClient = new WebClient();
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
_webView = FindViewById<WebView> (Resource.Id.webView1);
var settings = _webView.Settings;
settings.JavaScriptEnabled = true;
settings.AllowFileAccessFromFileURLs = true;
settings.AllowUniversalAccessFromFileURLs = true;
settings.BuiltInZoomControls = true;
_webView.SetWebViewClient(new WebViewClient());
_webView.SetWebChromeClient(new WebChromeClient());
_webView.LoadUrl(_webUrl);
}
public override bool OnTouchEvent(MotionEvent e)
{
if (_webUrl.Contains(".pdf"))
{
DownloadPDFDocument();
}
return base.OnTouchEvent(e);
}
protected override void OnResume ()
{
base.OnResume ();
_webView.LoadUrl( "javascript:window.location.reload( true )" );
}
protected override void OnPause ()
{
base.OnPause ();
_webView.ClearCache(true);
}
private void DownloadPDFDocument()
{
AndHUD.Shared.Show(this, "Downloading PDF\nPlease Wait ..", -1, MaskType.Clear);
_pdfPath = _documentsPath + "/PDFView";
_pdfFilePath = Path.Combine(_pdfPath, _pdfFileName);
// Check if the PDFDirectory Exists
if(!Directory.Exists(_pdfPath)){
Directory.CreateDirectory(_pdfPath);
}
else{
// Check if the pdf is there, If Yes Delete It. Because we will download the fresh one just in a moment
if (File.Exists(_pdfFilePath)){
File.Delete(_pdfFilePath);
}
}
// This will be executed when the pdf download is completed
_webClient.DownloadDataCompleted += OnPDFDownloadCompleted;
// Lets downlaod the PDF Document
var url = new Uri(_pdfURL);
_webClient.DownloadDataAsync(url);
}
private void OnPDFDownloadCompleted (object sender, DownloadDataCompletedEventArgs e)
{
// Okay the download's done, Lets now save the data and reload the webview.
var pdfBytes = e.Result;
File.WriteAllBytes (_pdfFilePath, pdfBytes);
if(File.Exists(_pdfFilePath))
{
var bytes = File.ReadAllBytes(_pdfFilePath);
}
_webView.LoadUrl("file:///android_asset/pdfviewer/index.html?file=" + _pdfFilePath);
AndHUD.Shared.Dismiss();
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
}
}
推荐答案
有没有办法从链接中显示pdf文件
is there any way to show pdf file from a link
如前所述,android webview 不支持 pdf 文件.作为一种解决方法,您可以在 android webview 中使用 Google Docs:
As discussed android webview doesn't support pdf files. As a workaround, you can use Google Docs in android webview:
创建一个自定义的 webview 客户端并像这样覆盖
ShouldOverrideUrlLoading
:
public class MyWebViewClient: WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
//if PDF file then use google client to show it
if (url.ToLower().EndsWith(".pdf"))
{
var newUrl = "https://docs.google.com/viewer?url=" + url;
view.LoadUrl(newUrl);
}
return true;
}
}
使用您的自定义 webview 客户端:
Use your custom webview client:
mWebview.SetWebViewClient(new MyWebViewClient());
注意:如果您使用 android:layout_height="wrap_content"
作为您的 webview,您需要将其更改为固定 dp,例如 android:layout_height="400dp"
,否则 webview 将无法正确显示 pdf.
Notes: If you use android:layout_height="wrap_content"
for your webview, you need to change it to a fixed dp like android:layout_height="400dp"
, otherwise the webview won't show pdf correctly.
这篇关于从该网站 Xamarin android 中的链接将 pdf 显示到 webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!