)
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: June 24, 2019).
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.Android |
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

Some adjustments to run on IOS

For IOS the popupPresentationController requires a UIView to anchor itself to.

So I changed the IShare interface to 

 public interface IFileShare
    {
        Task Show(string title, string message, string filePath, View sourceView);
    }

For Windows and Android, the implementation justs set the sourceView to null as default.

When Show is called I pass either the Page ContentView or the Button that was tapped to cause the popup to show.

The Show method in ShareIOS now becomes... (I also added a line to use the 'title' parameter as the email Subject.

public async Task Share(string title, string message, string filePath, View view)
        {
            var items = new[] { NSObject.FromObject(message), NSUrl.FromFilename(filePath) };
            var activityController = new UIActivityViewController(items, null);

            var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;

            var renderer = Platform.GetRenderer(view);

            var uiView = renderer.NativeView;
            activityController.PopoverPresentationController.SourceView = uiView;

            activityController.SetValueForKey((NSString)title, (NSString)"Subject");

            var excludedActivityTypes = new[] { UIActivityType.AssignToContact, UIActivityType.AddToReadingList, UIActivityType.CopyToPasteboard, UIActivityType.SaveToCameraRoll };

            if (excludedActivityTypes.Length > 0)
                activityController.ExcludedActivityTypes = excludedActivityTypes;

            await rootController.PresentViewControllerAsync(activityController, true);
        }



Reply
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.

Up arrow icon

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

Live Chat Icon For mobile
Live Chat Icon