forked from tabulapdf/tabula-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectExtractor.java
More file actions
427 lines (351 loc) · 14.6 KB
/
ObjectExtractor.java
File metadata and controls
427 lines (351 loc) · 14.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package technology.tabula;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.pdfviewer.PageDrawer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.encryption.BadSecurityHandlerException;
import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.pdmodel.graphics.PDGraphicsState;
import org.apache.pdfbox.pdmodel.text.PDTextState;
import org.apache.pdfbox.util.TextPosition;
public class ObjectExtractor extends org.apache.pdfbox.pdfviewer.PageDrawer {
private static final char[] spaceLikeChars = { ' ', '-', '1', 'i' };
private static final String NBSP = "\u00A0";
private float minCharWidth;
private float minCharHeight;
private List<TextElement> characters;
private List<Ruling> rulings;
private RectangleSpatialIndex<TextElement> spatialIndex;
private AffineTransform pageTransform;
public List<Shape> clippingPaths;
private boolean debugClippingPaths;
private boolean extractRulingLines;
private final PDDocument pdf_document;
protected List pdf_document_pages;
public ObjectExtractor(PDDocument pdf_document) throws IOException {
this(pdf_document, null, true, false);
}
public ObjectExtractor(PDDocument pdf_document, boolean debugClippingPaths) throws IOException {
this(pdf_document, null, true, debugClippingPaths);
}
public ObjectExtractor(PDDocument pdf_document, String password) throws IOException {
this(pdf_document, password, true, false);
}
public ObjectExtractor(PDDocument pdf_document, String password, boolean extractRulingLines, boolean debugClippingPaths)
throws IOException {
super();
this.clippingPaths = new ArrayList<Shape>();
this.debugClippingPaths = debugClippingPaths;
this.extractRulingLines = extractRulingLines;
this.initialize();
// patch PageDrawer: dummy Graphics2D context so some drawing operators don't complain
try {
Field field = PageDrawer.class.getDeclaredField("graphics");
field.setAccessible(true);
field.set(this, new DummyGraphics2D());
}
catch (Exception e1) {
}
if (pdf_document.isEncrypted()) {
try {
pdf_document
.openProtection(new StandardDecryptionMaterial(password));
} catch (BadSecurityHandlerException e) {
// TODO Auto-generated catch block
throw new IOException("BadSecurityHandler");
} catch (CryptographyException e) {
throw new IOException("Document is encrypted");
}
}
this.pdf_document = pdf_document;
this.pdf_document_pages = this.pdf_document.getDocumentCatalog()
.getAllPages();
}
protected Page extractPage(Integer page_number) throws IOException {
if (page_number > this.pdf_document_pages.size() || page_number < 1) {
throw new java.lang.IndexOutOfBoundsException(
"Page number does not exist");
}
this.initialize();
PDPage pdPage = (PDPage) this.pdf_document_pages.get(page_number - 1);
pdPage = this.drawPage(pdPage);
if(pdPage != null) {
Utils.sort(this.characters);
float w, h;
int pageRotation = pdPage.findRotation();
if (Math.abs(pageRotation) == 90 || Math.abs(pageRotation) == 270) {
w = pdPage.findCropBox().getHeight();
h = pdPage.findCropBox().getWidth();
}
else {
w = pdPage.findCropBox().getWidth();
h = pdPage.findCropBox().getHeight();
}
return new Page(0, 0, w, h, pageRotation, page_number, pdPage, this.characters,
this.rulings, this.minCharWidth, this.minCharHeight,
this.spatialIndex);
}
return null;//TODO: content is empty, return null? or empty Page? or exception?
}
public PageIterator extract(Iterable<Integer> pages) {
return new PageIterator(this, pages);
}
public PageIterator extract() {
return extract(Utils.range(1, this.pdf_document_pages.size() + 1));
}
public Page extract(int pageNumber) {
return extract(Utils.range(pageNumber, pageNumber + 1)).next();
}
public void close() throws IOException {
this.pdf_document.close();
}
private PDPage drawPage(PDPage p) throws IOException {
this.page = p;
PDStream contents = p.getContents();
if (contents != null) {
ensurePageSize();
this.processStream(p, p.findResources(), contents.getStream());
return p;
}
return null;
}
private void ensurePageSize() {
if (this.pageSize == null && this.page != null) {
PDRectangle cropBox = this.page.findCropBox();
this.pageSize = cropBox == null ? null : cropBox
.createDimension();
}
}
private void initialize() {
this.characters = new ArrayList<TextElement>();
this.rulings = new ArrayList<Ruling>();
this.pageTransform = null;
this.spatialIndex = new RectangleSpatialIndex<TextElement>();
this.minCharWidth = Float.MAX_VALUE;
this.minCharHeight = Float.MAX_VALUE;
}
@Override
public void drawImage(Image awtImage, AffineTransform at) {
// we just ignore images (for now)
}
public void strokeOrFillPath(boolean isFill) {
GeneralPath path = this.getLinePath();
if (!this.extractRulingLines) {
this.getLinePath().reset();
return;
}
PathIterator pi = path.getPathIterator(this.getPageTransform());
float[] c = new float[6];
int currentSegment;
// skip paths whose first operation is not a MOVETO
// or contains operations other than LINETO, MOVETO or CLOSE
if ((pi.currentSegment(c) != PathIterator.SEG_MOVETO)) {
path.reset();
return;
}
pi.next();
while (!pi.isDone()) {
currentSegment = pi.currentSegment(c);
if (currentSegment != PathIterator.SEG_LINETO
&& currentSegment != PathIterator.SEG_CLOSE
&& currentSegment != PathIterator.SEG_MOVETO) {
path.reset();
return;
}
pi.next();
}
// TODO: how to implement color filter?
// skip the first path operation and save it as the starting position
float[] first = new float[6];
pi = path.getPathIterator(this.getPageTransform());
pi.currentSegment(first);
// last move
Point2D.Float start_pos = new Point2D.Float(Utils.round(first[0], 2), Utils.round(first[1], 2));
Point2D.Float last_move = start_pos;
Point2D.Float end_pos = null;
Line2D.Float line;
PointComparator pc = new PointComparator();
while (!pi.isDone()) {
pi.next();
currentSegment = pi.currentSegment(c);
switch (currentSegment) {
case PathIterator.SEG_LINETO:
end_pos = new Point2D.Float(c[0], c[1]);
line = pc.compare(start_pos, end_pos) == -1 ? new Line2D.Float(
start_pos, end_pos) : new Line2D.Float(end_pos,
start_pos);
if (line.intersects(this.currentClippingPath())) {
Ruling r = new Ruling(line.getP1(), line.getP2())
.intersect(this.currentClippingPath());
if (r.length() > 0.01) {
this.rulings.add(r);
}
}
break;
case PathIterator.SEG_MOVETO:
last_move = new Point2D.Float(c[0], c[1]);
end_pos = last_move;
break;
case PathIterator.SEG_CLOSE:
// according to PathIterator docs:
// "the preceding subpath should be closed by appending a line
// segment
// back to the point corresponding to the most recent
// SEG_MOVETO."
line = pc.compare(end_pos, last_move) == -1 ? new Line2D.Float(
end_pos, last_move) : new Line2D.Float(last_move,
end_pos);
if (line.intersects(this.currentClippingPath())) {
Ruling r = new Ruling(line.getP1(), line.getP2())
.intersect(this.currentClippingPath());
if (r.length() > 0.01) {
this.rulings.add(r);
}
}
break;
}
start_pos = end_pos;
}
path.reset();
}
@Override
public void strokePath() throws IOException {
this.strokeOrFillPath(false);
}
@Override
public void fillPath(int windingRule) throws IOException {
//
// float[] color_comps =
// this.getGraphicsState().getNonStrokingColor().getJavaColor().getRGBColorComponents(null);
// float[] color = this.getGraphicsState().getNonStrokingColor().getJavaColor().getComponents(null);
// TODO use color_comps as filter_by_color
this.strokeOrFillPath(true);
}
private float currentSpaceWidth() {
PDGraphicsState gs = this.getGraphicsState();
PDTextState ts = gs.getTextState();
PDFont font = ts.getFont();
float fontSizeText = ts.getFontSize();
float horizontalScalingText = ts.getHorizontalScalingPercent() / 100.0f;
float spaceWidthText = 1000;
if (font instanceof PDType3Font) {
// TODO WHAT?
}
for (int i = 0; i < spaceLikeChars.length; i++) {
spaceWidthText = font.getFontWidth(spaceLikeChars[i]);
if (spaceWidthText > 0)
break;
}
float ctm00 = gs.getCurrentTransformationMatrix().getValue(0, 0);
return (float) ((spaceWidthText / 1000.0) * fontSizeText
* horizontalScalingText * (ctm00 == 0 ? 1 : ctm00));
}
@Override
protected void processTextPosition(TextPosition textPosition) {
String c = textPosition.getCharacter();
// if c not printable, return
if (!isPrintable(c)) {
return;
}
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.getFontSize(),
c,
// workaround a possible bug in PDFBox:
// https://issues.apache.org/jira/browse/PDFBOX-1755
(Float.isNaN(wos) || wos == 0) ? this.currentSpaceWidth() : wos,
textPosition.getDir());
if (this.currentClippingPath().intersects(te)) {
this.minCharWidth = (float) Math.min(this.minCharWidth, te.getWidth());
this.minCharHeight = (float) Math.min(this.minCharHeight, te.getHeight());
this.spatialIndex.add(te);
this.characters.add(te);
}
if (this.isDebugClippingPaths() && !this.clippingPaths.contains(this.currentClippingPath())) {
this.clippingPaths.add(this.currentClippingPath());
}
}
public AffineTransform getPageTransform() {
if (this.pageTransform != null) {
return this.pageTransform;
}
PDRectangle cb = page.findCropBox();
int rotation = Math.abs(page.findRotation());
this.pageTransform = new AffineTransform();
if (rotation == 90 || rotation == 270) {
this.pageTransform = AffineTransform.getRotateInstance(rotation * (Math.PI / 180.0), 0, 0);
this.pageTransform.concatenate(AffineTransform.getScaleInstance(1, -1));
this.pageTransform.concatenate(AffineTransform.getTranslateInstance(0, cb.getHeight()));
this.pageTransform.concatenate(AffineTransform.getScaleInstance(1, -1));
}
return this.pageTransform;
}
public Rectangle2D currentClippingPath() {
Shape clippingPath = this.getGraphicsState().getCurrentClippingPath();
Shape transformedClippingPath = this.getPageTransform()
.createTransformedShape(clippingPath);
Rectangle2D transformedClippingPathBounds = transformedClippingPath
.getBounds2D();
return transformedClippingPathBounds;
}
public boolean isExtractRulingLines() {
return extractRulingLines;
}
private static boolean isPrintable(String s) {
Character c = s.charAt(0);
Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
return (!Character.isISOControl(c)) && c != KeyEvent.CHAR_UNDEFINED
&& block != null && block != Character.UnicodeBlock.SPECIALS;
}
public boolean isDebugClippingPaths() {
return debugClippingPaths;
}
public int getPageCount() {
return this.pdf_document_pages.size();
}
class PointComparator implements Comparator<Point2D> {
@Override
public int compare(Point2D o1, Point2D o2) {
float o1X = Utils.round(o1.getX(), 2);
float o1Y = Utils.round(o1.getY(), 2);
float o2X = Utils.round(o2.getX(), 2);
float o2Y = Utils.round(o2.getY(), 2);
if (o1Y > o2Y)
return 1;
if (o1Y < o2Y)
return -1;
if (o1X > o2X)
return 1;
if (o1X < o2X)
return -1;
return 0;
}
}
}