Search code examples
javaitext7

iText 7 - set width of cells


I'm using iText 7 to generate a PDF with a table inside.

I would like to define the size of my cells.

For example I've this : enter image description here

I would like to reduce columns with numbers (07, 08 ...) and increase the others.

Here is how I create my table :

int nbColumns = 7 + planning.size() *4;
Table table = new Table(nbColumns);
table.setWidthPercent(100);
table.addCell(createCell(""));
table.addCell(createCell("Col1"));
table.addCell(createCell("Col2"));
DateFormat hourFormat = new SimpleDateFormat("HH", Locale.FRENCH);
for(Date hourDate : planning){
    table.addCell(new Cell(1,4).setTextAlignment(TextAlignment.CENTER).add(hourFormat.format(hourDate)).setFont(regular).setFontSize(10));
}

table.addCell(createCell("Long"));
table.addCell(createCell("A"));
table.addCell(createCell("B"));
table.addCell(createCell(""));

I tried using the method setWidth of Cell but it doesn't do anything.

I tried defining width of columns at creation of table :

float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};
Table table = new Table(colWidths);

But it creates a big mess with header cells one multiple lines.

Can you help me pls ?


Solution

  • If float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2}; is an array containing the relative widths you want for each column, transform them to a UnitValue percent array, and cosntruct your table using that:

    Table table = new Table(UnitValue.createPercentArray(colWidths));
    

    If you want the absolute width of a table to be respected above the widths passed for the columns, use Table#setFixedLayout().

    It might help to run and/or experiment on the following piece of code to see the effect of each different kind of declaration:

    public void createPdf(String dest) throws IOException, FileNotFoundException{
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdfDoc = new PdfDocument(writer);
        Document doc = new Document(pdfDoc);
        float tableWidth = 100f;
        //Using defaults
        doc.add(new Paragraph("default/auto-layout"));
        doc.add(new Paragraph("Using a float[]"));
        float[] colWidths = {1,2,3,4};
        Table table = new Table(colWidths);
        table.setWidthPercent(tableWidth);
        fillTable(table);
        doc.add(table);
    
        doc.add(new Paragraph("Using a UnitValue[] point array"));
        colWidths=new float[]{20f,40f,80f,160f};
        table = new Table(UnitValue.createPointArray(colWidths));
        table.setWidth(tableWidth);
        fillTable(table);
        doc.add(table);
    
        doc.add(new Paragraph("Using a UnitValue[] percent array"));
        colWidths=new float[]{1,2,3,4};
        table = new Table(UnitValue.createPercentArray(colWidths));
        table.setWidthPercent(tableWidth);
        fillTable(table);
        doc.add(table);
    
        doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
        //Using fixedLayout
        doc.add(new Paragraph("Using fixedLayout"));
        doc.add(new Paragraph("Using a float[]"));
        colWidths =new float[]{1,2,3,4};
        table = new Table(colWidths);
        table.setFixedLayout();
        table.setWidthPercent(tableWidth);
        fillTable(table);
        doc.add(table);
    
        doc.add(new Paragraph("Using a UnitValue[] point array"));
        colWidths=new float[]{20f,40f,80f,160f};
        table = new Table(UnitValue.createPointArray(colWidths));
        table.setWidthPercent(tableWidth);
        table.setFixedLayout();
        fillTable(table);
        doc.add(table);
    
        doc.add(new Paragraph("Using a UnitValue[] percent array"));
        colWidths=new float[]{1,2,3,4};
        table = new Table(UnitValue.createPercentArray(colWidths));
        table.setWidthPercent(tableWidth);
        table.setFixedLayout();
        fillTable(table);
        doc.add(table);
    
        doc.close();
    }
    
    public void fillTable(Table table){
        table.addCell("Water");
        table.addCell("Fire");
        table.addCell("Heaven");
        table.addCell("As I'm grounded here on");
        table.addCell("It's waving oceans are calling");
        table.addCell("is burning me deep inside");
        table.addCell("can wait for me");
        table.addCell("Earth");
    }