Pier Luigi
Pier Luigi

Reputation: 7871

itext7: Why setting style in table headers propagate the style on entire table cells?

I'm playing with Table API in iText 7.2.1.

With the following code:

        Style header = new Style()
                .setBackgroundColor(new DeviceRgb(210, 210, 210))
                .setFont(getFontCardo());

        final int nCols = 4;
        Table table2 = new Table(UnitValue.createPercentArray(nCols)).useAllAvailableWidth();
        table2.setMarginLeft(36);
        table2.setMarginBottom(36);
        //one header row 
        table2.addHeaderCell("column 1").addStyle(header);
        table2.addHeaderCell("column 2");
        table2.addHeaderCell("column 3");
        table2.addHeaderCell("column 4");

        //36 rows
        for (int i = 0; i < nCols * 36; i++) {
            table2.addCell("cell " + table2.getNumberOfRows() + "," + (i % nCols));
        }

        document.add(table2);

the result is that all table cells get the style header, while I have set it only on the first header cell.

Same result if I set header style on any other header cell (or all of them), here a snapshot of PDF document:

enter image description here

Why this unexpected (for me) behavior?

Upvotes: 0

Views: 642

Answers (1)

Pier Luigi
Pier Luigi

Reputation: 7871

Found the solution, my mistake: the method chaining tecnique misled me.

In my code I actually add the style to the entiore table, not the single cell!

Just correct the code like this:

        table2.addHeaderCell(new Paragraph().addStyle(header).add("column 1"));
        table2.addHeaderCell(new Paragraph().addStyle(header).add("column 2"));
        table2.addHeaderCell(new Paragraph().addStyle(header).add("column 3"));
        table2.addHeaderCell(new Paragraph().addStyle(header).add("column 4"));

Upvotes: 1

Related Questions