title: Adding a page with PDFBox doesn't work
body: <p>I'm trying to add a page to an existing PDF-Document that I'm performing multiple different actions on before and after the page is supposed to be added.</p>
  
  <p>Currently I open the page at the beginning of the document and write stuff on the first and second page of it. On the second page I add some images aswell. The Stuff that's written on the PDFs is different per PDF and sometimes it's so much stuff that two pages (or sometimes even 3) aren't enough. Now I'm trying to add a third or even fourth page once a certain amount of written text/printed images is on the second page. </p>
  
  <p>Somehow no matter what I do, the third page I want to add doesn't show up in the final document. Here's my code to add the page:</p>
  
  <pre><code>if(doc.getNumberOfPages() &lt; p+1){
      PDDocument emptyDoc = PDDocument.load("./data/EmptyPage.pdf");
      PDPage emptyPage = (PDPage)emptyDoc.getDocumentCatalog().getAllPages().get(0);
      doc.addPage(emptyPage);;
      emptyDoc.close();
  }
  </code></pre>
  
  <p>When I check doc.getNumberOfPages() before, it says 2. Afterwards it says 3. The final document still just has 2 pages. The code after the if-clause contains multiple contentStreams that are supposed to write text on the new page (and on the first and second page).</p>
  
  <pre><code> contentStream = new PDPageContentStream(doc, (PDPage) allPages.get((int)p), true, true);
  </code></pre>
  
  <p>In the end, I save the document via </p>
  
  <pre><code>doc.save(tarFolder+nr.get(i)+".pdf");
  doc.close();
  </code></pre>
  
  <p>I've created a whole new project with a class that's supposed to do the exact same thing - add a page to another PDF. This code works perfectly fine and the third page shows up - so it seems like I'm just missing something. My code works perfectly fine for page 1 + 2, we just had the new case that we need a third/fourth page sometimes lately, so I want to integrate that into my main project.</p>
  
  <p>Here's the new project that works:</p>
  
  <pre><code>PDDocument doc = PDDocument.load("D:\\test.pdf");
  PDDocument doc2 = PDDocument.load("D:\\EmptyPage.pdf");
  
  List&lt;PDPage&gt; allPages = doc2.getDocumentCatalog().getAllPages();
  PDPage page = (PDPage) allPages.get(pageNumber);
  
  doc.addPage(page); 
  doc.save("D:\\testoutput.pdf");
  </code></pre>
  
  <p>What's weird in my main project is that the third page I add gets counted by </p>
  
  <pre><code>"getNumberOfPages()"
  </code></pre>
  
  <p>but doesn't show up in the final product. The program throws an error if I don't add the page because it tries to write content on the third page.</p>
  
  <p>Any idea what I'm doing wrong?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>If I add the page at the beginning, when my document is loaded the first time, the page gets added and exists in my final document - like this:</p>
  
  <pre><code>doc = PDDocument.load(config.getFolder("template"));
  PDDocument emptyDoc = PDDocument.load("./data/EmptyPage.pdf");
  PDPage emptyPage = (PDPage)emptyDoc.getDocumentCatalog().getAllPages().get(0);
  doc.addPage(emptyPage);
  </code></pre>
  
  <p>However, since some documents don't need that extra page, it gets unnecessarily complicated - and I feel like removing the page if it isn't needed isn't really pretty, since I'd like to avoid adding it in the first place. Maybe somebody has an idea now?</p>