Forum Xamarin.Forms

How to display word document in Webview Xamarin forms?

MonalishaMonalisha Member ✭✭
edited November 2019 in Xamarin.Forms

I tried the following code for Android to display ms word in webview. But still unable to display. I stored the document in Assets folder.

public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            var customWebView = Element as CustomWebView;
            Control.Settings.AllowUniversalAccessFromFileURLs = true;
            Control.LoadUrl(string.Format("file:///android_asset/{0}", customWebView.Uri));
        }
    }
}

I tried the following code for UWP but still it did not work. I stored the document dirrectly in the root project

public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            var customWebView = Element as CustomWebView;
            Control.Source = new Uri(string.Format("ms-appx-web:///{0}", WebUtility.UrlEncode(customWebView.Uri)));
           //Control.Source = new Uri(string.Format("ms-appx-web:///{0}", customWebView.Uri));
        }
    }
}

However, I am able to display document for ios. Any suggestion Please?

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    edited November 2019

    For local MS office file , you can use the plugin PDFTron .

    And for remote , you just need to use the prefix of MS office file previewer .

     Control.LoadUrl(string.Format("https://view.officeapps.live.com/op/view.aspx?src={0}", customWebView.Uri));
    
    //or 
    //Control.LoadUrl(string.Format("https://drive.google.com/viewerng/viewer?url={0}", customWebView.Uri));
    
    
  • MonalishaMonalisha Member ✭✭

    @LucasZhang said:
    For local MS office file , you can use the plugin PDFTron .

    And for remote , you just need to use the prefix of MS office file previewer .

     Control.LoadUrl(string.Format("https://view.officeapps.live.com/op/view.aspx?src={0}", customWebView.Uri));
    
    //or 
    //Control.LoadUrl(string.Format("https://drive.google.com/viewerng/viewer?url={0}", customWebView.Uri));
    
    

    Thanks for your reply. But according to my project requirement, I can't use any plug in. Any other Suggestion please without Plugin............

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    Interesting. This is now the third or fourth thread that i'm seeing that asks for a solution to display a word document in a webview without the use of a third party nuget.

    What is this? Some kind of homework?

  • NMackayNMackay GBInsider, University mod

    @DirkWilhelm said:
    Interesting. This is now the third or fourth thread that i'm seeing that asks for a solution to display a word document in a webview without the use of a third party nuget.

    What is this? Some kind of homework?

    :D

Sign In or Register to comment.