title: Convert Byte [] to PDF
body: <p>With help of this question <a href="https://stackoverflow.com/questions/2451864/c-4-0-convert-pdf-to-byte-and-vice-versa">C# 4.0: Convert pdf to byte[] and vice versa</a> i was able to convert byte[] to PDF. Byte array length is 25990 approx. When i try to open the PDF it says file is corrupted. What could be the reason?</p>
  
  <p>I tried the BinaryWriter but it creates PDF of 0 KB.</p>
  
  <p>It's a response from a Web Service</p>
  
  <p>Sample Code</p>
  
  <pre><code>WebResponse resp = request.GetResponse();
  var buffer = new byte[4096];
  Stream responseStream = resp.GetResponseStream();
  {
    int count;
    do
    {
      count = responseStream.Read(buffer, 0, buffer.Length);
      memoryStream.Write(buffer, 0, responseStream.Read(buffer, 0, buffer.Length));
    } while (count != 0);
  }
  resp.Close();
  byte[] memoryBuffer = memoryStream.ToArray();
  System.IO.File.WriteAllBytes(@"E:\sample1.pdf", memoryBuffer);
  int s = memoryBuffer.Length;
  
  BinaryWriter binaryWriter = new BinaryWriter(File.Open(@"E:\sample2.pdf", FileMode.Create));
  binaryWriter.Write(memoryBuffer);
  </code></pre>