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.