title: Cannot access a closed file .net MVC?
body: <p>wants to just add a text to an existing pdf using <a href="https://sourceforge.net/projects/itextsharp/" rel="nofollow noreferrer">itextsharp</a>
  .and so far I have done.my code =></p>
  
  <pre><code>    public FileStreamResult DownloadCertificate(string UserId)
      {
          //get user info using UserId from database
          //UserDetail UserDetail = db.UserDetails.Where(x =&gt; x.UserId == UserId).FirstOrDefault();
          string oldFile = Server.MapPath("~/Content/img/tsms/Certificate/Certificate-of-Completion-Award-Template-Blue.pdf");
          string newFile = Server.MapPath("~/Content/img/tsms/Certificate/newFile.pdf");
          // open the reader
          PdfReader reader = new PdfReader(oldFile);
          Rectangle size = reader.GetPageSizeWithRotation(1);
          Document document = new Document(size);
          // open the writer
          FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
          PdfWriter writer = PdfWriter.GetInstance(document, fs);
          document.Open();
          // the pdf content
          PdfContentByte cb = writer.DirectContent;
          // select the font properties
          BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
          cb.SetColorFill(BaseColor.DARK_GRAY);
          cb.SetFontAndSize(bf, 8);
          // write the text in the pdf content
          cb.BeginText();
          string text = "Some random blablablabla...";
          // put the alignment and coordinates here
          cb.ShowTextAligned(1, text, 520, 640, 0);
          cb.EndText();
          // write the text in the pdf content
          cb.BeginText();
          text = "Other random blabla...";
          // put the alignment and coordinates here
          cb.ShowTextAligned(2, text, 100, 200, 0);
          cb.EndText();
          // create the new page and add it to the pdf
          PdfImportedPage page = writer.GetImportedPage(reader, 1);
          cb.AddTemplate(page, 0, 0);
          // close the streams and voilá the file should be changed :)
          document.Close();
          fs.Close();
          writer.Close();
          reader.Close();
          return new FileStreamResult(fs, "application/pdf");
      }
  </code></pre>
  
  <p>the problem is when I am trying to access the method browser showing me error like below=>
  <a href="https://i.stack.imgur.com/txuzP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/txuzP.png" alt="enter image description here"></a></p>
  
  <p>I don't know why it's giving me this kind of error.</p>
  
  <p>I tried to find the solution. and I find....
  <a href="https://stackoverflow.com/questions/29617289/filestream-cannot-access-closed-file">FileStream &quot;cannot access closed file&quot;</a></p>
  
  <p>but that is not enough for me.</p>
  
  <p>and I also try to change some lines in my code. below =></p>
  
  <pre><code>  // close the streams and voilá the file should be changed :)
    document.Close();
    //fs.Close();
    //writer.Close();
    //reader.Close();
    return new FileStreamResult(fs, "application/pdf");
  </code></pre>
  
  <p>but that change also doesn't help me. what I have done wrong in my code.(also how can I provide a download mode to the user.)</p>