'How can I disable the splitting of rows (not tables!) in Itext7 for C# when one row is to big to fit on the first page?
How can I disable the splitting of rows in an Itext7-Table if the row is the last one on a page and does not fit to the first page?
The only thing I found is an example for Itext5 where the method setSplitLate() is set to false:
http://what-when-how.com/itext-5/dealing-with-large-tables-itext-5/ (Listing 4.19 HeaderFooter2.java)
Does anyone have a solution for this?
Solution 1:[1]
Here's my solution based on Uladzimir's one.
Basically you have to save a status, on a cell basis, that allow to nullify the SplitRenderer on the cells only the first time the layout method is called on that specific cell.
@Test
public void tableWithCellNotSplitted() {
File fOut = getFileOutput("table-cellsNotSplitter.pdf");
try {
final float MARGIN = 80;
Document document = getDocumentForOutput(fOut, true);
document.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
final String text = "Enjoyed minutes related as at on on. Is fanny dried as often me. Goodness as reserved raptures to mistaken steepest oh screened he.";
final String bigText = text + "\n" + text + "\n" + text + "\n" + text + "\n" + text + "\n" + text;
Table t = new Table(2);
t.setWidth(new UnitValue(UnitValue.PERCENT, 60));
for (int nRow = 0; nRow < 10; nRow++) {
for (int iCol = 0; iCol < 2; iCol++) {
String content = nRow == 1 ? bigText : text;
t.addCell(createNonSplittingCell2(content));
}
}
document.add(t);
document.close();
openOutputWithReader(fOut);
} catch (Exception e) {
Assert.fail(e.toString());
}
}
private Cell createPlainCell(String text) {
Cell c = new Cell().add(new Paragraph(text));
return c;
}
private Cell createNonSplittingCell1(String text) {
Cell c = new Cell().add(new Paragraph(text));
c.setNextRenderer(new NonSplittingRenderer1(c));
return c;
}
private Cell createNonSplittingCell2(String text) {
Cell c = new NotSplittingCell().add(new Paragraph(text));
c.setNextRenderer(new NonSplittingRenderer2(c));
return c;
}
public class NonSplittingRenderer1 extends CellRenderer {
public NonSplittingRenderer1(Cell modelElement) {
super(modelElement);
}
@Override
public LayoutResult layout(LayoutContext layoutContext) {
LayoutResult result = super.layout(layoutContext);
if (LayoutResult.FULL != result.getStatus()) {
result.setStatus(LayoutResult.NOTHING);
result.setSplitRenderer(null);
result.setOverflowRenderer(this);
}
return result;
}
@Override
public IRenderer getNextRenderer() {
return new NonSplittingRenderer1((Cell) getModelElement());
}
}
public class NonSplittingRenderer2 extends CellRenderer {
public NonSplittingRenderer2(Cell modelElement) {
super(modelElement);
}
@Override
public LayoutResult layout(LayoutContext layoutContext) {
LayoutResult result = super.layout(layoutContext);
Cell c = (Cell) modelElement;
if (c instanceof NotSplittingCell) {
if (LayoutResult.FULL != result.getStatus()) {
NotSplittingCell nsc = (NotSplittingCell) c;
if ( !nsc.isSplit()) {
nsc.setSplit(true);
result.setStatus(LayoutResult.NOTHING);
result.setSplitRenderer(null);
result.setOverflowRenderer(this);
}
}
}
return result;
}
@Override
public IRenderer getNextRenderer() {
return new NonSplittingRenderer2((Cell) getModelElement());
}
}
public class NotSplittingCell extends Cell {
private boolean _split;
public boolean isSplit() {
return _split;
}
public void setSplit(boolean split) {
_split = split;
}
}
this code has three ways to create cells:
- Plain cells with no renderer (createPlainCell method)
- Plain cells with renderer like the previous solution (createNonSplittingCell1 method)
- Custom cell with modified version of renderer (createNonSplittingCell2 method)
Solution 1 obviously split cells.
Solution 2 stops writing cells after the firts row: the layout method nullify the splitrender every time he try to render the second row's cells: simply the table vanishes.
Solution 3 uses a subclass of Cell, NotSplittingCell, that simply store the split status of the cell: the splitrenderer is nullified only if the cell is never split. That way correctly keep cells unsplit, but only if it's possible
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
