PDF from byte array

-- -- 957 Reputation points
2021-10-18T00:16:43.77+00:00

Hi

I have a bytes array which I need to convert to PDF and view in Xamarin forms. How can I do that?

Thanks

Regards

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-10-18T08:08:25.217+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To save the pdf file and show the pdf from the byte array, try to achieve the function on each platform and you could use DependencyService to call the code in the shared project.

    Here is the sample code about the Android platofrom, you could refer to it.

       [assembly: Dependency(typeof(DroidImplementation))]  
       namespace TestApplication_6.Droid  
       {  
           public class DroidImplementation : ITestInterface  
           {  
               public bool SaveAndOpenPDF(string fileName, byte[] data)  
               {  
                   Context context = MainActivity.Instance;  
                   try  
                   {  
                       string filepath = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);  
                       string fullName = System.IO.Path.Combine(filepath, fileName);  
         
                       // If the file exist on device delete it  
                       if (File.Exists(fullName))  
                       {  
                           File.Delete(fullName);  
                       }  
         
                       File.WriteAllBytes(fullName, data);  
         
                       Android.Net.Uri file = FileProvider.GetUriForFile(context, context.PackageName + ".fileprovider", new Java.IO.File(fileName));  
                       Intent intent = new Intent(Intent.ActionView);  
                       intent.SetDataAndType(file, "application/pdf");  
                       intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask | ActivityFlags.GrantReadUriPermission | ActivityFlags.NewTask);  
                       context.StartActivity(intent);  
                       return true;  
                   }  
                   catch (Exception ex)  
                   {  
                       Console.WriteLine("ERROR: " + ex.Message + ex.StackTrace);  
                       return false;  
                   }  
               }  
           }  
       }  
    

    For the function code of iOS platform, you could search with the keyword as Xamarin.Forms save and open PDF file to check the related documentation.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.