How to create a PDF file in Xamarin

Hello, I am doing a basic project to create a pdf with xamarin following this link "https://www.syncfusion.com/kb/9174/how-to-create-a-pdf-file-in-xamarin". I have followed all the steps and I cannot show the pdf. I don't think I can save the pdf. The method is exited in the following line:

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); // In this line
        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"));
        }
    }

4 Replies

JA Jose Antonio March 13, 2020 06:12 PM UTC

Now if I created the PDF, it was a permission problem. 

but I still don't see it(PDF)

what´s the problem??


RB Ravikumar Baladhandapani Syncfusion Team March 16, 2020 02:43 PM UTC

Hi Jose, 
 
Thank you for using Syncfusion products. 
 
 
We have checked the reported issue.In that we have created a PDF file in Xamarin using with mentioned KB(https://www.syncfusion.com/forums/152410/how-to-create-a-pdf-file-in-xamarin) and its working fine. 
The output  document saved inside internal storage with following location , “/Syncfusion/Output.pdf”. Please find the output document in this location. 
 
Please let us know if you need any further assistance on this 
 
Regards, 
Ravikumar. 



DA David August 31, 2020 04:50 PM UTC

I have the same problem, what were the missing permissions?
Because I already gave him read and write permissions


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


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

Hi David,   
   
Thank you for your update.   
   
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 


Loader.
Up arrow icon