The easiest way to do this is to first create the contents of the entire PDF file in memory, and then, once all the pages have been created, you need to open the PDF file in memory in pdfStamper and iterate over all the pages added to the header and footer objects are the correct coordinates.
If you do a quick Google search for adding page numbers to itextPDF, you will find a number of examples that you can quickly adapt to your needs.
The key is that this is done after the pdf is created, not before.
source share