40

I am trying to convert content of a file stored in a sql column to a pdf.

I use the following piece of code:

byte[] bytes;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, fileContent);
bytes = ms.ToArray();
System.IO.File.WriteAllBytes("hello.pdf", bytes);

The pdf generated is corrupt in the sense that when I open the pdf in notepad++, I see some junk header (which is same irrespective of the fileContent). The junk header is NUL SOH NUL NUL NUL ....

CharithJ
  • 44,463
  • 20
  • 111
  • 128
blue piranha
  • 3,364
  • 12
  • 48
  • 86
  • 5
    `BinaryFormatter` has *nothing whatsoever* to do with pdf. Using the two in a single sentence is a big mistake. What is `fileContent` here? (i.e. what is the *type* of that?) – Marc Gravell Jan 10 '13 at 13:34
  • Does your SQL column hold serialised a PDF file or could it by any binary data? – Tim Croydon Jan 10 '13 at 13:35
  • I use the similar method, but pdf do not open in Adobe reader in Mobile version. – Mukund Feb 15 '17 at 11:32

2 Answers2

79

You shouldn't be using the BinaryFormatter for this - that's for serializing .Net types to a binary file so they can be read back again as .Net types.

If it's stored in the database, hopefully, as a varbinary - then all you need to do is get the byte array from that (that will depend on your data access technology - EF and Linq to Sql, for example, will create a mapping that makes it trivial to get a byte array) and then write it to the file as you do in your last line of code.

With any luck - I'm hoping that fileContent here is the byte array? In which case you can just do

System.IO.File.WriteAllBytes("hello.pdf", fileContent);
Andras Zoltan
  • 41,423
  • 12
  • 100
  • 159
  • Thank you. You were right. I had defined fileContent as ds.Tables[0].Rows[0]["fileData"]. So all I did is byte[] b = (byte[])fileContent. There was NO need of BinaryFormatter. Thank you Mark and Tim! – blue piranha Jan 10 '13 at 13:54
  • i was searching for the solution however i got it here, may i know the variable type "fileContent" thanks in advance. – sidhewsar Nov 23 '16 at 14:47
  • Hi @sidhewsar - It's `byte[]` – Andras Zoltan Nov 24 '16 at 21:29
  • That's working and does what it needs to do, saves the byte content as pdf file. Be aware that this is not a "real" pdf file, as the structure is not respecting the pdf file structure. That means the if you need to open it later as pdf document, it would not be recognized as pdf file. Just a note. – tanuk Mar 03 '21 at 09:36
0

Usually this happens if something is wrong with the byte array.

File.WriteAllBytes("filename.PDF", Byte[]);

This creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

Asynchronous implementation of this is also available.

public static System.Threading.Tasks.Task WriteAllBytesAsync 
(string path, byte[] bytes, System.Threading.CancellationToken cancellationToken = null);
CharithJ
  • 44,463
  • 20
  • 111
  • 128