for(int i-=0; i<firstItems.size(); i++
{
table.addCell(String.valueOf(firstItems.get(z)));

                        document.add(table);







                        document.close();


                    response.addHeader("Content-Disposition", "attachment; filename="test.pdf");
                    OutputStream out = response.getOutputStream();
                     baos.writeTo(out);
                     out.flush();
}

Hi I got an error stating the document has been closed. You can't add any Elements.

First pass through the loop you add and close, so on the secand pass through the loop the document is already closed, so the add fails