XamDev
XamDev

Reputation: 45

display local PDF file in webview control - Displays Blank Pdf File

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

Upvotes: 1

Views: 1548

Answers (2)

Yuri S
Yuri S

Reputation: 5370

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

Upvotes: 1

Nico Zhu
Nico Zhu

Reputation: 32775

I have run into the similar issue when I implement a HybridWebView following the official document.

The problem is that the build action property of PDF file is not Content. Please set the build action as Content within your uwp client project.

enter image description here

Upvotes: 1

Related Questions