How to use a dotted line as a cell border?
I am trying to create a table with cells that have a dotted line for a border. How can I do this?
Posted on StackOverflow on Nov 21, 2013 by user1913695
I've made an example that solves your problem: DottedLineCell. The resulting PDF is a document with two tables. For the first table, we use a table renderer:
Java
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| private class DottedLineTableRenderer extends TableRenderer {
public DottedLineTableRenderer(Table modelElement, Table.RowRange rowRange) {
super(modelElement, rowRange);
}
@Override
public void drawChildren(DrawContext drawContext) {
super.drawChildren(drawContext);
PdfCanvas canvas = drawContext.getCanvas();
canvas.setLineDash(3f, 3f);
// first horizontal line
CellRenderer[] cellRenderers = rows.get(0);
canvas.moveTo(cellRenderers[0].getOccupiedArea().getBBox().getLeft(),
cellRenderers[0].getOccupiedArea().getBBox().getTop());
canvas.lineTo(cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getRight(),
cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getTop());
for (CellRenderer[] renderers : rows) {
// horizontal lines
canvas.moveTo(renderers[0].getOccupiedArea().getBBox().getX(),
renderers[0].getOccupiedArea().getBBox().getY());
canvas.lineTo(renderers[renderers.length - 1].getOccupiedArea().getBBox().getRight(),
renderers[renderers.length - 1].getOccupiedArea().getBBox().getBottom());
// first vertical line
Rectangle cellRect = renderers[0].getOccupiedArea().getBBox();
canvas.moveTo(cellRect.getLeft(), cellRect.getBottom());
canvas.lineTo(cellRect.getLeft(), cellRect.getTop());
// vertical lines
for (CellRenderer renderer : renderers) {
cellRect = renderer.getOccupiedArea().getBBox();
canvas.moveTo(cellRect.getRight(), cellRect.getBottom());
canvas.lineTo(cellRect.getRight(), cellRect.getTop());
}
}
canvas.stroke();
}
} |
This is the most elegant way to draw the cell borders, as it uses only one stroke()
operator for all the lines. Unfortunately, this solution isn't an option if you have tables with rowspans. The second table uses a cell renderer:
Java
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
| private class DottedLineCellRenderer extends CellRenderer {
public DottedLineCellRenderer(Cell modelElement) {
super(modelElement);
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
drawContext.getCanvas().setLineDash(3f, 3f);
drawContext.getCanvas().rectangle(this.getOccupiedArea().getBBox());
drawContext.getCanvas().stroke();
}
} |
With a cell event, a border is drawn around every cell. This means you'll have multiple stroke()
operators and overlapping lines. However: this solution always works, also when the table has cells with a rowspan greater than one.
Click this link if you want to see how to answer this question in iText 5.