title: C# - Xamarin.Forms - Can not access a closed Stream
body: <p>I am using itext7 in Xamarin.Forms application. I am trying to take a PDF file from a Stream, use PdfAcroForm in itext7 to write to it, but when I do I get this error: Can not access a closed Stream. I feel I have tried everything, here is my original code:</p>
  
  <pre><code>MemoryStream outFile = new MemoryStream();
  
  var streamFile = await App.GraphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();
  
  PdfDocument pdf = new PdfDocument(new PdfReader(streamFile), new PdfWriter(outFile));
  
  PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
  
  IDictionary&lt;String, PdfFormField&gt; fields = form.GetFormFields();
  
  PdfFormField toSet;
  fields.TryGetValue("Full_Names", out toSet);
  toSet.SetValue("aaa");
  
  form.FlattenFields();
  pdf.Close();
  
  await App.GraphClient.Me.Drive.Items[newFolder.Id].ItemWithPath(item.Name).Content.Request().PutAsync&lt;DriveItem&gt;(outFile);
  </code></pre>
  
  <p>Then I tried with using and set the position of the stream to 0 and flush my stream and that still did not work.</p>
  
  <pre><code>using (MemoryStream outFile = new MemoryStream())
  {
      var streamFile = await App.GraphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();
  
      PdfDocument pdf = new PdfDocument(new PdfReader(streamFile), new PdfWriter(outFile));
  
      PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
  
      IDictionary&lt;String, PdfFormField&gt; fields = form.GetFormFields();
  
      PdfFormField toSet;
      fields.TryGetValue("Full_Names", out toSet);
      toSet.SetValue("aaa");
  
      form.FlattenFields();
      pdf.Close();
  
      outFile.Flush();
      outFile.Position = 0;
  
      await App.GraphClient.Me.Drive.Items[newFolder.Id].ItemWithPath(item.Name).Content.Request().PutAsync&lt;DriveItem&gt;(outFile);
  }
  </code></pre>
  
  <p>When I remove this line...pdf.Close(); the pdf get written, but when I open it in OneDrive its corrupted...What am I doing wrong? </p>