This repository was archived by the owner on Nov 17, 2024. It is now read-only.
forked from tabulapdf/tabula-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableWithRulingLines.java
More file actions
93 lines (77 loc) · 2.88 KB
/
TableWithRulingLines.java
File metadata and controls
93 lines (77 loc) · 2.88 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package technology.tabula;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import technology.tabula.extractors.ExtractionAlgorithm;
@SuppressWarnings("serial")
public class TableWithRulingLines extends Table {
List<Ruling> verticalRulings, horizontalRulings;
RectangleSpatialIndex<Cell> si = new RectangleSpatialIndex<>();
public TableWithRulingLines(Rectangle area, List<Cell> cells, List<Ruling> horizontalRulings, List<Ruling> verticalRulings, ExtractionAlgorithm extractionAlgorithm) {
super(extractionAlgorithm);
this.setRect(area);
this.verticalRulings = verticalRulings;
this.horizontalRulings = horizontalRulings;
this.addCells(cells);
}
private void addCells(List<Cell> cells) {
if (cells.isEmpty()) {
return;
}
for (Cell ce: cells) {
si.add(ce);
}
List<List<Cell>> rowsOfCells = rowsOfCells(cells);
for (int i = 0; i < rowsOfCells.size(); i++) {
List<Cell> row = rowsOfCells.get(i);
Iterator<Cell> rowCells = row.iterator();
Cell cell = rowCells.next();
List<List<Cell>> others = rowsOfCells(
si.contains(
new Rectangle(cell.getBottom(), si.getBounds().getLeft(), cell.getLeft() - si.getBounds().getLeft(),
si.getBounds().getBottom() - cell.getBottom())
));
int startColumn = 0;
for (List<Cell> r: others) {
startColumn = Math.max(startColumn, r.size());
}
this.add(cell, i, startColumn++);
while (rowCells.hasNext()) {
this.add(rowCells.next(), i, startColumn++);
}
}
}
private static List<List<Cell>> rowsOfCells(List<Cell> cells) {
Cell c;
float lastTop;
List<List<Cell>> rv = new ArrayList<>();
List<Cell> lastRow;
if (cells.isEmpty()) {
return rv;
}
Collections.sort(cells, new Comparator<Cell>() {
@Override
public int compare(Cell arg0, Cell arg1) {
return java.lang.Double.compare(arg0.getTop(), arg1.getTop());
}
});
Iterator<Cell> iter = cells.iterator();
c = iter.next();
lastTop = c.getTop();
lastRow = new ArrayList<>();
lastRow.add(c);
rv.add(lastRow);
while (iter.hasNext()) {
c = iter.next();
if (!Utils.feq(c.getTop(), lastTop)) {
lastRow = new ArrayList<>();
rv.add(lastRow);
}
lastRow.add(c);
lastTop = c.getTop();
}
return rv;
}
}