-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathObjectExtractor.java
More file actions
79 lines (62 loc) · 2.81 KB
/
ObjectExtractor.java
File metadata and controls
79 lines (62 loc) · 2.81 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
package technology.tabula;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class ObjectExtractor implements java.io.Closeable {
private final PDDocument pdfDocument;
private final Integer lineColorFilter;
public ObjectExtractor(PDDocument pdfDocument) {
this(pdfDocument, null);
}
public ObjectExtractor(PDDocument pdfDocument, Integer lineColorFilter) {
this.pdfDocument = pdfDocument;
this.lineColorFilter = lineColorFilter;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
protected Page extractPage(Integer pageNumber) throws IOException {
if (pageNumber > pdfDocument.getNumberOfPages() || pageNumber < 1) {
throw new java.lang.IndexOutOfBoundsException("Page number does not exist.");
}
PDPage page = pdfDocument.getPage(pageNumber - 1);
ObjectExtractorStreamEngine streamEngine = new ObjectExtractorStreamEngine(page, lineColorFilter);
streamEngine.processPage(page);
TextStripper textStripper = new TextStripper(pdfDocument, pageNumber);
textStripper.process();
Utils.sort(textStripper.getTextElements(), Rectangle.ILL_DEFINED_ORDER);
float width, height;
int rotation = page.getRotation();
if (Math.abs(rotation) == 90 || Math.abs(rotation) == 270) {
width = page.getCropBox().getHeight();
height = page.getCropBox().getWidth();
} else {
width = page.getCropBox().getWidth();
height = page.getCropBox().getHeight();
}
return Page.Builder.newInstance()
.withPageDims(PageDims.of(0, 0, width, height))
.withRotation(rotation)
.withNumber(pageNumber)
.withPdPage(page)
.withPdDocument(pdfDocument)
.withRulings(streamEngine.rulings)
.withTextElements(textStripper.getTextElements())
.withMinCharWidth(textStripper.getMinCharWidth())
.withMinCharHeight(textStripper.getMinCharHeight())
.withIndex(textStripper.getSpatialIndex())
.build();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
public PageIterator extract(Iterable<Integer> pages) {
return new PageIterator(this, pages);
}
public PageIterator extract() {
return extract(Utils.range(1, pdfDocument.getNumberOfPages() + 1));
}
public Page extract(int pageNumber) {
return extract(Utils.range(pageNumber, pageNumber + 1)).next();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
public void close() throws IOException {
pdfDocument.close();
}
}