Asked  10 Months ago    Answers:  5   Viewed   7 times

I am working on Xamarin Forms - UWP. I want to display local PDF file in webview control. I followed these 2 links :- https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

Xamarin Forms UWP - Display PDF

It opens the pdf file but content is all blank. Can anyone please help me in what I may be missing?

Thanks in advance!

Here is my code:- CustomWebView.cs

public class CustomWebView : WebView
    {
        public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
                returnType: typeof(string),
                declaringType: typeof(CustomWebView),
                defaultValue: default(string));

        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }
    }


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:PdfViewer;assembly=PdfViewer"
         x:Class="PdfViewer.MainPage"
         Padding="0,20,0,0">

<ContentPage.Content>
    <local:CustomWebView Uri="samplepdf.pdf" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>

[assembly: ExportRenderer(typeof(PdfViewer.CustomWebView), typeof(CustomWebViewRenderer))]
namespace PdfViewer.UWP
{
    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:///Assets/pdfjs/web/viewer.html?file={0}", string.Format("ms-appx-web:///Assets/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));
            }
        }
    }
}

UWP - Assets - pdfjs structure

 Answers

1

Looks like something happened with Asset folder files. When was updated from nuget repository it worked.

Saturday, August 21, 2021
 
shivam
 
1

I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.

That has never been possible.

Is there a better way to load a local pdf inside my app without having it take up the entire activity page?

The best thing is to not load it inside your app at all. Use the user's preferred PDF viewing app, via ACTION_VIEW.

Beyond that, you could use a slightly modified version of PDF.js on Android 4.4+. Or, use AndroidPdfViewer on a wider range of Android versions, though it makes your APK a lot bigger. See this blog post for more context on these and other (worse) options.

Also, here are sample apps demonstrating:

  • PDF.js: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/PdfJS
  • AndroidPdfViewer: https://github.com/commonsguy/cw-omnibus/tree/v8.10/PDF/Pdfium
Thursday, August 5, 2021
 
4

The plugin you used is too old. You could use ImageCropper.Forms.Fix.v2 instead.

Add the code below in your MainActivity:

   Stormlion.ImageCropper.Droid.Platform.Init();
   protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        Stormlion.ImageCropper.Droid.Platform.OnActivityResult(requestCode, resultCode, data);
    }

Add the code in tag of AndroidManifest.xaml:

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:theme="@style/Base.Theme.AppCompat" />

Code behind:

 new ImageCropper()
        {
            //                PageTitle = "Test Title",
            //                AspectRatioX = 1,
            //                AspectRatioY = 1,
            Success = (imageFile) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    image.Source = ImageSource.FromFile(imageFile);
                });
            }
        }.Show(this);

When you click the button, it would pop up a window. If you want to take photo, choose the Take Photo. Please note, you need to add the CAMERA permission.

<uses-permission android:name="android.permission.CAMERA" />

Or you could select the image from the device to crop.

OutPut:

https://imgur.com/okWKHfk

Wednesday, September 1, 2021
 
Norgul
 
2

Is it possible to open a local PDF file using Google Docs + webview?

Only if you upload the PDF somewhere on the Internet where Google Docs can access it, or perhaps arrange for somebody else to upload the PDF somewhere on the Internet where Google Docs can access it (e.g., Google Drive).

Thursday, September 23, 2021
 
4

As of Xamarin Forms 2.3.1, WebViewRenderer for Windows RT simply ignores the BaseUrl (https://github.com/xamarin/Xamarin.Forms/blob/2.3.1/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs#L26). However, there is a fix for this on the 2.3.2 branch: https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs

Saturday, December 25, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :