OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

setVerticalAlignment methods for Cell class causes endless loop when starting a new page

Open ObsisMc opened this issue 2 years ago • 1 comments

Describe the bug

the same as #514 but adding codes to reproduce it

  1. Create a Table instance which has at least 2 columns in document
  2. Add a Cell into the first column which should call setVerticalAlignment method, and the second column should have a cell with a large String which will make the document start a new page
  3. Run code. The program cannot stop in short time and size of the output pdf will become will large

To Reproduce

public void test() throws FileNotFoundException {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
    Table table = new Table(2);
    Cell cell = new Cell("any text");
    cell.setVerticalAlignment(VerticalAlignment.BOTTOM);
    table.addCell(cell);

    StringBuilder largeStr = new StringBuilder();
    for (int i = 0; i < 45; i++) {
        largeStr.append(String.format("this is to test-> row %d\n", i));
    }
    table.addCell(new Phrase(largeStr.toString()) );
    document.open();
    document.add(table);
    document.close();
}

Expected behavior size of output pdf should be small and it can be generated very fast

Bug Screenshots 图片

Additional context You can make i in the for loop smaller so there won't a new page and no bug will happen.

ObsisMc avatar May 26 '22 11:05 ObsisMc

I will try to fix it.

ObsisMc avatar May 26 '22 11:05 ObsisMc