title: iText 7 - Different footer on last page - Null pointer in PdfDictionary
body: <p>I am trying to implement a footer in <code>iText7</code>, the footer should be different on the last page of the document, I have added an event handler which is called when the document is closed but trying to then loop over the pages causes a null pointer exception:</p>
  
  <pre><code>java.lang.NullPointerException
  at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482)
  at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:152)
  at com.itextpdf.kernel.pdf.PdfPage.newContentStream(PdfPage.java:777)
  at com.itextpdf.kernel.pdf.PdfPage.newContentStreamBefore(PdfPage.java:212)
  </code></pre>
  
  <p>Here is the code I am trying to implement:</p>
  
  <pre><code>private void onCloseDocument(
          final PdfDocument document, final float xpos, final float ypos)
  {
  
      for (int i = 1; i &lt;= document.getPdfDocument().getNumberOfPages(); i++)
      {
          final PdfPage page = document.getPdfDocument().getPage(i);
          final PdfCanvas canvas =
                  new PdfCanvas(page.newContentStreamBefore(), page.getResources(), document.getPdfDocument());
          canvas.beginText();
          canvas.setFontAndSize(document.getFont(), FONT_SIZE);
          canvas.setLeading(14.4F);
  
          canvas.moveText(xpos, ypos);
  
          if (i == document.getPdfDocument().getNumberOfPages())
          {
              canvas.showText("Last page");
          }
          else
          {
              canvas.showText("Another page");
          }
  
          canvas.endText();
          canvas.stroke();
          canvas.release();
      }
  }
  </code></pre>
  
  <p>Initially I thought this worked as I was inserting a single area break to get the pdf to split over two pages, however when I add more content and the pages increase naturally they I get this exception, similarly I have now tested by trying to insert multiple area breaks and I get the same exception.</p>
  
  <p>Note* I have found that if I set <code>immediateFlush</code> to false when creating the document then I do not get this issue. </p>
  
  <p>Is there a major performance hit\downside to doing this?</p>