@@ -478,6 +478,11 @@ float[] fixedLayout() {
478
478
firtsRow = null ;
479
479
}
480
480
481
+ float [] columnWidthIfPercent = new float [columnWidths .length ];
482
+ for (int i = 0 ; i < columnWidthIfPercent .length ; i ++) {
483
+ columnWidthIfPercent [i ] = -1 ;
484
+ }
485
+ float sumOfPercents = 0 ;
481
486
if (firtsRow != null && getTable ().isComplete () && 0 == getTable ().getLastRowBottomBorder ().size ()) { // only for not large tables
482
487
for (int i = 0 ; i < numberOfColumns ; i ++) {
483
488
if (columnWidths [i ] == -1 ) {
@@ -486,9 +491,14 @@ float[] fixedLayout() {
486
491
UnitValue cellWidth = getCellWidth (cell , true );
487
492
if (cellWidth != null ) {
488
493
assert cellWidth .getValue () >= 0 ;
489
- float width = cellWidth .isPercentValue ()
490
- ? tableWidth * cellWidth .getValue () / 100
491
- : cellWidth .getValue ();
494
+ float width = 0 ;
495
+ if (cellWidth .isPercentValue ()) {
496
+ width = tableWidth * cellWidth .getValue () / 100 ;
497
+ columnWidthIfPercent [i ] = cellWidth .getValue ();
498
+ sumOfPercents += columnWidthIfPercent [i ];
499
+ } else {
500
+ width = cellWidth .getValue ();
501
+ }
492
502
int colspan = ((Cell ) cell .getModelElement ()).getColspan ();
493
503
for (int j = 0 ; j < colspan ; j ++) {
494
504
columnWidths [i + j ] = width / colspan ;
@@ -510,31 +520,31 @@ float[] fixedLayout() {
510
520
}
511
521
}
512
522
}
513
-
523
+ if (sumOfPercents > 100 ) {
524
+ warn100percent ();
525
+ }
514
526
if (remainWidth > 0 ) {
515
527
if (numberOfColumns == processedColumns ) {
516
528
//Set remaining width to all columns.
517
529
for (int i = 0 ; i < numberOfColumns ; i ++) {
518
530
columnWidths [i ] = tableWidth * columnWidths [i ] / (tableWidth - remainWidth );
519
531
}
520
- } else {
521
- // Set remaining width to the unprocessed columns.
522
- for (int i = 0 ; i < numberOfColumns ; i ++) {
523
- if (columnWidths [i ] == -1 ) {
524
- columnWidths [i ] = remainWidth / (numberOfColumns - processedColumns );
525
- }
526
- }
527
532
}
528
- } else if (numberOfColumns != processedColumns ) {
529
- // Logger logger = LoggerFactory.getLogger(TableWidths.class);
530
- // logger.warn(LogMessageConstant.SUM_OF_TABLE_COLUMNS_IS_GREATER_THAN_TABLE_WIDTH);
533
+ } else if (remainWidth < 0 ){
534
+ //Only columns with a width of percentage type should suffer.
531
535
for (int i = 0 ; i < numberOfColumns ; i ++) {
532
- if (columnWidths [i ] == -1 ) {
533
- columnWidths [i ] = 0 ;
534
- }
536
+ columnWidths [i ] += -1 != columnWidthIfPercent [i ]
537
+ ? remainWidth * columnWidthIfPercent [i ] / sumOfPercents
538
+ : 0 ;
539
+ }
540
+ }
541
+ for (int i = 0 ; i < numberOfColumns ; i ++) {
542
+ if (columnWidths [i ] == -1 ) {
543
+ columnWidths [i ] = Math .max (0 , remainWidth / (numberOfColumns - processedColumns ));
535
544
}
536
545
}
537
546
547
+ // Set remaining width to the unprocessed columns.
538
548
if (tableRenderer .bordersHandler instanceof SeparatedTableBorders ) {
539
549
for (int i = 0 ; i < numberOfColumns ; i ++) {
540
550
columnWidths [i ] += horizontalBorderSpacing ;
0 commit comments