javaitext7

Set the width of the cells, using itext 7


I hope you can help me, I am looking to be able to set the width of the cells, I am using itext 7. If the string does not have space, itext puts all the text on a single line and does not add a line break to respect the width indicated.

       for(String columnName : columNames) {
            Paragraph paragraphCell = new Paragraph(columnName);
            paragraphCell.setFont(font);
            paragraphCell.setFontSize(10);
            paragraphCell.setTextAlignment(TextAlignment.CENTER);
            Cell celda1 = new Cell();
            celda1.add(paragraphCell);
            **celda1.setWidth(25);** //Here I try to set the size, it does not respect the instruction
            table.addHeaderCell(celda1);
        }

Solution

  • Set the column size for all the columns at the time of initializing the table.

      int numColumns = columNames.size();
             
      float [] columnWidths = new float[numColumns};   
      Arrays.fill(columnWidths, 25);
    
      Table table = new Table(columnWidths); 
    

    References:

    https://www.tutorialspoint.com/itext/itext_adding_table.htm

    https://javadoc.io/doc/com.itextpdf/layout/7.0.1/com/itextpdf/layout/element/Table.html#Table-float:A-