Xamarin Android Convert Byte array to PDF file

private void ConvertToFile(string fileName , string filePath,Byte[] Bytes){

    if (!File.Exists (filePath)) {
        File.WriteAllBytes(filePath, Bytes);

    }

}

Above mentioned code is giving me Invalid format PDF

Answers

  • YkshLeoYkshLeo USMember ✭✭✭✭
    edited January 2016

    @ManikMahendru,

    Try something like this :

        Java.IO.File dir = Environment.GetExternalStoragePublicDirectory ();
        Java.IO.File assist = new Java.IO.File ("/mnt/sdcard/Sample.pdf");
        try
        {
            Java.IO.InputStream fis = new Java.IO.InputStream(assist);
            long length =assist.Length();
            if(length>int.MaxValue)
            {
                Android.Util.Log.Error("Warning","Sorry! Your given file is too large.");
            }
            byte[] bytes = new byte[(int)length];
            int offset = 0;
            int numRead = 0;
            while(offset < bytes.Length && (numRead=fis.Read(bytes, offset, bytes.Length-offset)) >= 0)
            {
                offset+=numRead;
            }
            Java.IO.File data = new Java.IO.File(dir,"mydemo.pdf");
            Java.IO.OutputStream outPut = new Java.IO.OutputStream(data);
            outPut.Write(bytes);
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine (ex.StackTrace);
        } 
    
Sign In or Register to comment.