title: Exception when attempting to generate variable-page PDF with iText
body: <p>I'm trying to create an auto-filled PDF of a government payroll form, which involves the possibility of a variable number of pages. I'm currently storing each page as a Map, with the keys being the names of the fields and the values being their contents.
  At the moment, I have this code:</p>
  
  <pre><code>in = new FileInputStream(inputPDF);
  PdfCopyFields adder = new PdfCopyFields(outStream);
  PdfReader reader = null;
  PdfStamper stamper = null;
  ByteArrayOutputStream baos = null;
  for (int pageNum = 0; pageNum &lt; numPages; pageNum++) {
      reader = new PdfReader(in);
      baos = new ByteArrayOutputStream();
      stamper = new PdfStamper(reader, baos);
      AcroFields form = stamper.getAcroFields();
      Map&lt;String, String&gt; page = pages.get(pageNum);
      setFieldsToPage(form, pageNum);
      populatePage(form, page, pageNum);
      stamper.close();
      reader = new PdfReader(baos.toByteArray());
      adder.addDocument(reader);
  }
  </code></pre>
  
  <p>The methods called are:</p>
  
  <pre><code>private void populatePage(AcroFields form, Map&lt;String, String&gt; pageMap, int pageNum) throws Exception {
      ArrayList&lt;String&gt; fieldNames = new ArrayList&lt;String&gt;();
      for (String key : pageMap.keySet()) {
          fieldNames.add(key);
      }
      for (String key : fieldNames) {
          form.setField(key + pageNum, pageMap.get(key));
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>private void setFieldsToPage(AcroFields form, int pageNum) {
      ArrayList&lt;String&gt; fieldNames = new ArrayList&lt;String&gt;();
      Map&lt;String, AcroFields.Item&gt; fields = form.getFields();
      for (String fieldName : fields.keySet()) {
          fieldNames.add(fieldName);
      }
      for (String fieldName : fieldNames) {
          form.renameField(fieldName, fieldName + pageNum);
      }
  }
  </code></pre>
  
  <p>The issue is that this throws an exception on the second iteration through the loop: at <code>reader = new PdfReader(in);</code> I get the following exception: 
  <code>java.io.IOException: PDF header signature not found.</code>
  What am I doing wrong here, and how do I fix it?</p>
  
  <p>EDIT:
  Here is the exception:</p>
  
  <pre><code>java.io.IOException: PDF header signature not found.
    at com.lowagie.text.pdf.PRTokeniser.checkPdfHeader(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readPdf(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.&lt;init&gt;(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.&lt;init&gt;(Unknown Source)
  </code></pre>
  
  <p>By the way, I'm sorry if the formatting is bad - this is my first time using stackoverflow.</p>