0

I've seen a ton of examples on the stack about converting a base64 string and/or URI but can't seem to get it to render the base64. I've tried using a uri and a string. Using the code below, i am able to render a pdf but when i try to open the pdf it says an error has occurred. Nothing is rendered. Any ideas?

string base64BinaryStr = "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlIC9YT2JqZWN0IC9T";

    using (FileStream stream = System.IO.File.Create(@"c:\Users\signature.pdf"))
    {
        byte[] byteArray = Convert.FromBase64String(base64BinaryStr);
        stream.Write(byteArray, 0, byteArray.Length);
    }

**Here's a screenshot of what the file does when i try to open it:

enter image description here

MizAkita
  • 1,115
  • 5
  • 21
  • 52
  • Are you sure the base64BinaryStr is valid? When you look at the byteArray, does it have the expected length? – mageos May 08 '15 at 22:40
  • yes it shows up 2kb and if i use a different string that number changes as expected... just wont open. I tried it with a data URI as well... it wont render. – MizAkita May 08 '15 at 22:42
  • are you sure the data is a valid PDF? It looks like you are doing everything correct. – mageos May 08 '15 at 22:43
  • it creates the pdf in the correct file location but when you try to open the pdf it says it can not be opened. – MizAkita May 08 '15 at 22:45
  • @MizAkita What is the reason for the file not being able to be opened? Access Denied? Corrupted File? – John Odom May 08 '15 at 22:49
  • @JohnOdom i just added a screenshot to the original post... – MizAkita May 08 '15 at 22:52
  • @MizAkita Have you tried using the `BinaryWriter` to write to the file like in [this link](http://stackoverflow.com/questions/1626982/convert-base64binary-to-pdf)? – John Odom May 08 '15 at 22:57
  • I think that you may need to confirm that the base64 data you have is actually a PDF. I would take a working PDF, convert it to a base64 and then see if the your algorithm will load it and convert it back to a PDF ok. – mageos May 08 '15 at 22:58
  • no my base64 is an image... but i need it converted to pdf. Just tried BinaryWriter and that didnt work either. same result. – MizAkita May 08 '15 at 23:03
  • 3
    @MizAkita You can't simply just save an image as a PDF and expect it to work. Maybe you can read up on http://stackoverflow.com/questions/1642280/jpg-to-pdf-convertor-in-c-sharp – Sandeep Bansal May 08 '15 at 23:08
  • Very good just tried itext sharp and it works like a charm. – MizAkita May 08 '15 at 23:25

0 Answers0