How to give fixed percentage value of column width in iText JAVA

Viewed 651

Here I am using iText 7 for generations of pdf. I have set table width 100% using table.setWidthPercent(100) and i want fixed column width which is in percentage.

I set column width like below

 float columnWidth[] = {33,34,33};
 Table table = new Table(columnWidth);

but it's not percentage when we execute it will gives 3 column with this relative width but this one is break when we give left margin to last cell.

Every Cell left,right margin is affecting column width I want fixed size of column in percentage

Here is my whole code and output image.

        String pdfPath = "first.pdf";
        PdfWriter pdfWriter = new PdfWriter(pdfPath);
        PdfDocument pdfDocument = new PdfDocument(pdfWriter);
        Document document = new Document(pdfDocument);
        pdfDocument.setDefaultPageSize(PageSize.A4);
        document.setMargins(20,20,20,20);

        float columnWidth[] = {33,34,33};
        Table table = new Table(columnWidth);
        table.setWidthPercent(100);
        table.addCell(new Cell().add("First").setFontSize(7).setMargins(0,0,0,0));
        table.addCell(new Cell().add("Second").setFontSize(7).setMargins(0,0,0,0));
        table.addCell(new Cell().add("Third").setFontSize(7).setMargins(0,0,0,50));
        document.add(table);
        document.close();
        System.out.println("pdf created");

This one is output of above code:

This one is output of above code

Want fixed column size even if apply left margin. it should affect only on text not on column Here column size is changing

Want this type of behaviour:

Want this type of behaviour

1 Answers

Instead of giving float value directly

  float columnWidth[] = {33,34,33};
  Table table = new Table(columnWidth);

UnitValue solved my query

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

Now Column width is fixed. Cell margin won't affect column width.

Related