-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathObjectExtractor.java
More file actions
87 lines (71 loc) · 3.17 KB
/
Copy pathObjectExtractor.java
File metadata and controls
87 lines (71 loc) · 3.17 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
package technology.tabula;
import java.io.IOException;
import java.util.List;
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 List<String> tableNames;
public ObjectExtractor(PDDocument pdfDocument, List<String> tableNames) {
this.pdfDocument = pdfDocument;
this.tableNames = tableNames;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
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);
streamEngine.processPage(page);
TextStripper textStripper = new TextStripper(pdfDocument, pageNumber);
textStripper.process();
String tableName = "";
//TODO 判断表名是否存在
if (tableNames != null){
//采用文本包含方式判断表名,后续需优化
tableName = Utils.findTableName(tableNames, pdfTextStripper.getContent());
if ("".equals(tableName)) {
return null;
}
}
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())
.withContent(pdfTextStripper.getContent())
.withTableName(tableName)
.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();
}
}