|
13 | 13 | import java.util.List; |
14 | 14 |
|
15 | 15 | public class TextStripper extends PDFTextStripper { |
| 16 | + |
16 | 17 | private static final String NBSP = "\u00A0"; |
17 | 18 | private static final float AVG_HEIGHT_MULT_THRESHOLD = 6.0f; |
| 19 | + private static final float MAX_BLANK_FONT_SIZE = 40.0f; |
| 20 | + private static final float MIN_BLANK_FONT_SIZE = 2.0f; |
18 | 21 | private PDDocument document; |
19 | 22 | public ArrayList<TextElement> textElements; |
20 | 23 | public RectangleSpatialIndex<TextElement> spatialIndex; |
@@ -69,15 +72,24 @@ protected void writeString(String string, List<TextPosition> textPositions) thro |
69 | 72 |
|
70 | 73 | this.minCharWidth = (float) Math.min(this.minCharWidth, te.getWidth()); |
71 | 74 | this.minCharHeight = (float) Math.min(this.minCharHeight, te.getHeight()); |
72 | | - |
| 75 | + |
73 | 76 | countHeight++; |
74 | 77 | totalHeight += te.getHeight(); |
75 | 78 | float avgHeight = totalHeight / countHeight; |
76 | 79 |
|
77 | | - if (avgHeight > 0 |
78 | | - && te.getHeight() >= (avgHeight * AVG_HEIGHT_MULT_THRESHOLD) |
79 | | - && (te.getText() == null || te.getText().trim().equals(""))) { |
80 | | - continue; |
| 80 | + //We have an issue where tall blank cells throw off the row height calculation |
| 81 | + //Introspect a blank cell a bit here to see if it should be thrown away |
| 82 | + if ((te.getText() == null || te.getText().trim().equals(""))) { |
| 83 | + //if the cell height is more than AVG_HEIGHT_MULT_THRESHOLDxaverage, throw it away |
| 84 | + if (avgHeight > 0 |
| 85 | + && te.getHeight() >= (avgHeight * AVG_HEIGHT_MULT_THRESHOLD)) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + //if the font size is outside of reasonable ranges, throw it away |
| 90 | + if (textPosition.getFontSizeInPt() > MAX_BLANK_FONT_SIZE || textPosition.getFontSizeInPt() < MIN_BLANK_FONT_SIZE) { |
| 91 | + continue; |
| 92 | + } |
81 | 93 | } |
82 | 94 |
|
83 | 95 | this.spatialIndex.add(te); |
|
0 commit comments