Home > Back-end >  How to insert a large PDF table into PDF document that doesn't fit on one page using Itext?
How to insert a large PDF table into PDF document that doesn't fit on one page using Itext?

Time:01-20

I make a PDF table with three columns and the number of rows is determined by the arrays of data that I get from a device. These can vary anywhere from 1 to 200 rows.

When the data that I get is less than how many rows can fit on a page, everything works fine, but when I get a lot of data, 40 rows, I get the Document exception - infinite loop.

Here is the method where I make the table:

private static PdfPTable createTableSerial(String[] serialOcr, ArrayList<java.awt.Image> serialImage)
        throws BadElementException {
    PdfPTable table = new PdfPTable(3);

    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);

    PdfPCell c1 = new PdfPCell(new Phrase("Apoen"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Serijski broj"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Slika serijskog broja"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    table.setHeaderRows(serialImage.size() - 1);


    try {
        int i = 0;
        int j = 0;
        while (j < serialImage.size()) {
            table.addCell(serialOcr[i]);
            table.addCell(serialOcr[i   1]);
            table.addCell(Image.getInstance(serialImage.get(j), null));
            i  = 2;
            j  ;
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }


    return table;

}

And I call it later like this:

document.add(createTableSerial(serialOcr, serialImage));

I tried using the method:

table.splitLateRows(false);

But it didn't work. A version of Itext is itextpdf-5.5.13.2.jar

How can I split the table if it's bigger than one page? Can I check how much free space there is on a pdf page?

Thanks in advance.

CodePudding user response:

I tried a lot of things, but the one below is the only way I got it to work. Maybe it could be easier with a newer version of IText, but with version 5.5 that I used this is the only way I could find.

PdfPTable largeTable = createTableSerial(serialOcr, serialImage);
    List<PdfPRow> rows = largeTable.getRows();
    int rowsPerTable = 20;
    int currentRow = 0;

    while(currentRow < rows.size()){
        PdfPTable smallTable = new PdfPTable(largeTable.getNumberOfColumns());
        smallTable.setWidthPercentage(100);
        for (int i = currentRow; i < currentRow   rowsPerTable; i  ) {
            if (i >= rows.size()) {
                break;
            }
            PdfPCell[] cells = rows.get(i).getCells();
            for (PdfPCell cell : cells) {
                smallTable.addCell(cell);
            }
        }
        document.add(smallTable);
        currentRow  = rowsPerTable;
    }

I first call the method that I posted in my question and create one large table. After this I break the large table into multiple small tables and add them to the Pdf document one by one. You can adjust the int rowsPerTable = 20; value to define on how many rows will the table break.

  • Related