Image click display Image/Pdf

What's the best way to display image/Pdf on Image click?

Best Answers

Answers

  • GeetSGeetS USMember ✭✭✭
    Accepted Answer

    For PDF:

  • GeetSGeetS USMember ✭✭✭
    edited July 2017 Accepted Answer

    I was trying to display PDF in android as explained in Video & https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/. For Android, downloaded required folders from https://github.com/xamarin/recipes/tree/master/cross-platform/xamarin-forms/Controls/DisplayPDF/Droid/Assets/pdfjs
    But the problem is that I am trying to use URL and it's displaying blank PDF. What can be the issue?

  • GeetSGeetS USMember ✭✭✭
    Accepted Answer

    Ok my mistake. Path was incorrect. Updated Path like below. But the PDF is shown very dim. What can be the issue?

    namespace DisplayPDF.Droid
    {
    public class CustomWebViewRenderer : WebViewRenderer
    {
    protected override void OnElementChanged (ElementChangedEventArgs e)
    {
    base.OnElementChanged (e);

            if (e.NewElement != null) {
                var customWebView = Element as CustomWebView;
                Control.Settings.AllowUniversalAccessFromFileURLs = true;
                Control.LoadUrl (string.Format ("file:///android_asset/pdfjs/web/viewer.html?file={0}",  WebUtility.UrlEncode (customWebView.Uri)));  **//Updated this Path**
            }
        }
    }
    

    }

  • GeetSGeetS USMember ✭✭✭

    It was the issue of Emulator. In mobile it display is fine.

  • GeetSGeetS USMember ✭✭✭

    To display HTML too, I had referred to https://developer.xamarin.com/api/type/Xamarin.Forms.WebView/. In Short, checked code like below:
    public partial class DocumentImage : ContentPage
    {
    public DocumentImage(String UploadedDoc)
    {
    InitializeComponent();
    Padding = new Thickness(0, 20, 0, 0);
    String FileExtension = Path.GetExtension(UploadedDoc);

            String murl = string.Empty;
            String FileName = (UploadedDoc.Contains("/")) ? UploadedDoc.Substring(UploadedDoc.LastIndexOf('/') + 1) : UploadedDoc; 
            if (FileExtension.ToLower() == ".pdf")
            {
                murl = "http://developer.xamarin.com/guides/cross-platform/getting_started/introduction_to_mobile_development/offline.pdf";  //For PDF
                Content = new StackLayout
                {
                    // VerticalOptions = LayoutOptions.Fill,
                    Children ={
                                new Label()
                                {
                                    Text=FileName,
                                    TextColor=Color.FromHex("#0B56A5"),
                                    Margin = new Thickness(7,0,0,0),
                                    FontAttributes=FontAttributes.Bold
                                    // HorizontalTextAlignment = TextAlignment.Center,
                                    //Text=(FileExtension.ToLower() == "pdf") ? "PDF File" : "Image File"
                                },
                                new MyCustomWebView
                                {
                                    Uri=murl,
                                    HorizontalOptions=LayoutOptions.FillAndExpand,
                                    VerticalOptions=LayoutOptions.FillAndExpand
                                }
                            }
                };
            }
            else if(FileExtension.ToLower() == ".html")
             {
                murl = "https://www.sheldonbrown.com/web_sample1.html";
                Content = new StackLayout
                {
                    // VerticalOptions = LayoutOptions.Fill,
                    Children ={
                                new Label()
                                {
                                    Text=FileName,
                                    TextColor=Color.FromHex("#0B56A5"),
                                    Margin = new Thickness(7,0,0,0),
                                    FontAttributes=FontAttributes.Bold
                                    // HorizontalTextAlignment = TextAlignment.Center,
                                   // Text=(FileExtension.ToLower() == "pdf") ? "PDF File" : "Image File"
                                },
                                new WebView
                                {
                                    Source=new UrlWebViewSource{Url = murl, },
                                    VerticalOptions=LayoutOptions.FillAndExpand
                                }
                            }
                };
            }
            else
            {
                murl = "https://xamarin.com/content/images/pages/forms/example-app.png";    //For Image
                Content = new StackLayout
                {
                    // VerticalOptions = LayoutOptions.Fill,
                    Children ={
                                new Label()
                                {
                                    Text=FileName,
                                    TextColor=Color.FromHex("#0B56A5"),
                                    Margin = new Thickness(7,0,0,0),
                                    FontAttributes=FontAttributes.Bold
                                    // HorizontalTextAlignment = TextAlignment.Center,
                                   // Text=(FileExtension.ToLower() == "pdf") ? "PDF File" : "Image File"
                                },
                                new Image()
                                {
                                   Source=ImageSource.FromUri(new Uri(murl))
                                }
                            }
                };
            }
        }
    }
    
  • GeetSGeetS USMember ✭✭✭

    In case on wants to open PDF in Emulator, without writing any code, then add 'com.google.android.apps.pdfviewer-2.2.083.11.70-508311170-minAPI14.apk' in folder c:/>ProgramFiles(x86)>Android>android-sdk.
    Download from https://www.apkmirror.com/apk/google-inc/google-pdf-viewer/google-pdf-viewer-2-2-083-11-release/google-pdf-viewer-2-2-083-11-30-android-apk-download/download/

    Now, open Visual Studio 2017(but don't open any project) and click on Tools>Android>Android Adb Command prompt and type the below command:
    C:\Program Files(x86)\Android\android-sdk>adb install com.google.android.apps.pdfviewer-2.2.083.11.70-508311170-minAPI14.apk

    Now, in Emulator click on any PDF file and it will be opened.

Sign In or Register to comment.