Display pdf

csampaio01csampaio01 PTMember ✭✭✭

Hi, I followed the tutorial on displaying pdf documents in PCL projects. In iOS works perfectly but on Android it doesn't. My app downloads pdf and shows them. Here is my code:

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

        if (e.NewElement != null)
        {
            var customWebView = Element as CustomWebView;
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var filename2 = Path.Combine(documents, customWebView.Uri);

            Control.Settings.AllowUniversalAccessFromFileURLs = true;

            //Control.LoadUrl(string.Format("file:///android_asset/pdfviewer/web/viewer.html?file={0}","file://" + filename2));

            //Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("{0}", WebUtility.UrlEncode(filename2))));

            Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}",WebUtility.UrlEncode(filename2)));
        }
    }
}

`
I have tried many ways to find the pdf.

Best Answer

  • csampaio01csampaio01 PT ✭✭✭
    Accepted Answer

    I forgot to add pdfjs to assets folder.

    I used:

    ` Control.Settings.AllowUniversalAccessFromFileURLs = true;

                Control.Settings.BuiltInZoomControls = true;
                Control.Settings.DisplayZoomControls = true;
    
                var customWebView = Element as CustomWebView;
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                var filename2 = Path.Combine(documents, customWebView.Uri);
    
    
                var test = string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file://{0}", WebUtility.UrlEncode(filename2)));
                Control.LoadUrl(test);`
    

Answers

  • vkt_rbvkt_rb USMember ✭✭✭
    edited April 2017

    Hi @carlasampaio01, I couldn't understand you if you could or not download and show the .pdf file in android, but I will share you my code, I did use a webClient to download it, and a webView to open it as a container, but at the same time I could do it open it with a third software, in this case the .PDF manager installed in the fisical device.

    DownloadCode

    void descargarCondiciones()
            {
                try
                {
                    //the link of the .Pdf file in the cloud to download it
                    string _sUrl = "http://127.0.0.1:8080/_Recursos_Marzu/Explicacion_de_Condiciones_Prueba.pdf";
    
                    string _rutaLocal = "/storage/emulated/0/download/Explicacion_de_Condiciones_Prueba.pdf";
                    var _webClient = new WebClient();
                    _webClient.DownloadFile(_sUrl, _rutaLocal);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    Third Software Display

    void abrirCondiciones()
            {
                try
                {
                    string contrato = "Explicacion_de_Condiciones_Prueba.pdf";
                    Android.Net.Uri uri = Android.Net.Uri.Parse("file:///storage/emulated/0/download/" + contrato);
                    Intent intent = new Intent(Intent.ActionView);
                    intent.SetDataAndType(uri, "application/pdf");
                    intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
    
                    try
                    {
                        Xamarin.Forms.Forms.Context.StartActivity(intent);
                    }
                    catch (Exception)
                    {
                        Toast.MakeText(Xamarin.Forms.Forms.Context, "No hay aplicacion instalada para ver PDF", ToastLength.Long).Show();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    With the "container" or the WebView

    void mostrarContrato()
            {
                try
                {
                    Webv_Contrato = new WebView(view_Content_Contrato.Bounds);
                    stl_Content_Contrato.Children.Add(Webv_Contrato);//principal stacklayout to expands the content to all the device screen or the size you wants.
                    var _sUrl = "http://127.0.0.1:8080/_Recursos_Marzu/Contrato_Server_Prueba.pdf";
    
                    Webv_Contrato.LoadRequest(new UrlRequest(new Url(_sUrl)));
                    Webv_Contrato.ScalesPageToFit = true;
    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    Let me know if that could help you.

  • vkt_rbvkt_rb USMember ✭✭✭

    Hi @carlasampaio01, I couln't understand if you could or not download it in android, but I'll share you my code, I could figured it out, downloading the .pdf file with a webClient and open it with a Third Software, in this case the PDF file manager installed in the device, but at the same time I could show it directly from the link in a "web container" in the same View without leaving the app, but I just did it in iOS but you just need to change the UIiOS object to UI android objects(for example the UIWebView to WebView) :

    Download Code

    void descargarCondiciones()
      {
          try
          {
              
              string _sUrl = "http://131.108.38.29:8140/_Recursos_Marzu/Explicacion_de_Condiciones_Prueba.pdf";
              string _rutaLocal = "/storage/emulated/0/download/Explicacion_de_Condiciones_Prueba.pdf";
              var _webClient = new WebClient();
              _webClient.DownloadFile(_sUrl, _rutaLocal);
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
    

    Third Software

    void abrirCondiciones()
            {
                try
                {
                    //string contrato = "Contrato_de_Prueba.pdf";
                    string contrato = "Explicacion_de_Condiciones_Prueba.pdf";
                    Android.Net.Uri uri = Android.Net.Uri.Parse("file:///storage/emulated/0/download/" + contrato);
                    Intent intent = new Intent(Intent.ActionView);
                    intent.SetDataAndType(uri, "application/pdf");
                    intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
    
                    try
                    {
                        Xamarin.Forms.Forms.Context.StartActivity(intent);
                    }
                    catch (Exception)
                    {
                        Toast.MakeText(Xamarin.Forms.Forms.Context, "No hay aplicacion instalada para ver PDF", ToastLength.Long).Show();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    and the webView

    void mostrarContrato()
            {
                try
                {
                    Webv_Contrato = new UIWebView(view_Content_Contrato.Bounds);
                    view_Content_Contrato.AddSubview(Webv_Contrato);
                    var _sUrl = "http://131.108.38.29:8140/_Recursos_Marzu/Contrato_Server_Prueba.pdf";
    
                    Webv_Contrato.LoadRequest(new NSUrlRequest(new NSUrl(_sUrl)));
                    Webv_Contrato.ScalesPageToFit = true;
    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    Of course you can change the sURL and set your fisical path if you want to dowload it and use it

  • vkt_rbvkt_rb USMember ✭✭✭

    Hi @carlasampaio01, I couldn't understand if you could or not download the .pdf in android and after display it, but I'll share you my code, first at all to download the file in the device I used a webClien, and after to open it I used a third software in this case the pdf file manager of the device, but at the same time I could open a Pdf with a webView, here's the code:

    Downloading

    void descargarCondiciones()
            {
                try
                {
    
                    string _sUrl = "http://131.108.38.29:8140/_Recursos_Marzu/Explicacion_de_Condiciones_Prueba.pdf";
                    string _rutaLocal = "/storage/emulated/0/download/Explicacion_de_Condiciones_Prueba.pdf";
                    var _webClient = new WebClient();
                    _webClient.DownloadFile(_sUrl, _rutaLocal);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    Third Software Display
    void abrirCondiciones()
    {
    try
    {
    //string contrato = "Contrato_de_Prueba.pdf";
    string contrato = "Explicacion_de_Condiciones_Prueba.pdf";
    Android.Net.Uri uri = Android.Net.Uri.Parse("file:///storage/emulated/0/download/" + contrato);
    Intent intent = new Intent(Intent.ActionView);
    intent.SetDataAndType(uri, "application/pdf");
    intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

                        try
                        {
                            Xamarin.Forms.Forms.Context.StartActivity(intent);
                        }
                        catch (Exception)
                        {
                            Toast.MakeText(Xamarin.Forms.Forms.Context, "No hay aplicacion instalada para ver PDF", ToastLength.Long).Show();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
    

    and here's the webView way but this one I just did for iOS but, you just need to change the UI elements of iOS for Android (for example the UIWebView to WebView and view_Content_Contrato(UIViewController) to stack layout )

    void mostrarContrato()
            {
                try
                {
                    Webv_Contrato = new UIWebView(view_Content_Contrato.Bounds);
                    view_Content_Contrato.AddSubview(Webv_Contrato);
                    var _sUrl = "http://131.108.38.29:8140/_Recursos_Marzu/Contrato_Server_Prueba.pdf";
    
                    Webv_Contrato.LoadRequest(new NSUrlRequest(new NSUrl(_sUrl)));
                    Webv_Contrato.ScalesPageToFit = true;
    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

    Let me know if the code was of help.

  • csampaio01csampaio01 PTMember ✭✭✭
    Accepted Answer

    I forgot to add pdfjs to assets folder.

    I used:

    ` Control.Settings.AllowUniversalAccessFromFileURLs = true;

                Control.Settings.BuiltInZoomControls = true;
                Control.Settings.DisplayZoomControls = true;
    
                var customWebView = Element as CustomWebView;
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                var filename2 = Path.Combine(documents, customWebView.Uri);
    
    
                var test = string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file://{0}", WebUtility.UrlEncode(filename2)));
                Control.LoadUrl(test);`
    
Sign In or Register to comment.