title: Is there a bug in PdfPTable in iText 5.5.4+?
body: <p>There seems to be a bug since iText 5.5.4 or at least a change in default behavior when working with tables and the setComplete() method.</p>
  
  <p>If I run the code below with iText 5.5.4 the resulting pdf will have pages with exactly 20 rows per page (except the last page but one). Same results with iText 5.5.5. With iText 5.5.3 the number of rows per page was fitted to the height of the page resulting in a smaller number of total pages.</p>
  
  <p>Here is an example program to reproduce the behavior:</p>
  
  <pre><code>import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.Random;
  
  import com.itextpdf.text.BaseColor;
  import com.itextpdf.text.Document;
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.Font;
  import com.itextpdf.text.FontFactory;
  import com.itextpdf.text.PageSize;
  import com.itextpdf.text.Phrase;
  import com.itextpdf.text.pdf.BaseFont;
  import com.itextpdf.text.pdf.PdfPCell;
  import com.itextpdf.text.pdf.PdfPTable;
  import com.itextpdf.text.pdf.PdfWriter;
  
  public class PdfPTableTest {
  
      private File file;
      private Font font;
  
      private Random rand;
  
      public PdfPTableTest() {
          file = new File("out.pdf");
          rand = new Random();
          font = FontFactory.getFont(FontFactory.HELVETICA, BaseFont.WINANSI,
                  BaseFont.NOT_EMBEDDED, 12, Font.NORMAL, BaseColor.BLACK, true);
      }
  
      public void run() {
          try (
                  FileOutputStream fos = new FileOutputStream(file);
                  BufferedOutputStream bos = new BufferedOutputStream(fos)
              )
          {
              Document document = new Document(PageSize.A4.rotate());
              PdfWriter.getInstance(document, bos);
              document.addTitle("itext PdfPTable setComplete() test");
              document.addCreator("test");
              document.addAuthor("test");
              document.addSubject("test");
  
              document.open();
  
              float[] widths = new float[] {50f, 50f};
              PdfPTable table = new PdfPTable(widths.length);
              table.setComplete(false);
              table.setWidths(widths);
              table.setWidthPercentage(100);
  
              PdfPCell cell = new PdfPCell(new Phrase("Column #1", font));
              table.addCell(cell);
              cell = new PdfPCell(new Phrase("Column #2", font));
              table.addCell(cell);
              table.setHeaderRows(1);
  
              for (int i = 0; i &lt; 250; i++) {
                  cell = new PdfPCell(new Phrase("Table cell #" + i, font));
                  table.addCell(cell);
                  String s = getRandomText();
                  cell = new PdfPCell(new Phrase(s, font));
                  table.addCell(cell);
                  if (i % 20 == 0) {
                      document.add(table);
                  }
              }
  
              table.setComplete(true);
              document.add(table);
              document.close();
  
          } catch (DocumentException | IOException e) {
              e.printStackTrace();
          }
      }
  
      public String getRandomText() {
          int length = rand.nextInt(100);
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i &lt; length; i++) {
              if (i % 15 == 0) {
                  sb.append(" ");
              }
              sb.append(Integer.valueOf(rand.nextInt(10)));
          }
          return sb.toString();
      }
  
      public static void main(String[] args) {
          PdfPTableTest pdfTest = new PdfPTableTest();
          pdfTest.run();
      }
  
  }
  </code></pre>
  
  <p>If I increase the max. length of the random generated text to 120 characters </p>
  
  <pre><code>int length = rand.nextInt(120);
  </code></pre>
  
  <p>then also an exception gets thrown:</p>
  
  <pre><code>com.itextpdf.text.DocumentException: com.itextpdf.text.DocumentException: Infinite table loop; row content is larger than the page (for instance because you didn't scale an image).
      at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:809)
      at com.itextpdf.text.Document.add(Document.java:278)
      at de.itext.test.PdfPTableTest.run(PdfPTableTest.java:69)
      at de.itext.test.PdfPTableTest.main(PdfPTableTest.java:96)
  Caused by: com.itextpdf.text.DocumentException: Infinite table loop; row content is larger than the page (for instance because you didn't scale an image).
      at com.itextpdf.text.pdf.PdfDocument.addPTable(PdfDocument.java:2679)
      at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:750)
      ... 3 more
  </code></pre>
  
  <p>In iText 5.5.3 there is no exception and the pdf looks fine (resulting in cells with 3 separate lines).</p>
  
  <p>Can anyone confirm that this is a bug (that will get fixed in a future release)?</p>