We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to share the PDF document in Xamarin.Forms platform?

Platform: Xamarin.iOS |
Control: PdfViewer |
Published Date: April 17, 2018 |
Last Revised Date: February 19, 2019 |
.NET Framework: 4.5.1

PDF documents can be shared by saving the loaded PDF to the file system using the SaveDocument API and share the saved PDF. Refer to the following code snippets.

Portable

Add an interface to the PCL project which defines a method that shares the saved PDF document.

C#

 

Add another interface to save the loaded PDF to the file system.

C#

public interface ISave

{

    string Save(MemoryStream fileStream);

}

 

Get the saved file path using the Save method defined in ISave and use this as an argument to share the file using Show method of IShare interface. 

C#

 

Android

Implement IShare with file sharing options available in Android platform.

C#

 

Implement ISave with file write options available in Android platform.

C#

 

iOS

Implement IShare with file sharing options available in iOS platform.

C#

 

Implement ISave with file write options available in iOS platform.

C#

 

UWP

Implement IShare with file sharing options available in UWP platform.

C#

 

Implement ISave with file write options available in UWP platform.

C#

 

Sample link:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/GettingStarted_PDFViewer-1659303577

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Solomon Fried
Jan 31, 2019

A few changes needed for this to work on Android past version 24. 

1. Edit the Android manifest and wit hing the xml node, add the following... (replace YOUR_PACKAGE_NAME with your package name.

              android:authorities="[YOUR_PACKAGE_NAME].fileprovider"
              android:exported="false"
              android:grantUriPermissions="true">
     
   

2. Create a folder under Resources called "xml"

3. add a file to the xml directory called file_paths.xml containing the following.. (you probably only need the "files" node)



 
 
 
 
 

4. In the ShareAndroid class,  
replace var uri = Android.Net.Uri.Parse("file://" + filePath); with ....    var uri = FileProvider.GetUriForFile(context, context.PackageName + ".fileprovider", new File(filePath));

5. Right before context.StartActivity(chooserIntent), add the following

    chooserIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
    chooserIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);

Finally, The Save... classes are not needed and you can use Xamarin.Essentials.FileSystem.AppDataDirectory to get the platform-specific directories.

And that is all folks...

Much thanks to the Xam.Plugins.Messaging team where I found some code in the Email messaging code that helped out here.

You may want to enhance your code to handle older versions of Android (prior to 24) as well. See here for how it was done in the Email plugin

https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Plugin.Messaging.Android/EmailTask.cs

To the Syncfusion team. Adding a "Share" icon to the PDFViewer with this capability built in would be a really valuable addition.




Reply
Solomon Fried
Jan 31, 2019

I posted the aboce code but my pasted XML seems to have been encoded for me. I will try again.


A few changes needed for this to work on Android past version 24. 

1. Edit the Android manifest and withing the  <application ></application> xml node, add the following... (replace YOUR_PACKAGE_NAME with your package name.

  <provider android:name="android.support.v4.content.FileProvider"

              android:authorities="com.companyname.ControlsTester.fileprovider"

              android:exported="false"

              android:grantUriPermissions="true">

      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>

    </provider>

2. Create a folder under Resources called "xml"

3. add a file to the xml directory called file_paths.xml containing the following.. (you probably only need the "files" node)

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

  <external-path name="external" path="." />

  <external-files-path name="external_files" path="." />

  <cache-path name="cache" path="." />

  <external-cache-path name="external_cache" path="." />

  <files-path name="files" path="." />

</paths>

 

4. In the ShareAndroid class,  
replace var uri = Android.Net.Uri.Parse("file://" + filePath); with ....    var uri = FileProvider.GetUriForFile(context, context.PackageName + ".fileprovider", new File(filePath));

5. Right before context.StartActivity(chooserIntent), add the following

    chooserIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
    chooserIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);

Finally, The Save... classes are not needed and you can use Xamarin.Essentials.FileSystem.AppDataDirectory to get the platform-specific directories.

And that is all folks...

Much thanks to the Xam.Plugins.Messaging team where I found some code in the Email messaging code that helped out here.

https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Plugin.Messaging.Android/EmailTask.cs

You may want to enhance your code to handle older versions of Android (prior to 24) as well. See here for how it was done in the Email plugin



To the Syncfusion team. Adding a "Share" icon to the PDFViewer with this capability built in would be a really valuable addition.

Karthik Krishnaraj [Syncfusion]
Feb 01, 2019

Hi Solomon,

 

Thank you for your suggestion,

 

We will check through it and modify the sample accordingly.

 

Regards,

Karthik.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon