title: PdfCopyForms in ITextSharp causing a Stack Overflow error
body: <p>In this method I am trying to grab the input fields from one PDF document, paste them onto another document, and print out the result as a pdf file.  The result would be a new PDF file which has the input fields of the first PDF and the static content of the second PDF.  </p>
  
  <p>I wrote some code that I thought would perform this task, but I ran into a StackOverflow error each time "copier.close()" is executed.  This is the error that it throws:</p>
  
  <pre><code>An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>public static void AddFormFieldsFromSource(string sourcePath, string secondSourcePath, string targetPath) {
    lock (syncLock) {
  
      PdfReader.unethicalreading = true;
  
      PdfReader readerMain = new PdfReader(sourcePath);
  
      FileStream stream = new FileStream(targetPath, FileMode.Create, FileAccess.Write);
  
      PdfCopyForms copier = new PdfCopyForms(stream);
  
      PdfReader secondSourceReader = new PdfReader(secondSourcePath);
  
      copier.AddDocument(secondSourceReader);
  
      copier.CopyDocumentFields(readerMain);
  
  
  
      copier.Close();
      secondSourceReader.Close();
    }
  }
  </code></pre>
  
  <p>The sourcepath is where I get my input fields from, and my second source path is where I get my static content from.  </p>
  
  <p>The PDF I used for the SourcePath variable is located here: 
  <a href="https://www.dropbox.com/s/qcc6ug8oohqvmca/primarytwopages2.pdf" rel="nofollow">https://www.dropbox.com/s/qcc6ug8oohqvmca/primarytwopages2.pdf</a></p>
  
  <p>The PDF I use for the secondSourcePath variable is located here:
  <a href="https://www.dropbox.com/s/kx2rlhmizh46hl7/secondarytwopages.pdf" rel="nofollow">https://www.dropbox.com/s/kx2rlhmizh46hl7/secondarytwopages.pdf</a></p>
  
  <p>Also, on another note, I am using ITextSharp version 5.5.0.</p>
  
  <p>Any idea why it is throwing the StackOverflow error?  I don't make any recursive calls in my code.  My first guess is that I am trying to do this task incorrectly.  The other possibility is that perhaps ITextSharp has a bug.</p>
  
  <p>UPDATE: I downloaded the source code to the LATEST REVISION of ITextSharp (5.5.1), built a dll so I could debug, and then referenced that dll in my code.  The stack overflow error appears to occur in the class PdfIndirectReference in this method:</p>
  
  <pre><code>public class PdfIndirectReference : PdfObject {
  ....
          internal PdfIndirectReference(int type, int number, int generation) : base(0, new StringBuilder().Append(number).Append(' ').Append(generation).Append(" R").ToString()) {
          this.number = number;
          this.generation = generation;
      }
  </code></pre>
  
  <p>In the call stack of the dll code, I found that it recursively calls a method over and over again in </p>
  
  <blockquote>
    <p>itextsharp.text.pdf.PdfCopyFieldsImp.Propagate().  </p>
  </blockquote>
  
  <p>This must be why the stack overflow is occurring.</p>
  
  <p>So, it doesn't occur in my code, but rather the dll.  Any idea how to get around this?</p>