title: How do you create a PDF document using iText that has pages with differing page sizes?
body: <p>I am trying to create a PDF document using iText that has pages with differing page sizes. (That is, the PDF document may have several pages, some legal size, some letter size, in no particular order.)  </p>
  
  <p>Here is the code that I have that isn't working:  </p>
  
  <pre><code>Document pdfDocument = new Document();
  PdfWriter writer = PdfWriter.getInstance(pdfDocument, ServletActionContext.getResponse().getOutputStream());
  pdfDocument.open();
  
  for (int i = 0; i &lt; 10; i++) {
  
    Rectangle pageSize = PageSize.LETTER;
  
    // Code removed... this is where I calculate what the pageSize is supposed to be... either PageSize.LETTER or PageSize.LEGAL
  
    pdfDocument.setPageSize(pageSize);
    pdfDocument.newPage();
    writer.setPageEmpty(false);
  
    // Code removed... this is where I add images to the page
  
  }
  
  pdfDocument.close();
  </code></pre>