title: Convert Base64 encoded string to PDF
body: <p>I have Base64 string that represents a PDF file which needs to be converted to a PDF file, and open with the default PDF reader or browser in C#.</p>
  
  <p>I have written only the part of Base64 string because it's too long string to paste here.</p>
  
  <pre><code>public void DownloadPDF()
  {
      string pdflocation = "E:\\";
      string fileName = "softcore.pdf";
      // This is only part of Base64 string
      var base64String = "JVBERi0xLjQKJdP0zOEKMSAwIue704O58dOXPgst+hmQ+laj/"; 
      int mod4 = base64String.Length % 4;
      if (mod4 &gt; 0)
      {
          base64String += new string('=', 4 - mod4);
      }
      byte[] data = Convert.FromBase64String(base64String);        
  
      if (Directory.Exists(pdflocation))
      {
          pdflocation = pdflocation + fileName;
          using (MemoryStream Writer = new System.IO.MemoryStream())
          {
              Response.ContentType = "application/pdf";
              Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
              Response.AddHeader("content-length", data.Length.ToString());
              Writer.WriteTo(Response.OutputStream);
              Response.Flush();
              Response.End();
              //Writer.Write(data, 0, data.Length);
          }                
      }
  }
  </code></pre>
  
  <p>The problem I'm facing is that it shows as pdf generating but at the end, it says Network error.</p>
  
  <p>The decoded string from the input encoded Base64 string:
  <a href="https://i.stack.imgur.com/SM2GV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SM2GV.png" alt="enter image description here"></a></p>