title: PdfSharp Cloning a page: An item with the same key has already been added
body: <p>I am reading in a PDF and trying to duplicate some pages in a PDF that I am creating.  However when I try to save the created PDF, an exception is thrown with the infamous </p>
  
  <blockquote>
    <p>An item with the same key has already been added</p>
  </blockquote>
  
  <p>I found the same error mentioned in <a href="http://forum.pdfsharp.com/viewtopic.php?f=2&amp;t=1406&amp;start=0#p4173" rel="nofollow noreferrer">Pdf Document Save to stream method throwing exception</a> (PdfSharp forum from 2010), but no resolution was mentioned.  and the SO <a href="https://stackoverflow.com/q/27170832/31326">c# PDFSharp - what's the point of Table.Clone?</a> appears to be the same sort of problem (but I do not see how to apply it to my case)</p>
  
  <p>My test code is:
  (Note using latest PdfSharp via Nuget)</p>
  
  <pre><code>using PdfSharp.Pdf;
  using PdfSharp.Pdf.IO;
  
  namespace TestError
  {
      public class TestCode
      {
          public void DupePages(string inFilePath, string outFilePath)
          {
              var inDoc = PdfReader.Open(inFilePath, PdfDocumentOpenMode.Import);
              var outDoc = new PdfDocument();
  
              outDoc.AddPage((PdfPage)inDoc.Pages[0].Clone());
              outDoc.AddPage((PdfPage)inDoc.Pages[0].Clone());
  
              outDoc.Save(outFilePath); // Exception thrown here
          }
      }
  }
  </code></pre>
  
  <p>And the stack trace is</p>
  
  <pre><code>System.ArgumentException occurred
    HResult=0x80070057
    Message=An item with the same key has already been added.
    Source=mscorlib
    StackTrace:
     at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
     at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
     at PdfSharp.Pdf.PdfReferenceTable.Compact()
     at PdfSharp.Pdf.PdfDocument.PrepareForSave()
     at PdfSharp.Pdf.PdfDocument.DoSave(PdfWriter writer)
     at PdfSharp.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream)
     at PdfSharp.Pdf.PdfDocument.Save(String path)
     at TestError.TestCode.DupePages(String inFilePath, String outFilePath)
  </code></pre>
  
  <p>I have checked that the clone operation is returning different objects, but somehow that is not enough for PdfSharp.</p>
  
  <p>How do I successfully clone a page?</p>