Skip to content

Commit 504a9a8

Browse files
committed
Preserve cell's percent width between relayouts
DEVSIX-5158
1 parent e5a5dfd commit 504a9a8

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,12 @@ public LayoutResult layout(LayoutContext layoutContext) {
554554
}
555555

556556
LayoutResult cellResult = cell.setParent(this).layout(new LayoutContext(cellArea, null, childFloatRendererAreas, wasHeightClipped || wasParentsHeightClipped));
557-
557+
if (cellWidthProperty != null && cellWidthProperty.isPercentValue()) {
558+
cell.setProperty(Property.WIDTH, cellWidthProperty);
559+
if (null != cellResult.getOverflowRenderer()) {
560+
cellResult.getOverflowRenderer().setProperty(Property.WIDTH, cellWidthProperty);
561+
}
562+
}
558563
cell.setProperty(Property.VERTICAL_ALIGNMENT, verticalAlignment);
559564
// width of BlockRenderer depends on child areas, while in cell case it is hardly define.
560565
if (cellResult.getStatus() != LayoutResult.NOTHING) {

layout/src/test/java/com/itextpdf/layout/TableTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,28 @@ public void simpleTableTest23() throws IOException, InterruptedException {
798798
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
799799
}
800800

801+
@Test
802+
public void widthInPercentShouldBeResetAfterOverflow() throws IOException, InterruptedException {
803+
String testName = "widthInPercentShouldBeResetAfterOverflow.pdf";
804+
String outFileName = destinationFolder + testName;
805+
String cmpFileName = sourceFolder + "cmp_" + testName;
806+
807+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
808+
Document doc = new Document(pdfDoc);
809+
810+
doc.add(new Div().setHeight(730).setWidth(523));
811+
Table table = new Table(2).useAllAvailableWidth().setFixedLayout()
812+
.addCell(new Cell().add(new Paragraph("Hello")).setWidth(UnitValue.createPercentValue(20)))
813+
.addCell(new Cell().add(new Paragraph("World")).setWidth(UnitValue.createPercentValue(80)));
814+
// will be added on the first page
815+
doc.add(table);
816+
817+
// will be added on the second page
818+
doc.add(table);
819+
doc.close();
820+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
821+
}
822+
801823
@Test
802824
public void bigRowspanTest01() throws IOException, InterruptedException {
803825
String testName = "bigRowspanTest01.pdf";

0 commit comments

Comments
 (0)