title: Itextsharp: Error when converting directly from PdfReader to XMLDocument using ConvertToXML
body: <p>I'm writing a method to convert files and/or PdfReader to a XMLDocument</p>
  
  <p>When I try with a file, it works perfectly.
  However, I called the method using a PdfReader as a parameter and when passing through '<strong>reader.ConvertToXml(pdfReader, ms);</strong>' it throws out the error "Already closed". Any ideas</p>
  
  <pre><code>public static XmlDocument PDF_Tag_Extract(string filesource)
  {
      PdfReader pdfReader = new PdfReader(filesource);
      return PDF_Tag_Extract(pdfReader);
  }
  
  public static XmlDocument PDF_Tag_Extract(PdfReader pdfReader)
  {
  
      TaggedPdfReaderTool reader = new TaggedPdfReaderTool();
      XmlDocument XMLized_PDF = new XmlDocument();
      using (MemoryStream ms = new MemoryStream())
      {
          reader.ConvertToXml(pdfReader, ms);
          StringBuilder sb = new StringBuilder();
          foreach (byte b in ms.ToArray())
          {
              sb.Append((char)b);
          }
          var doc = XDocument.Parse(sb.ToString());
          var emptyElements = from descendant in doc.Descendants()
                              where descendant.IsEmpty || string.IsNullOrWhiteSpace(descendant.Value)
                              select descendant;
          emptyElements.Remove();
          XMLized_PDF.LoadXml(doc.ToString());
      }
      return XMLized_PDF;
  }
  </code></pre>
  
  <p>Actually, this is the code where I call the method from:</p>
  
  <pre><code>public static void Process_Trademarks(string filesource)
  {
  
      PdfReader pdfReader = new PdfReader(filesource);
      string[] PDF_TXT_page = new string[pdfReader.NumberOfPages];
      for (int i = 1; i &lt;= pdfReader.NumberOfPages; i++)
      {
          // Separa la página en un nuevo PdfReader
          MemoryStream output = new MemoryStream();
          Document document = new Document();
          PdfCopy copy = new PdfCopy(document, output);
          copy.SetTagged();
          copy.CloseStream = false;
          document.Open();
          copy.AddPage(copy.GetImportedPage(pdfReader, i, true));
          document.Close();
          PdfReader page = new PdfReader(output.ToArray());
          output.Close();
          copy.Close();
  
          // Página a XML
          try
          {
              // Identifies Section
              PDF_TXT_page[i - 1] = Common.ExtractText(page);
              // HERE IT ENTERS THE TROUBLESOME CODE.
              var xmlnodes = Common.PDF_Tag_Extract(page).DocumentElement.SelectNodes("/Sect/Table");
          }
          catch (Exception ex)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>However, I have found that if a put "PDF_TXT_page[i - 1] = Common.ExtractText(page);" after the "var xmlnodes = Common.PDF_Tag_Extract(page).DocumentElement.SelectNodes("/Sect/Table");", the code works without problems.</p>