简体   繁体   中英

How do I display a pdf file in Xamarin.Android?

I have converted and saved a base64 string to a pdf file. Currently, I have the following code to open the PDF:

public void Pdf(string blob)
       {
           try
           {
               string filePath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "agremeent.pdf");
                        
               try
               {              
                   if (blob != null)
                   {
                       Java.IO.File file = new Java.IO.File(filePath);
                   
                       if (!file.Exists())
                       {
                           file.Delete();
                       }
                       fos = new FileOutputStream(filePath);

                       var bytes = Base64.Decode(blob, Base64Flags.Default);
                       fos.Write(bytes);
                       fos.Flush();
                       fos.Close();

                   }
               }
               catch (Exception e)
               {
               }
               finally
               {
                   if (fos != null)
                   {
                       fos = null;
                   }
               }
               Android.Net.Uri url = Android.Net.Uri.Parse(filePath); 

              
               Intent target = new Intent(Intent.ActionView);
               target.SetDataAndType(url, "application/pdf");

               Intent intent = Intent.CreateChooser(target, "Open File");
               try
               {
                   StartActivity(intent);
               }
               catch (ActivityNotFoundException e)
               {
                   // Instruct the user to install a PDF reader here, or something
               }
           
           }
           catch
           {
           }
        
       }

This lets user choose which PDF viewer they would like to use to open the PDF. But, the PDF isn't opening in Google Drive. What is causing this? Is there a way to open the PDF without leaving the app and using a third party viewer?

Answer: You can use the Xamarin.Bindings.PDFView-Android package and create an activity to show the pdf. It can make Android display the pdf easily. Such as: MainActicity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.pdfview.PDFView
    android:id="@+id/pdfview"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>
</LinearLayout>

MainActivity.cs:

 protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        string filePath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "agremeent.pdf");
        var pdfview = this.FindViewById<PDFView>(Resource.Id.pdfview);
        Java.IO.File file = new Java.IO.File(filePath);
        pdfview.FromFile(file).Show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM