title: Java pdfBox: Fill out pdf form, append it to pddocument, and repeat
body: <p>I have a pdf form made and I'm trying to use pdfBox to fill in the form and print the document. I got it working great for 1 page print jobs but i had to try and modify for multiple pages. Basically it's a form with basic info up top and a list of contents. Well if the contents are larger than what the form has room for I have to make it a multiple page document. I end up with a document with a nice page one and then all the remaining pages are the blank template. What am I doing wrong?</p>
  
  <pre><code>PDDocument finalDoc = new PDDocument();
  File template = new File("path/to/template.pdf");
  
  //Declare basic info to be put on every page
  String name = "John Smith";
  String phoneNum = "555-555-5555";
  //Get list of contents for each page
  List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; pageContents = methodThatReturnsMyInfo();
  
  for (List&lt;Map&lt;String, String&gt;&gt; content : pageContents) {
      PDDocument doc = new PDDocument().load(template);
      PDDocumentCatlog docCatalog = doc.getDocumentCatalog();
      PDAcroForm acroForm = docCatalog.getAcroForm();
  
      acroForm.getField("name").setValue(name);
      acroForm.getField("phoneNum").setValue(phoneNum);
  
      for (int i=0; i&lt;content.size(); i++) {
          acroForm.getField("qty"+i).setValue(content.get(i).get("qty"));
          acroForm.getField("desc"+i).setValue(content.get(i).get("desc"));
      }
  
      List&lt;PDPage&gt; pages = docCatalog.getAllPages();
      finalDoc.addPage(pages.get(0));
  }
  
  //Then prints/saves finalDoc
  </code></pre>