Skip to content

Commit 47f7706

Browse files
committed
Update the table's fixed-layout algorithm in case of a negative width remained.
DEVSIX-2711
1 parent 704f5dd commit 47f7706

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/TableWidths.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ float[] fixedLayout() {
478478
firtsRow = null;
479479
}
480480

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;
481486
if (firtsRow != null && getTable().isComplete() && 0 == getTable().getLastRowBottomBorder().size()) { // only for not large tables
482487
for (int i = 0; i < numberOfColumns; i++) {
483488
if (columnWidths[i] == -1) {
@@ -486,9 +491,14 @@ float[] fixedLayout() {
486491
UnitValue cellWidth = getCellWidth(cell, true);
487492
if (cellWidth != null) {
488493
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+
}
492502
int colspan = ((Cell) cell.getModelElement()).getColspan();
493503
for (int j = 0; j < colspan; j++) {
494504
columnWidths[i + j] = width / colspan;
@@ -510,31 +520,31 @@ float[] fixedLayout() {
510520
}
511521
}
512522
}
513-
523+
if (sumOfPercents > 100) {
524+
warn100percent();
525+
}
514526
if (remainWidth > 0) {
515527
if (numberOfColumns == processedColumns) {
516528
//Set remaining width to all columns.
517529
for (int i = 0; i < numberOfColumns; i++) {
518530
columnWidths[i] = tableWidth * columnWidths[i] / (tableWidth - remainWidth);
519531
}
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-
}
527532
}
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.
531535
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));
535544
}
536545
}
537546

547+
// Set remaining width to the unprocessed columns.
538548
if (tableRenderer.bordersHandler instanceof SeparatedTableBorders) {
539549
for (int i = 0; i < numberOfColumns; i++) {
540550
columnWidths[i] += horizontalBorderSpacing;

0 commit comments

Comments
 (0)