title: Why am I getting "Cannot access a closed Stream" here?
body: <p>Stack trace looks like </p>
  
  <blockquote>
    <p>[ObjectDisposedException: Cannot access a closed Stream.]<br>
    System.IO.__Error.StreamIsClosed() +53<br>
    System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count)
    +11411219    System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response)
    +81    System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +168<br>
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext
    controllerContext, ActionResult actionResult) +13</p>
  </blockquote>
  
  <p>after invoking </p>
  
  <pre><code>        //Byte[] bytes;
          using ( var ms = new MemoryStream() )
          {
              using ( var doc = new Document() )
              {
                  using ( var writer = PdfWriter.GetInstance(doc, ms) )
                  {
  
                      doc.Open();
                      //var example_html = @"&lt;p&gt;This &lt;em&gt;is &lt;/em&gt;&lt;span class=""headline"" style=""text-decoration: underline;""&gt;some&lt;/span&gt; &lt;strong&gt;sample &lt;em&gt; text&lt;/em&gt;&lt;/strong&gt;&lt;span style=""color: red;""&gt;!!!&lt;/span&gt;&lt;/p&gt;";
                      var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "template.html"));
                      var example_css = @".headline{font-size:200%}";
                      using ( var srHtml = new StringReader(example_html) )
                      {
                          iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                      }
                      using ( var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)) )
                      {
                          using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
                          {
                              iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                          }
                      }
  
  
                      doc.Close();
                  }
              }
              //bytes = ms.ToArray();
              return File(ms, "application/pdf", "Test.pdf");
          }
  </code></pre>
  
  <p>I've read <a href="https://stackoverflow.com/questions/10934585/memorystream-cannot-access-a-closed-stream">MemoryStream - Cannot access a closed Stream</a>, but that's not the same scenario because I'm not using <code>StreamReader</code></p>
  
  <p><strong>Edit:</strong> Still not working with </p>
  
  <pre><code>    [OutputCache(NoStore = true, Duration = 0)]
      public ActionResult Run()
      {
          Byte[] bytes;
          var ms = new MemoryStream();
          try
          {
              using (var doc = new Document())
              {
                  using (var writer = PdfWriter.GetInstance(doc, ms))
                  {
                      writer.CloseStream = false;
                      doc.Open();
                      var example_html = @"&lt;p&gt;This &lt;em&gt;is &lt;/em&gt;&lt;span class=""headline"" style=""text-decoration: underline;""&gt;some&lt;/span&gt; &lt;strong&gt;sample &lt;em&gt; text&lt;/em&gt;&lt;/strong&gt;&lt;span style=""color: red;""&gt;!!!&lt;/span&gt;&lt;/p&gt;";
                      //var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "LinkEmailTemplate.html"));
                      var example_css = @".headline{font-size:200%}";
                      using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
                      {
                          using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
                          {
                              iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                          }
                      }
                      doc.Close();
                  }
              }
              bytes = ms.ToArray();
              ms.Position = 0;
              return File(ms, "application/pdf", "Test.pdf");
          }
          catch
          {
              ms.Dispose();
              throw;
          }           
      }
  </code></pre>