Skip to content

Commit 008c395

Browse files
ZaqueuCavalcantejazzido
authored andcommitted
Refactoring classes related to a pdf page.
1 parent a9932a8 commit 008c395

3 files changed

Lines changed: 40 additions & 38 deletions

File tree

src/main/java/technology/tabula/ObjectExtractor.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,50 @@ public ObjectExtractor(PDDocument pdfDocument) {
1313
this.pdfDocument = pdfDocument;
1414
}
1515

16+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
1617
protected Page extractPage(Integer pageNumber) throws IOException {
17-
18-
if (pageNumber > this.pdfDocument.getNumberOfPages() || pageNumber < 1) {
19-
throw new java.lang.IndexOutOfBoundsException(
20-
"Page number does not exist");
18+
if (pageNumber > pdfDocument.getNumberOfPages() || pageNumber < 1) {
19+
throw new java.lang.IndexOutOfBoundsException("Page number does not exist.");
2120
}
21+
PDPage page = pdfDocument.getPage(pageNumber - 1);
2222

23-
PDPage p = this.pdfDocument.getPage(pageNumber - 1);
24-
25-
ObjectExtractorStreamEngine se = new ObjectExtractorStreamEngine(p);
26-
se.processPage(p);
27-
28-
29-
TextStripper pdfTextStripper = new TextStripper(this.pdfDocument, pageNumber);
23+
ObjectExtractorStreamEngine streamEngine = new ObjectExtractorStreamEngine(page);
24+
streamEngine.processPage(page);
3025

31-
pdfTextStripper.process();
26+
TextStripper textStripper = new TextStripper(pdfDocument, pageNumber);
27+
textStripper.process();
3228

33-
Utils.sort(pdfTextStripper.textElements, Rectangle.ILL_DEFINED_ORDER);
29+
Utils.sort(textStripper.textElements, Rectangle.ILL_DEFINED_ORDER);
3430

35-
float w, h;
36-
int pageRotation = p.getRotation();
37-
if (Math.abs(pageRotation) == 90 || Math.abs(pageRotation) == 270) {
38-
w = p.getCropBox().getHeight();
39-
h = p.getCropBox().getWidth();
31+
float width, height;
32+
int rotation = page.getRotation();
33+
if (Math.abs(rotation) == 90 || Math.abs(rotation) == 270) {
34+
width = page.getCropBox().getHeight();
35+
height = page.getCropBox().getWidth();
4036
} else {
41-
w = p.getCropBox().getWidth();
42-
h = p.getCropBox().getHeight();
37+
width = page.getCropBox().getWidth();
38+
height = page.getCropBox().getHeight();
4339
}
4440

45-
return new Page(0, 0, w, h, pageRotation, pageNumber, p, this.pdfDocument, pdfTextStripper.textElements,
46-
se.rulings, pdfTextStripper.minCharWidth, pdfTextStripper.minCharHeight, pdfTextStripper.spatialIndex);
41+
return new Page(0, 0, width, height, rotation, pageNumber, page, pdfDocument, streamEngine, textStripper);
4742
}
4843

44+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
4945
public PageIterator extract(Iterable<Integer> pages) {
5046
return new PageIterator(this, pages);
5147
}
5248

5349
public PageIterator extract() {
54-
return extract(Utils.range(1, this.pdfDocument.getNumberOfPages() + 1));
50+
return extract(Utils.range(1, pdfDocument.getNumberOfPages() + 1));
5551
}
5652

5753
public Page extract(int pageNumber) {
5854
return extract(Utils.range(pageNumber, pageNumber + 1)).next();
5955
}
6056

57+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
6158
public void close() throws IOException {
62-
this.pdfDocument.close();
59+
pdfDocument.close();
6360
}
64-
65-
66-
61+
6762
}

src/main/java/technology/tabula/Page.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public Page(float top, float left, float width, float height, int rotation, int
3939
this.rulings = rulings;
4040
}
4141

42+
public Page(float top, float left, float width, float height, int rotation, int number, PDPage pdPage, PDDocument doc,
43+
ObjectExtractorStreamEngine streamEngine, TextStripper textStripper) {
44+
this(top, left, width, height, rotation, number, pdPage, doc, textStripper.textElements, streamEngine.rulings);
45+
this.minCharWidth = textStripper.minCharWidth;
46+
this.minCharHeight = textStripper.minCharHeight;
47+
this.spatial_index = textStripper.spatialIndex;
48+
}
4249

4350
public Page(float top, float left, float width, float height, int rotation, int page_number, PDPage pdPage, PDDocument doc,
4451
List<TextElement> characters, List<Ruling> rulings,

src/main/java/technology/tabula/PageIterator.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@
55

66
public class PageIterator implements Iterator<Page> {
77

8-
private ObjectExtractor oe;
8+
private ObjectExtractor objectExtractor;
99
private Iterator<Integer> pageIndexIterator;
10-
11-
public PageIterator(ObjectExtractor oe, Iterable<Integer> pages) {
10+
11+
public PageIterator(ObjectExtractor objectExtractor, Iterable<Integer> pages) {
1212
super();
13-
this.oe = oe;
13+
this.objectExtractor = objectExtractor;
1414
this.pageIndexIterator = pages.iterator();
1515
}
1616

17+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
1718
@Override
1819
public boolean hasNext() {
19-
return this.pageIndexIterator.hasNext();
20+
return pageIndexIterator.hasNext();
2021
}
2122

2223
@Override
2324
public Page next() {
24-
Page page = null;
25+
Page nextPage = null;
2526
if (!this.hasNext()) {
2627
throw new IllegalStateException();
2728
}
2829
try {
29-
page = oe.extractPage(this.pageIndexIterator.next());
30+
nextPage = objectExtractor.extractPage(pageIndexIterator.next());
3031
} catch (IOException e) {
31-
// TODO Auto-generated catch block
3232
e.printStackTrace();
3333
}
34-
return page;
34+
return nextPage;
3535
}
3636

37+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
3738
@Override
3839
public void remove() {
3940
throw new UnsupportedOperationException();
40-
4141
}
4242

43-
}
43+
}

0 commit comments

Comments
 (0)