Skip to content

Commit 9d27a8e

Browse files
committed
Clone cell's styles during Cell#clone. Add a test. Suggested by Richard Cohn
1 parent 2249c87 commit 9d27a8e

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

layout/src/main/java/com/itextpdf/layout/element/Cell.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This file is part of the iText (R) project.
5656
import java.text.MessageFormat;
5757
import java.util.ArrayList;
5858
import java.util.HashMap;
59+
import java.util.LinkedHashSet;
5960

6061
/**
6162
* A {@link Cell} is one piece of data in an enclosing grid, the {@link Table}.
@@ -195,6 +196,7 @@ public Cell clone(boolean includeContent) {
195196
newCell.row = row;
196197
newCell.col = col;
197198
newCell.properties = new HashMap<>(properties);
199+
newCell.styles = new LinkedHashSet<>(styles);
198200
if (includeContent) {
199201
newCell.childElements = new ArrayList<>(childElements);
200202
}

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,37 @@ public void splitTableOnShortPage() throws IOException, InterruptedException {
12061206
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
12071207
}
12081208

1209+
@Test
1210+
public void splitCellWithStyles() throws IOException, InterruptedException {
1211+
String testName = "splitCellWithStyles.pdf";
1212+
String outFileName = destinationFolder + testName;
1213+
String cmpFileName = sourceFolder + "cmp_" + testName;
1214+
1215+
String text = "Make Gretzky Great Again";
1216+
1217+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
1218+
Document doc = new Document(pdfDoc, PageSize.A7);
1219+
1220+
Table table = new Table(2)
1221+
.setBorder(Border.NO_BORDER)
1222+
.setMarginTop(10)
1223+
.setMarginBottom(10);
1224+
Style cellStyle = new Style();
1225+
cellStyle.setBorderLeft(Border.NO_BORDER)
1226+
.setBorderRight(Border.NO_BORDER)
1227+
.setBorderTop(new SolidBorder(Color.BLUE, 1))
1228+
.setBorderBottom(new SolidBorder(Color.BLUE, 1));
1229+
for (int i = 0; i < 10; i++) {
1230+
table.addCell(new Cell().add(Integer.toString(i)).addStyle(cellStyle));
1231+
table.addCell(new Cell().add(text).addStyle(cellStyle));
1232+
}
1233+
1234+
doc.add(table);
1235+
1236+
doc.close();
1237+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
1238+
}
1239+
12091240
@Test
12101241
public void imageInTableTest_HA() throws IOException, InterruptedException {
12111242
String testName = "imageInTableTest_HA.pdf";
@@ -1253,6 +1284,28 @@ public void cellAlignmentAndSplittingTest01() throws IOException, InterruptedExc
12531284
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
12541285
}
12551286

1287+
1288+
@Test
1289+
public void memoryTest01() throws IOException, InterruptedException {
1290+
String testName = "memoryTest01.pdf";
1291+
String outFileName = destinationFolder + testName;
1292+
String cmpFileName = sourceFolder + "cmp_" + testName;
1293+
1294+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
1295+
Document doc = new Document(pdfDoc);
1296+
1297+
Table table = new Table(5);
1298+
for (int i = 0; i < 20000; i++) {
1299+
for (int j = 0; j < 5; j++) {
1300+
table.addCell(j + " Liberté!\nÉgalité!\nFraternité!");
1301+
}
1302+
}
1303+
doc.add(table);
1304+
1305+
doc.close();
1306+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, testName + "_diff"));
1307+
}
1308+
12561309
@Test
12571310
public void cellAlignmentAndKeepTogetherTest01() throws IOException, InterruptedException {
12581311
String testName = "cellAlignmentAndKeepTogetherTest01.pdf";
@@ -1549,7 +1602,7 @@ public void tableWithHeaderInTheBottomOfPageTest() throws IOException, Interrupt
15491602
doc.add(new Paragraph("Text"));
15501603
}
15511604

1552-
Table table = new Table(UnitValue.createPercentArray(new float[] {10, 10}));
1605+
Table table = new Table(UnitValue.createPercentArray(new float[]{10, 10}));
15531606
table.addHeaderCell(new Cell().add("Header One"));
15541607
table.addHeaderCell(new Cell().add("Header Two"));
15551608
table.addCell(new Cell().add("Hello"));
@@ -1613,7 +1666,7 @@ public void tableWithDocumentRelayoutTest() throws IOException, InterruptedExcep
16131666
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
16141667
Document doc = new Document(pdfDoc, PageSize.A4, false);
16151668

1616-
Table table = new Table(UnitValue.createPercentArray(new float[] {10}));
1669+
Table table = new Table(UnitValue.createPercentArray(new float[]{10}));
16171670
for (int i = 0; i < 40; i++) {
16181671
table.addCell(new Cell().add("" + (i + 1)));
16191672
}
@@ -1633,7 +1686,7 @@ public void tableWithKeepTogetherOnCells() throws IOException, InterruptedExcept
16331686

16341687
Document document = new Document(new PdfDocument(new PdfWriter(outFileName)));
16351688

1636-
Table table = new Table(UnitValue.createPercentArray(new float[] { 1.3f, 1f, 1f, 1f, 1f, 1f, 1f }));
1689+
Table table = new Table(UnitValue.createPercentArray(new float[]{1.3f, 1f, 1f, 1f, 1f, 1f, 1f}));
16371690
table.setWidthPercent(100f).setFixedLayout();
16381691
for (int i = 1; i <= 7 * 100; i++) {
16391692
Cell cell = new Cell().setKeepTogether(true).setMinHeight(45).add("" + i);
@@ -1899,7 +1952,7 @@ public void tableNothingResultTest() throws IOException, InterruptedException {
18991952
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
19001953
Document doc = new Document(pdfDoc);
19011954

1902-
Table table = new Table(UnitValue.createPercentArray(new float[] {30, 30}));
1955+
Table table = new Table(UnitValue.createPercentArray(new float[]{30, 30}));
19031956
table.setKeepTogether(true);
19041957
for (int i = 0; i < 40; i++) {
19051958
table.addCell(new Cell().add("Hello"));
@@ -1979,7 +2032,7 @@ public void fixedLayoutTest01() throws IOException, InterruptedException {
19792032
Document doc = new Document(pdf);
19802033

19812034
doc.add(new Paragraph("Simple table with proportional width. Ignore cell width, because sum(col[*]) < tableWidth:"));
1982-
Table table = new Table(new float[] {1,2,3}).setFixedLayout().setWidth(400);
2035+
Table table = new Table(new float[]{1, 2, 3}).setFixedLayout().setWidth(400);
19832036
table.addCell("1x");
19842037
table.addCell("2x");
19852038
table.addCell("3x");
@@ -2001,7 +2054,7 @@ public void fixedLayoutTest02() throws IOException, InterruptedException {
20012054
Document doc = new Document(pdf);
20022055

20032056
doc.add(new Paragraph("Simple table with proportional width. Ignore table width, because sum(col[*]) > tableWidth."));
2004-
Table table = new Table(new float[] {20,40,60}).setFixedLayout().setWidth(10);
2057+
Table table = new Table(new float[]{20, 40, 60}).setFixedLayout().setWidth(10);
20052058
table.addCell("1x");
20062059
table.addCell("2x");
20072060
table.addCell("3x");

0 commit comments

Comments
 (0)