how to create a pdf

Good afternoon, I am following the steps to create a pdf of these links:

https://www.syncfusion.com/kb/9174/how-to-create-a-pdf-file-in-xamarin

https://help.syncfusion.com/file-formats/pdf/create-pdf-file-in-xamarin

Even in the example project they give, I get the same problem, when pressing the button nothing happens and I see that the method is exited when it passes through the following line:


using System;
using System.IO;
using Android.Content;
using Java.IO;
using Xamarin.Forms;
using System.Threading.Tasks;


[assembly: Dependency(typeof(SaveAndroid))]

class SaveAndroid: ISave
    {
        //Method to save document as a file in Android and view the saved document
        public async Task SaveAndView(string fileName, String contentType, MemoryStream stream)
        {
            string root = null;
            //Get the root path in android device.
            if (Android.OS.Environment.IsExternalStorageEmulated)
            {
                root = Android.OS.Environment.ExternalStorageDirectory.ToString();
            }
            else
                root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            //Create directory and file 
            Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");
            myDir.Mkdir();

            Java.IO.File file = new Java.IO.File(myDir, fileName);

            //Remove if the file exists
            if (file.Exists()) file.Delete();

            //Write the stream into the file
            FileOutputStream outs = new FileOutputStream(file); //AQUI
            outs.Write(stream.ToArray());

            outs.Flush();
            outs.Close();

            //Invoke the created file for viewing
            if (file.Exists())
            {
                Android.Net.Uri path = Android.Net.Uri.FromFile(file);
                string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                Intent intent = new Intent(Intent.ActionView);
                intent.SetDataAndType(path, mimeType);
                Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
            }
        }
    }



I have already granted you the read and write permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



5 Replies

GK Gowthamraj Kumar Syncfusion Team September 1, 2020 11:58 AM UTC

Hi David, 
 
Thank you for using Syncfusion products. 
 
We have created a simple sample in our end, but it is working properly and generates the PDF properly. We were not able to reproduce the issue in our end. Please provide the permission, when it is asking for allowing the write storage permission in your Android device. We have shared the runnable sample and output document for your reference. Kindly please run this sample in your end and let us know the result.   
 
 
Please find the UG documentation link below for your reference.  
 
Note : In Android device, please ensure this location “/Syncfusion/Output.pdf” on your internal storage to find the output document. 
  
If still you are facing the same issue, Kindly please provide us the below details for further analysis and provide you a better solution on this.    
 
  1. Screenshot / video illustrating the issue  
  2. Device specification (Android) which you have facing the issue
 
Regards, 
Gowthamraj K 



DA David September 1, 2020 01:29 PM UTC

Thank you!!! I updated the SaveAndroid file of my project with the one of the sample and everything went perfectly! thank you very much.


GK Gowthamraj Kumar Syncfusion Team September 2, 2020 06:00 AM UTC

Hi David, 

Thank you for your update. We are glad to know that your problem has been solved. 

Regards, 
Gowthamraj K 



HN Huy Nguyen Xuan July 7, 2021 11:30 AM UTC

I have also encountered this problem.

I have handled it successfully.

Because I am missing the folder xml/provider_paths

And I modified this code.

Luckily it ran successfully

<application android:label="Demo.Android" android:icon="@drawable/icon">

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

        android:authorities="${applicationId}.provider"

        android:exported="false"

        android:grantUriPermissions="true">

      <meta-data android:name="android.support.FILE_PROVIDER_PATHS"

                 android:resource="@xml/provider_paths" />

    </provider>

  </application>



AA Anitha Azhagesan Syncfusion Team July 8, 2021 06:06 AM UTC

Hi David, 
  
Thanks for the update. Please let us know if you require further assistance. 
  
Regards, 
Anitha 


Loader.
Up arrow icon