2X faster development
The ultimate Xamarin UI toolkit to boost your development speed.
The PDF document in the Xamarin PDF viewer is saved in the application level. PDF viewer returns the PDF document along with the changes made (annotations addition and modification) as stream when tapping the save button in the default toolbar as an event argument of the DocumentSaveInitiated event. This stream is saved as a file locally in Xamarin Forms Android, iOS, and UWP platforms by using DependencyService in Xamarin.Forms. These platform specific saving options are used by defining an interface in the portable project. Refer to the following code snippets.
Portable Define the following interface.
C# namespace SavePDFUsingNative { public interface ISave { string Save(MemoryStream fileStream); } }
Load the PDF to PdfViewer and invoke save method with the stream obtained from the DocumentSaveInitiated event using DependencyService. Then, give the name of the interface as type parameter to the method DependencyService.Get<T>().
C# public MainPage() { InitializeComponent(); m_pdfDocumentStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SavePDFUsingNative.Assets.GIS Succinctly.pdf"); pdfViewerControl.LoadDocument(m_pdfDocumentStream); pdfViewerControl.DocumentSaveInitiated += PdfViewerControl_DocumentSaveInitiated; } private void PdfViewerControl_DocumentSaveInitiated(object sender, Syncfusion.SfPdfViewer.XForms.DocumentSaveInitiatedEventArgs args) { string filePath = DependencyService.Get<ISave>().Save(args.SaveStream as MemoryStream); string message = "The PDF has been saved to " + filePath; DependencyService.Get<IAlertView>().Show(message); }
Implement the interface in the platform projects with the available platform specific saving options.
Android
C# [assembly:Dependency(typeof(SavePDFUsingNative.Droid.SaveAndroid))] namespace SavePDFUsingNative.Droid { public class SaveAndroid : ISave { public string Save(MemoryStream stream) { string root = null; string fileName = "SavedDocument.pdf"; if (Android.OS.Environment.IsExternalStorageEmulated) { root = Android.OS.Environment.ExternalStorageDirectory.ToString(); } Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion"); myDir.Mkdir(); Java.IO.File file = new Java.IO.File(myDir, fileName); string filePath = file.Path; if (file.Exists()) file.Delete(); Java.IO.FileOutputStream outs = new Java.IO.FileOutputStream(file); outs.Write(stream.ToArray()); var ab = file.Path; outs.Flush(); outs.Close(); return filePath; } } }
iOS
C# [assembly: Dependency(typeof(SavePDFUsingNative.iOS.SaveiOS))] namespace SavePDFUsingNative.iOS { public class SaveiOS : ISave { public string Save(MemoryStream fileStream) { string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string filepath = Path.Combine(path, "SavedDocument.pdf"); FileStream outputFileStream = File.Open(filepath, FileMode.Create); fileStream.Position = 0; fileStream.CopyTo(outputFileStream); outputFileStream.Close(); return filepath; } } }
UWP
C# [assembly: Dependency(typeof(SavePDFUsingNative.UWP.SaveWindows))] namespace SavePDFUsingNative.UWP { public class SaveWindows : ISave { MemoryStream stream; StorageFolder folder; StorageFile file; public string Save(MemoryStream documentStream) { documentStream.Position = 0; stream = documentStream; SaveAsync(); return Path.Combine(ApplicationData.Current.LocalFolder.Path, "SavedDocument.pdf"); } private async void SaveAsync() { folder = ApplicationData.Current.LocalFolder; file = await folder.CreateFileAsync("SavedDocument.pdf", CreationCollisionOption.ReplaceExisting); if (file != null) { Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite); Stream st = fileStream.AsStreamForWrite(); st.SetLength(0); st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length); st.Flush(); st.Dispose(); fileStream.Dispose(); } } } }
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SavePDFUsingNative18258816
|
2X faster development
The ultimate Xamarin UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
this is very helpful information! for beginners and want to save time, I found zetpdf.com i heard a lot of this from my co workers. i tried using it nad it's really convenient and easy to use!
The sample crashes when I click save on android.
Hi White,
We need to provide the storage access permission to the save the PDF document in local storage of your device running on Android M or greater. Please find the modified sample in the below link for your reference.
http://www.syncfusion.com/downloads/support/directtrac/general/ze/SavePDFUsingNative18258816
Regards,
Sathish
It so happened when [assembly:Dependency(typeof(SavePDFUsingNative.Droid.SaveAndroid))]
namespace SavePDFUsingNative.Droid { public class SaveAndroid : ISave { public string Save(MemoryStream stream) { string root = null; string fileName = "SavedDocument.pdf"; if (Android.OS.Environment.IsExternalStorageEmulated) { root = Android.OS.Environment.ExternalStorageDirectory.ToString(); } Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion"); myDir.Mkdir(); Java.IO.File file = new Java.IO.File(myDir, fileName); string filePath = file.Path; if (file.Exists()) file.Delete(); Java.IO.FileOutputStream outs = new Java.IO.FileOutputStream(file); outs.Write(stream.ToArray()); var ab = file.Path; outs.Flush(); outs.Close(); return filePath; } } }
in "if (file.Exists()) file.Delete();" crash because I need to manually allow the my APP to access storage. Idk. but it happened there..
Hi white,
We tried to reproduce the issue “Application crashes while saving the PDF document using PDF Viewer in Xamarin.Forms.Android” , but it is working fine as expected. Kindly share the following details to analyze more on this issue and it will be helpful for us to analyze more on this issue.
With Regards, Gayathri R
When I try to save the document using the below code:
fileStream.CopyTo(stream); --> stream is empty. However fileStream has data. Do I need to set any special permission on iOS simulator?
Hi sri velic,
We have tested your code snippet and we found that you are setting stream.Position = 0 in save method.But we need to set fileStream.Position = 0 to save the file. Please find the updated code snippet below,
public string Save(MemoryStream fileStream, string fileName) { string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string filePath = Path.Combine(path, fileName + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf");
Please let us know if you are facing any other issue.
Regards, Sarath Kumar.