z8manu8z
z8manu8z

Reputation: 15

Itext LargeElement and splitRows option

I'm trying to create a pdf containing a big table with itext v5.5.3.

I need :

With the code below, everything works fine :

    Document document = new Document(PageSize.LETTER);
    PdfWriter.getInstance(document, baos);

    document.open();
    PdfPTable table = new PdfPTable(5);
    table.setHeaderRows(1);
    //table.setSplitRows(false);
    //table.setComplete(false);

    for (int i = 0; i < 5; i++) {table.addCell("Header " + i);}     

    for (int i = 0; i < 500; i++) {
        table.addCell("Test " + i);
        //if (i%5 == 0) {document.add(table);}
     }

    //table.setComplete(true);
    document.add(table);
    document.close();

If I add memory management (adding table to document every 5 cells), I'm losing first header :

    Document document = new Document(PageSize.LETTER);
    PdfWriter.getInstance(document, baos);

    document.open();
    PdfPTable table = new PdfPTable(5);
    table.setHeaderRows(1);
    //table.setSplitRows(false);
    table.setComplete(false);

    for (int i = 0; i < 5; i++) {table.addCell("Header " + i);}     

    for (int i = 0; i < 500; i++) {
        table.addCell("Test " + i);
        if (i%5 == 0) {document.add(table);}
    }

    table.setComplete(true);
    document.add(table);
    document.close();

If i add memory management + setSplitRows(false), I lose all headers and some lines :

    Document document = new Document(PageSize.LETTER);
    PdfWriter.getInstance(document, baos);

    document.open();
    PdfPTable table = new PdfPTable(5);
    table.setHeaderRows(1);
    table.setSplitRows(false);
    table.setComplete(false);

    for (int i = 0; i < 5; i++) {table.addCell("Header " + i);}     

    for (int i = 0; i < 500; i++) {
        table.addCell("Test " + i);
        if (i%5 == 0) {document.add(table);}
    }

    table.setComplete(true);
    document.add(table);
    document.close();

I've done some tests and with itext version 2.1.7.js2, all three test cases are working correctly.

So, bug or not bug ? What can I do to make this working ?

Thanks

Upvotes: 0

Views: 968

Answers (2)

Micha&#235;l Demey
Micha&#235;l Demey

Reputation: 1577

This was indeed a regression, so I've added a fix for this issue to the iText library. (Revision 6630).

It will be included in the 5.5.4 release, which is due in two-three weeks.

Upvotes: 1

blagae
blagae

Reputation: 2392

Your problem does not present itself if you replace

if (i % 5 == 0) {
    document.add(table);
}

by

int x = 5; // or any other value higher than your PdfPTable number of columns
if (i % 10 == x) {
    document.add(table);
}

The apparent reason for this problem is that iText doesn't want to include a table with only a header and one partial row. This is currently a workaround, but we're looking to find the core issue and to fix it.

Upvotes: 2

Related Questions