title: iTextSharp 5.5.6 PdfCopy Failing with "Cannot access a closed file"
body: <p>This seems to be similar to this question: <a href="https://stackoverflow.com/questions/19839445/merging-tagged-pdf-without-ruining-the-tags">Merging Tagged PDF without ruining the tags</a></p>
  
  <p>I'm using the latest iTextSharp NuGet package (v5.5.6) trying to merge two tagged PDFs. When calling <code>Document.Close()</code> I'm getting an <code>ObjectDisposedException</code> originating from <code>PdfCopy.FlushIndirectObjects()</code>.</p>
  
  <pre><code>at System.IO.__Error.FileNotOpen()
  at System.IO.FileStream.get_Position()
  at iTextSharp.text.io.RAFRandomAccessSource.Get(Int64 position, Byte[] bytes, Int32 off, Int32 len) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\io\RAFRandomAccessSource.cs:line 96
  at iTextSharp.text.io.IndependentRandomAccessSource.Get(Int64 position, Byte[] bytes, Int32 off, Int32 len) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\io\IndependentRandomAccessSource.cs:line 76
  at iTextSharp.text.pdf.RandomAccessFileOrArray.Read(Byte[] b, Int32 off, Int32 len) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\RandomAccessFileOrArray.cs:line 235
  at iTextSharp.text.pdf.RandomAccessFileOrArray.ReadFully(Byte[] b, Int32 off, Int32 len) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\RandomAccessFileOrArray.cs:line 264
  at iTextSharp.text.pdf.RandomAccessFileOrArray.ReadFully(Byte[] b) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\RandomAccessFileOrArray.cs:line 254
  at iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw(PRStream stream, RandomAccessFileOrArray file) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfReader.cs:line 2406
  at iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw(PRStream stream) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfReader.cs:line 2443
  at iTextSharp.text.pdf.PRStream.ToPdf(PdfWriter writer, Stream os) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PRStream.cs:line 224
  at iTextSharp.text.pdf.PdfIndirectObject.WriteTo(Stream os) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfIndirectObject.cs:line 157
  at iTextSharp.text.pdf.PdfWriter.PdfBody.Write(PdfIndirectObject indirect, Int32 refNumber, Int32 generation) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfWriter.cs:line 389
  at iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, Int32 refNumber, Int32 generation, Boolean inObjStm) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfWriter.cs:line 379
  at iTextSharp.text.pdf.PdfCopy.WriteObjectToBody(PdfIndirectObject objecta) in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfCopy.cs:line 1238
  at iTextSharp.text.pdf.PdfCopy.FlushIndirectObjects() in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfCopy.cs:line 1186
  at iTextSharp.text.pdf.PdfCopy.FlushTaggedObjects() in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfCopy.cs:line 884
  at iTextSharp.text.pdf.PdfDocument.Close() in d:\Downloads\itextsharp-master\src\core\iTextSharp\text\pdf\PdfDocument.cs:line 825
  </code></pre>
  
  <p>Here is the code that is producing the exception. If I don't call <code>copy.SetTagged()</code> and don't pass true as the third argument to <code>GetImportedPage()</code> the code executes without exception, but ignores all tagging.</p>
  
  <pre><code>using(var ms = new MemoryStream())
  {
      var doc = new Document();
      var copy = new PdfSmartCopy(doc, ms);
      copy.SetTagged();
      doc.Open();
  
      string[] files = new string[]{@"d:\tagged.pdf", @"d:\tagged.pdf"};
      foreach(var f in files)
      {
          var reader = new PdfReader(f);
          int pages = reader.NumberOfPages;
          for(int i = 0; i &lt; pages;)
              copy.AddPage(copy.GetImportedPage(reader, ++i, true));
          copy.FreeReader(reader);
          reader.Close();
      }
  
      // ObjectDisposedException
      doc.Close();
  
      ms.Flush();
      File.WriteAllBytes(@"d:\pdf.merged.v5.pdf", ms.ToArray());
  }
  </code></pre>
  
  <p>Looking at the 5.5.6 source branch it looks like RAFRandomAccessSource.cs line 96 is the culprit.</p>
  
  <pre><code>public virtual int Get(long position, byte[] bytes, int off, int len) {
     if (position &gt; length)
        return -1;
  
     // Not thread safe!
     if (raf.Position != position)
  </code></pre>
  
  <p>raf.Position has been disposed at this point, but I can't tell from where it has been disposed.</p>
  
  <p>I'm hoping that I just need to do something more than simply call <code>copy.SetTagged()</code> and pass <code>true</code> to <code>GetImportedPage()</code> to fix the issue.</p>