This repository was archived by the owner on Jan 7, 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 pathTextStripper.java
More file actions
147 lines (126 loc) · 4.98 KB
/
TextStripper.java
File metadata and controls
147 lines (126 loc) · 4.98 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package technology.tabula;
import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TextStripper extends PDFTextStripper {
private static final String NBSP = "\u00A0";
private static final float AVG_HEIGHT_MULT_THRESHOLD = 6.0f;
private PDDocument document;
public ArrayList<TextElement> textElements;
public RectangleSpatialIndex<TextElement> spatialIndex;
public float minCharWidth = Float.MAX_VALUE;
public float minCharHeight = Float.MAX_VALUE;
public float totalHeight = 0.0f;
public int countHeight = 0;
public TextStripper(PDDocument document, int pageNumber) throws IOException {
super();
this.document = document;
this.setStartPage(pageNumber);
this.setEndPage(pageNumber);
this.textElements = new ArrayList<>();
this.spatialIndex = new RectangleSpatialIndex<>();
}
public void process() throws IOException {
this.getText(this.document);
}
@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException
{
for (TextPosition textPosition: textPositions)
{
if (textPosition == null) {
continue;
}
String c = textPosition.getUnicode();
// if c not printable, return
if (!isPrintable(c)) {
continue;
}
Float h = textPosition.getHeightDir();
if (c.equals(NBSP)) { // replace non-breaking space for space
c = " ";
}
float wos = textPosition.getWidthOfSpace();
TextElement te = new TextElement(Utils.round(textPosition.getYDirAdj() - h, 2),
Utils.round(textPosition.getXDirAdj(), 2), Utils.round(textPosition.getWidthDirAdj(), 2),
Utils.round(textPosition.getHeightDir(), 2), textPosition.getFont(), textPosition.getFontSizeInPt(), c,
// workaround a possible bug in PDFBox:
// https://issues.apache.org/jira/browse/PDFBOX-1755
wos, textPosition.getDir());
this.minCharWidth = (float) Math.min(this.minCharWidth, te.getWidth());
this.minCharHeight = (float) Math.min(this.minCharHeight, te.getHeight());
countHeight++;
totalHeight += te.getHeight();
float avgHeight = totalHeight / countHeight;
if (avgHeight > 0
&& te.getHeight() >= (avgHeight * AVG_HEIGHT_MULT_THRESHOLD)
&& (te.getText() == null || te.getText().trim().equals(""))) {
continue;
}
this.spatialIndex.add(te);
this.textElements.add(te);
}
}
@Override
protected float computeFontHeight(PDFont font) throws IOException
{
BoundingBox bbox = font.getBoundingBox();
if (bbox.getLowerLeftY() < Short.MIN_VALUE)
{
// PDFBOX-2158 and PDFBOX-3130
// files by Salmat eSolutions / ClibPDF Library
bbox.setLowerLeftY(- (bbox.getLowerLeftY() + 65536));
}
// 1/2 the bbox is used as the height todo: why?
float glyphHeight = bbox.getHeight() / 2;
// sometimes the bbox has very high values, but CapHeight is OK
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
if (fontDescriptor != null)
{
float capHeight = fontDescriptor.getCapHeight();
if (Float.compare(capHeight, 0) != 0 &&
(capHeight < glyphHeight || Float.compare(glyphHeight, 0) == 0))
{
glyphHeight = capHeight;
}
// PDFBOX-3464, PDFBOX-448:
// sometimes even CapHeight has very high value, but Ascent and Descent are ok
float ascent = fontDescriptor.getAscent();
float descent = fontDescriptor.getDescent();
if (ascent > 0 && descent < 0 &&
((ascent - descent) / 2 < glyphHeight || Float.compare(glyphHeight, 0) == 0))
{
glyphHeight = (ascent - descent) / 2;
}
}
// transformPoint from glyph space -> text space
float height;
if (font instanceof PDType3Font)
{
height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
}
else
{
height = glyphHeight / 1000;
}
return height;
}
private boolean isPrintable(String s) {
Character c;
Character.UnicodeBlock block;
boolean printable = false;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
block = Character.UnicodeBlock.of(c);
printable |= !Character.isISOControl(c) && block != null && block != Character.UnicodeBlock.SPECIALS;
}
return printable;
}
}