pdf-textstream 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/PDFLayoutTextStripper.java +468 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/build.sh +2 -0
- data/classes/Character.class +0 -0
- data/classes/CharacterFactory.class +0 -0
- data/classes/PDFLayoutTextStripper.class +0 -0
- data/classes/TextLine.class +0 -0
- data/jars/commons-logging-1.2.jar +0 -0
- data/jars/fontbox-2.0.6.jar +0 -0
- data/jars/pdfbox-2.0.6.jar +0 -0
- data/lib/pdf/textstream.rb +30 -0
- data/lib/pdf/textstream/version.rb +5 -0
- data/pdf-textstream.gemspec +26 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 21c6dc3e502ceac31180f160ad74c03e790fd1e5c228438f4155d7ff8254876b
|
4
|
+
data.tar.gz: e328c69e215c0b657eac56098347f3374765fe25d1feb455b2793ed1e83bb435
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84ce6cb92773f7c5513a351e499108295e4cf164da1a1762de6af51317b8ba887d8eb2f211364772d8169156eccf300ee4d398fe20e377b54878365223e8c37d
|
7
|
+
data.tar.gz: 2fccca0559bf5e25a215dc03e7dc3d80361fa1513d7c95cc629f115349dff5d25b30c84819728b1d152df5191ef26672c4dd9c990b271ac726966bd4bc7b16e3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Michał Kulesza
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
@@ -0,0 +1,468 @@
|
|
1
|
+
/*
|
2
|
+
* Author: Jonathan Link
|
3
|
+
* Email: jonathanlink[d o t]email[a t]gmail[d o t]com
|
4
|
+
* Date of creation: 13.11.2014
|
5
|
+
* Version: 2.2.2
|
6
|
+
* Description:
|
7
|
+
*
|
8
|
+
* Version 2.1 uses PDFBox 2.x. Version 1.0 used PDFBox 1.8.x
|
9
|
+
* Acknowledgement to James Sullivan for version 2.0
|
10
|
+
*
|
11
|
+
* What does it DO:
|
12
|
+
* This object converts the content of a PDF file into a String.
|
13
|
+
* The layout of the texts is transcribed as near as the one in the PDF given at the input.
|
14
|
+
*
|
15
|
+
* What does it NOT DO:
|
16
|
+
* Vertical texts in the PDF file are not handled for the moment.
|
17
|
+
*
|
18
|
+
* I would appreciate any feedback you could offer. (see my email address above)
|
19
|
+
*
|
20
|
+
* LICENSE:
|
21
|
+
*
|
22
|
+
* The MIT License (MIT)
|
23
|
+
*
|
24
|
+
* Copyright (c) 2014-2017 Jonathan Link
|
25
|
+
*
|
26
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
27
|
+
* of this software and associated documentation files (the "Software"), to deal
|
28
|
+
* in the Software without restriction, including without limitation the rights
|
29
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
30
|
+
* copies of the Software, and to permit persons to whom the Software is
|
31
|
+
* furnished to do so, subject to the following conditions:
|
32
|
+
*
|
33
|
+
* The above copyright notice and this permission notice shall be included in
|
34
|
+
* all copies or substantial portions of the Software.
|
35
|
+
*
|
36
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
37
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
38
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
39
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
40
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
41
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
42
|
+
* THE SOFTWARE.
|
43
|
+
*
|
44
|
+
*/
|
45
|
+
|
46
|
+
import java.io.IOException;
|
47
|
+
import java.util.ArrayList;
|
48
|
+
import java.util.Collections;
|
49
|
+
import java.util.Iterator;
|
50
|
+
import java.util.List;
|
51
|
+
|
52
|
+
import org.apache.pdfbox.pdmodel.PDPage;
|
53
|
+
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
54
|
+
import org.apache.pdfbox.text.PDFTextStripper;
|
55
|
+
import org.apache.pdfbox.text.TextPosition;
|
56
|
+
import org.apache.pdfbox.text.TextPositionComparator;
|
57
|
+
|
58
|
+
public class PDFLayoutTextStripper extends PDFTextStripper {
|
59
|
+
|
60
|
+
public static final boolean DEBUG = false;
|
61
|
+
public static final int OUTPUT_SPACE_CHARACTER_WIDTH_IN_PT = 4;
|
62
|
+
|
63
|
+
private double currentPageWidth;
|
64
|
+
private TextPosition previousTextPosition;
|
65
|
+
private List<TextLine> textLineList;
|
66
|
+
|
67
|
+
public PDFLayoutTextStripper() throws IOException {
|
68
|
+
super();
|
69
|
+
this.previousTextPosition = null;
|
70
|
+
this.textLineList = new ArrayList<TextLine>();
|
71
|
+
}
|
72
|
+
|
73
|
+
@Override
|
74
|
+
public void processPage(PDPage page) throws IOException {
|
75
|
+
PDRectangle pageRectangle = page.getMediaBox();
|
76
|
+
if (pageRectangle!= null) {
|
77
|
+
this.setCurrentPageWidth(pageRectangle.getWidth());
|
78
|
+
super.processPage(page);
|
79
|
+
this.previousTextPosition = null;
|
80
|
+
this.textLineList = new ArrayList<TextLine>();
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
@Override
|
85
|
+
protected void writePage() throws IOException {
|
86
|
+
List<List<TextPosition>> charactersByArticle = super.getCharactersByArticle();
|
87
|
+
for( int i = 0; i < charactersByArticle.size(); i++) {
|
88
|
+
List<TextPosition> textList = charactersByArticle.get(i);
|
89
|
+
try {
|
90
|
+
this.sortTextPositionList(textList);
|
91
|
+
} catch ( java.lang.IllegalArgumentException e) {
|
92
|
+
System.err.println(e);
|
93
|
+
}
|
94
|
+
this.iterateThroughTextList(textList.iterator()) ;
|
95
|
+
}
|
96
|
+
this.writeToOutputStream(this.getTextLineList());
|
97
|
+
}
|
98
|
+
|
99
|
+
private void writeToOutputStream(final List<TextLine> textLineList) throws IOException {
|
100
|
+
for (TextLine textLine : textLineList) {
|
101
|
+
char[] line = textLine.getLine().toCharArray();
|
102
|
+
super.getOutput().write(line);
|
103
|
+
super.getOutput().write('\n');
|
104
|
+
super.getOutput().flush();
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
/*
|
109
|
+
* In order to get rid of the warning:
|
110
|
+
* TextPositionComparator class should implement Comparator<TextPosition> instead of Comparator
|
111
|
+
*/
|
112
|
+
@SuppressWarnings("unchecked")
|
113
|
+
private void sortTextPositionList(final List<TextPosition> textList) {
|
114
|
+
TextPositionComparator comparator = new TextPositionComparator();
|
115
|
+
Collections.sort(textList, comparator);
|
116
|
+
}
|
117
|
+
|
118
|
+
private void writeLine(final List<TextPosition> textPositionList) {
|
119
|
+
if ( textPositionList.size() > 0 ) {
|
120
|
+
TextLine textLine = this.addNewLine();
|
121
|
+
boolean firstCharacterOfLineFound = false;
|
122
|
+
for (TextPosition textPosition : textPositionList ) {
|
123
|
+
CharacterFactory characterFactory = new CharacterFactory(firstCharacterOfLineFound);
|
124
|
+
Character character = characterFactory.createCharacterFromTextPosition(textPosition, this.getPreviousTextPosition());
|
125
|
+
textLine.writeCharacterAtIndex(character);
|
126
|
+
this.setPreviousTextPosition(textPosition);
|
127
|
+
firstCharacterOfLineFound = true;
|
128
|
+
}
|
129
|
+
} else {
|
130
|
+
this.addNewLine(); // white line
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
private void iterateThroughTextList(Iterator<TextPosition> textIterator) {
|
135
|
+
List<TextPosition> textPositionList = new ArrayList<TextPosition>();
|
136
|
+
|
137
|
+
while ( textIterator.hasNext() ) {
|
138
|
+
TextPosition textPosition = (TextPosition)textIterator.next();
|
139
|
+
int numberOfNewLines = this.getNumberOfNewLinesFromPreviousTextPosition(textPosition);
|
140
|
+
if ( numberOfNewLines == 0 ) {
|
141
|
+
textPositionList.add(textPosition);
|
142
|
+
} else {
|
143
|
+
this.writeTextPositionList(textPositionList);
|
144
|
+
this.createNewEmptyNewLines(numberOfNewLines);
|
145
|
+
textPositionList.add(textPosition);
|
146
|
+
}
|
147
|
+
this.setPreviousTextPosition(textPosition);
|
148
|
+
}
|
149
|
+
if (!textPositionList.isEmpty()) {
|
150
|
+
this.writeTextPositionList(textPositionList);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
private void writeTextPositionList(final List<TextPosition> textPositionList) {
|
155
|
+
this.writeLine(textPositionList);
|
156
|
+
textPositionList.clear();
|
157
|
+
}
|
158
|
+
|
159
|
+
private void createNewEmptyNewLines(int numberOfNewLines) {
|
160
|
+
for (int i = 0; i < numberOfNewLines - 1; ++i) {
|
161
|
+
this.addNewLine();
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
private int getNumberOfNewLinesFromPreviousTextPosition(final TextPosition textPosition) {
|
166
|
+
TextPosition previousTextPosition = this.getPreviousTextPosition();
|
167
|
+
if ( previousTextPosition == null ) {
|
168
|
+
return 1;
|
169
|
+
}
|
170
|
+
|
171
|
+
float textYPosition = Math.round( textPosition.getY() );
|
172
|
+
float previousTextYPosition = Math.round( previousTextPosition.getY() );
|
173
|
+
|
174
|
+
if ( textYPosition > previousTextYPosition && (textYPosition - previousTextYPosition > 5.5) ) {
|
175
|
+
double height = textPosition.getHeight();
|
176
|
+
int numberOfLines = (int) (Math.floor( textYPosition - previousTextYPosition) / height );
|
177
|
+
numberOfLines = Math.max(1, numberOfLines - 1); // exclude current new line
|
178
|
+
if (DEBUG) System.out.println(height + " " + numberOfLines);
|
179
|
+
return numberOfLines ;
|
180
|
+
} else {
|
181
|
+
return 0;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
private TextLine addNewLine() {
|
186
|
+
TextLine textLine = new TextLine(this.getCurrentPageWidth());
|
187
|
+
textLineList.add(textLine);
|
188
|
+
return textLine;
|
189
|
+
}
|
190
|
+
|
191
|
+
private TextPosition getPreviousTextPosition() {
|
192
|
+
return this.previousTextPosition;
|
193
|
+
}
|
194
|
+
|
195
|
+
private void setPreviousTextPosition(final TextPosition setPreviousTextPosition) {
|
196
|
+
this.previousTextPosition = setPreviousTextPosition;
|
197
|
+
}
|
198
|
+
|
199
|
+
private int getCurrentPageWidth() {
|
200
|
+
return (int) Math.round(this.currentPageWidth);
|
201
|
+
}
|
202
|
+
|
203
|
+
private void setCurrentPageWidth(double currentPageWidth) {
|
204
|
+
this.currentPageWidth = currentPageWidth;
|
205
|
+
}
|
206
|
+
|
207
|
+
private List<TextLine> getTextLineList() {
|
208
|
+
return this.textLineList;
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
class TextLine {
|
214
|
+
|
215
|
+
private static final char SPACE_CHARACTER = ' ';
|
216
|
+
private int lineLength;
|
217
|
+
private String line;
|
218
|
+
private int lastIndex;
|
219
|
+
|
220
|
+
public TextLine(int lineLength) {
|
221
|
+
this.line = "";
|
222
|
+
this.lineLength = lineLength / PDFLayoutTextStripper.OUTPUT_SPACE_CHARACTER_WIDTH_IN_PT;
|
223
|
+
this.completeLineWithSpaces();
|
224
|
+
}
|
225
|
+
|
226
|
+
public void writeCharacterAtIndex(final Character character) {
|
227
|
+
character.setIndex(this.computeIndexForCharacter(character));
|
228
|
+
int index = character.getIndex();
|
229
|
+
char characterValue = character.getCharacterValue();
|
230
|
+
if ( this.indexIsInBounds(index) && this.line.charAt(index) == SPACE_CHARACTER) {
|
231
|
+
this.line = this.line.substring(0, index) + characterValue + this.line.substring(index + 1, this.getLineLength());
|
232
|
+
}
|
233
|
+
}
|
234
|
+
|
235
|
+
public int getLineLength() {
|
236
|
+
return this.lineLength;
|
237
|
+
}
|
238
|
+
|
239
|
+
public String getLine() {
|
240
|
+
return line;
|
241
|
+
}
|
242
|
+
|
243
|
+
private int computeIndexForCharacter(final Character character) {
|
244
|
+
int index = character.getIndex();
|
245
|
+
boolean isCharacterPartOfPreviousWord = character.isCharacterPartOfPreviousWord();
|
246
|
+
boolean isCharacterAtTheBeginningOfNewLine = character.isCharacterAtTheBeginningOfNewLine();
|
247
|
+
boolean isCharacterCloseToPreviousWord = character.isCharacterCloseToPreviousWord();
|
248
|
+
|
249
|
+
if ( !this.indexIsInBounds(index) ) {
|
250
|
+
return -1;
|
251
|
+
} else {
|
252
|
+
if ( isCharacterPartOfPreviousWord && !isCharacterAtTheBeginningOfNewLine ) {
|
253
|
+
index = this.findMinimumIndexWithSpaceCharacterFromIndex(index);
|
254
|
+
} else if ( isCharacterCloseToPreviousWord ) {
|
255
|
+
if ( this.line.charAt(index) != SPACE_CHARACTER ) {
|
256
|
+
index = index + 1;
|
257
|
+
} else {
|
258
|
+
index = this.findMinimumIndexWithSpaceCharacterFromIndex(index) + 1;
|
259
|
+
}
|
260
|
+
}
|
261
|
+
index = this.getNextValidIndex(index, isCharacterPartOfPreviousWord);
|
262
|
+
return index;
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
private boolean isSpaceCharacterAtIndex(int index) {
|
267
|
+
return this.line.charAt(index) != SPACE_CHARACTER;
|
268
|
+
}
|
269
|
+
|
270
|
+
private boolean isNewIndexGreaterThanLastIndex(int index) {
|
271
|
+
int lastIndex = this.getLastIndex();
|
272
|
+
return ( index > lastIndex );
|
273
|
+
}
|
274
|
+
|
275
|
+
private int getNextValidIndex(int index, boolean isCharacterPartOfPreviousWord) {
|
276
|
+
int nextValidIndex = index;
|
277
|
+
int lastIndex = this.getLastIndex();
|
278
|
+
if ( ! this.isNewIndexGreaterThanLastIndex(index) ) {
|
279
|
+
nextValidIndex = lastIndex + 1;
|
280
|
+
}
|
281
|
+
if ( !isCharacterPartOfPreviousWord && this.isSpaceCharacterAtIndex(index - 1) ) {
|
282
|
+
nextValidIndex = nextValidIndex + 1;
|
283
|
+
}
|
284
|
+
this.setLastIndex(nextValidIndex);
|
285
|
+
return nextValidIndex;
|
286
|
+
}
|
287
|
+
|
288
|
+
private int findMinimumIndexWithSpaceCharacterFromIndex(int index) {
|
289
|
+
int newIndex = index;
|
290
|
+
while( newIndex >= 0 && this.line.charAt(newIndex) == SPACE_CHARACTER ) {
|
291
|
+
newIndex = newIndex - 1;
|
292
|
+
}
|
293
|
+
return newIndex + 1;
|
294
|
+
}
|
295
|
+
|
296
|
+
private boolean indexIsInBounds(int index) {
|
297
|
+
return (index >= 0 && index < this.lineLength);
|
298
|
+
}
|
299
|
+
|
300
|
+
private void completeLineWithSpaces() {
|
301
|
+
for (int i = 0; i < this.getLineLength(); ++i) {
|
302
|
+
line += SPACE_CHARACTER;
|
303
|
+
}
|
304
|
+
}
|
305
|
+
|
306
|
+
private int getLastIndex() {
|
307
|
+
return this.lastIndex;
|
308
|
+
}
|
309
|
+
|
310
|
+
private void setLastIndex(int lastIndex) {
|
311
|
+
this.lastIndex = lastIndex;
|
312
|
+
}
|
313
|
+
|
314
|
+
}
|
315
|
+
|
316
|
+
|
317
|
+
class Character {
|
318
|
+
|
319
|
+
private char characterValue;
|
320
|
+
private int index;
|
321
|
+
private boolean isCharacterPartOfPreviousWord;
|
322
|
+
private boolean isFirstCharacterOfAWord;
|
323
|
+
private boolean isCharacterAtTheBeginningOfNewLine;
|
324
|
+
private boolean isCharacterCloseToPreviousWord;
|
325
|
+
|
326
|
+
public Character(char characterValue, int index, boolean isCharacterPartOfPreviousWord, boolean isFirstCharacterOfAWord, boolean isCharacterAtTheBeginningOfNewLine, boolean isCharacterPartOfASentence) {
|
327
|
+
this.characterValue = characterValue;
|
328
|
+
this.index = index;
|
329
|
+
this.isCharacterPartOfPreviousWord = isCharacterPartOfPreviousWord;
|
330
|
+
this.isFirstCharacterOfAWord = isFirstCharacterOfAWord;
|
331
|
+
this.isCharacterAtTheBeginningOfNewLine = isCharacterAtTheBeginningOfNewLine;
|
332
|
+
this.isCharacterCloseToPreviousWord = isCharacterPartOfASentence;
|
333
|
+
if (PDFLayoutTextStripper.DEBUG) System.out.println(this.toString());
|
334
|
+
}
|
335
|
+
|
336
|
+
public char getCharacterValue() {
|
337
|
+
return this.characterValue;
|
338
|
+
}
|
339
|
+
|
340
|
+
public int getIndex() {
|
341
|
+
return this.index;
|
342
|
+
}
|
343
|
+
|
344
|
+
public void setIndex(int index) {
|
345
|
+
this.index = index;
|
346
|
+
}
|
347
|
+
|
348
|
+
public boolean isCharacterPartOfPreviousWord() {
|
349
|
+
return this.isCharacterPartOfPreviousWord;
|
350
|
+
}
|
351
|
+
|
352
|
+
public boolean isFirstCharacterOfAWord() {
|
353
|
+
return this.isFirstCharacterOfAWord;
|
354
|
+
}
|
355
|
+
|
356
|
+
public boolean isCharacterAtTheBeginningOfNewLine() {
|
357
|
+
return this.isCharacterAtTheBeginningOfNewLine;
|
358
|
+
}
|
359
|
+
|
360
|
+
public boolean isCharacterCloseToPreviousWord() {
|
361
|
+
return this.isCharacterCloseToPreviousWord;
|
362
|
+
}
|
363
|
+
|
364
|
+
public String toString() {
|
365
|
+
String toString = "";
|
366
|
+
toString += index;
|
367
|
+
toString += " ";
|
368
|
+
toString += characterValue;
|
369
|
+
toString += " isCharacterPartOfPreviousWord=" + isCharacterPartOfPreviousWord;
|
370
|
+
toString += " isFirstCharacterOfAWord=" + isFirstCharacterOfAWord;
|
371
|
+
toString += " isCharacterAtTheBeginningOfNewLine=" + isCharacterAtTheBeginningOfNewLine;
|
372
|
+
toString += " isCharacterPartOfASentence=" + isCharacterCloseToPreviousWord;
|
373
|
+
toString += " isCharacterCloseToPreviousWord=" + isCharacterCloseToPreviousWord;
|
374
|
+
return toString;
|
375
|
+
}
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
class CharacterFactory {
|
381
|
+
|
382
|
+
private TextPosition previousTextPosition;
|
383
|
+
private boolean firstCharacterOfLineFound;
|
384
|
+
private boolean isCharacterPartOfPreviousWord;
|
385
|
+
private boolean isFirstCharacterOfAWord;
|
386
|
+
private boolean isCharacterAtTheBeginningOfNewLine;
|
387
|
+
private boolean isCharacterCloseToPreviousWord;
|
388
|
+
|
389
|
+
public CharacterFactory(boolean firstCharacterOfLineFound) {
|
390
|
+
this.firstCharacterOfLineFound = firstCharacterOfLineFound;
|
391
|
+
}
|
392
|
+
|
393
|
+
public Character createCharacterFromTextPosition(final TextPosition textPosition, final TextPosition previousTextPosition) {
|
394
|
+
this.setPreviousTextPosition(previousTextPosition);
|
395
|
+
this.isCharacterPartOfPreviousWord = this.isCharacterPartOfPreviousWord(textPosition);
|
396
|
+
this.isFirstCharacterOfAWord = this.isFirstCharacterOfAWord(textPosition);
|
397
|
+
this.isCharacterAtTheBeginningOfNewLine = this.isCharacterAtTheBeginningOfNewLine(textPosition);
|
398
|
+
this.isCharacterCloseToPreviousWord = this.isCharacterCloseToPreviousWord(textPosition);
|
399
|
+
char character = this.getCharacterFromTextPosition(textPosition);
|
400
|
+
int index = (int)textPosition.getX() / PDFLayoutTextStripper.OUTPUT_SPACE_CHARACTER_WIDTH_IN_PT;
|
401
|
+
return new Character(character,
|
402
|
+
index,
|
403
|
+
isCharacterPartOfPreviousWord,
|
404
|
+
isFirstCharacterOfAWord,
|
405
|
+
isCharacterAtTheBeginningOfNewLine,
|
406
|
+
isCharacterCloseToPreviousWord);
|
407
|
+
}
|
408
|
+
|
409
|
+
private boolean isCharacterAtTheBeginningOfNewLine(final TextPosition textPosition) {
|
410
|
+
if ( ! firstCharacterOfLineFound ) {
|
411
|
+
return true;
|
412
|
+
}
|
413
|
+
TextPosition previousTextPosition = this.getPreviousTextPosition();
|
414
|
+
float previousTextYPosition = previousTextPosition.getY();
|
415
|
+
return ( Math.round( textPosition.getY() ) < Math.round(previousTextYPosition) );
|
416
|
+
}
|
417
|
+
|
418
|
+
private boolean isFirstCharacterOfAWord(final TextPosition textPosition) {
|
419
|
+
if ( ! firstCharacterOfLineFound ) {
|
420
|
+
return true;
|
421
|
+
}
|
422
|
+
double numberOfSpaces = this.numberOfSpacesBetweenTwoCharacters(previousTextPosition, textPosition);
|
423
|
+
return (numberOfSpaces > 1) || this.isCharacterAtTheBeginningOfNewLine(textPosition);
|
424
|
+
}
|
425
|
+
|
426
|
+
private boolean isCharacterCloseToPreviousWord(final TextPosition textPosition) {
|
427
|
+
if ( ! firstCharacterOfLineFound ) {
|
428
|
+
return false;
|
429
|
+
}
|
430
|
+
double numberOfSpaces = this.numberOfSpacesBetweenTwoCharacters(previousTextPosition, textPosition);
|
431
|
+
int widthOfSpace = (int) Math.ceil(textPosition.getWidthOfSpace());
|
432
|
+
return (numberOfSpaces > 1 && numberOfSpaces <= widthOfSpace);
|
433
|
+
}
|
434
|
+
|
435
|
+
private boolean isCharacterPartOfPreviousWord(final TextPosition textPosition) {
|
436
|
+
TextPosition previousTextPosition = this.getPreviousTextPosition();
|
437
|
+
if ( previousTextPosition.getUnicode().equals(" ") ) {
|
438
|
+
return false;
|
439
|
+
}
|
440
|
+
double numberOfSpaces = this.numberOfSpacesBetweenTwoCharacters(previousTextPosition, textPosition);
|
441
|
+
return (numberOfSpaces <= 1);
|
442
|
+
}
|
443
|
+
|
444
|
+
private double numberOfSpacesBetweenTwoCharacters(final TextPosition textPosition1, final TextPosition textPosition2) {
|
445
|
+
double previousTextXPosition = textPosition1.getX();
|
446
|
+
double previousTextWidth = textPosition1.getWidth();
|
447
|
+
double previousTextEndXPosition = (previousTextXPosition + previousTextWidth);
|
448
|
+
double numberOfSpaces = Math.abs(Math.round(textPosition2.getX() - previousTextEndXPosition));
|
449
|
+
return numberOfSpaces;
|
450
|
+
}
|
451
|
+
|
452
|
+
|
453
|
+
|
454
|
+
private char getCharacterFromTextPosition(final TextPosition textPosition) {
|
455
|
+
String string = textPosition.getUnicode();
|
456
|
+
char character = string.charAt(0);
|
457
|
+
return character;
|
458
|
+
}
|
459
|
+
|
460
|
+
private TextPosition getPreviousTextPosition() {
|
461
|
+
return this.previousTextPosition;
|
462
|
+
}
|
463
|
+
|
464
|
+
private void setPreviousTextPosition(final TextPosition previousTextPosition) {
|
465
|
+
this.previousTextPosition = previousTextPosition;
|
466
|
+
}
|
467
|
+
|
468
|
+
}
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Pdf::Textstream
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pdf/textstream`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'pdf-textstream'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install pdf-textstream
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pdf-textstream.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pdf/textstream"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/build.sh
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "pdf/textstream/version"
|
2
|
+
require "java"
|
3
|
+
|
4
|
+
# load jars
|
5
|
+
require_relative "../../jars/pdfbox-2.0.6.jar"
|
6
|
+
require_relative "../../jars/commons-logging-1.2.jar"
|
7
|
+
require_relative "../../jars/fontbox-2.0.6.jar"
|
8
|
+
|
9
|
+
$CLASSPATH << "#{File.expand_path(File.dirname(__FILE__))}/../../classes"
|
10
|
+
module Pdf
|
11
|
+
module Textstream
|
12
|
+
PDFLayoutTextStripper = JavaUtilities.get_proxy_class("PDFLayoutTextStripper")
|
13
|
+
|
14
|
+
# change namespace
|
15
|
+
PDFParser = Java::OrgApachePdfboxPdfparser::PDFParser
|
16
|
+
RandomAccessFile = Java::OrgApachePdfboxIo::RandomAccessFile
|
17
|
+
PDDocument = Java::OrgApachePdfboxPdmodel::PDDocument
|
18
|
+
PDFTextStripper = Java::OrgApachePdfboxText::PDFTextStripper
|
19
|
+
|
20
|
+
def self.file_path_to_text(path)
|
21
|
+
# TODO: exception handling
|
22
|
+
pdfParser = PDFParser.new(RandomAccessFile.new(Java::JavaIo::File.new(path), "r"))
|
23
|
+
pdfParser.parse()
|
24
|
+
pdDocument = PDDocument.new(pdfParser.getDocument());
|
25
|
+
pdfTextStripper = PDFLayoutTextStripper.new
|
26
|
+
string = pdfTextStripper.getText(pdDocument);
|
27
|
+
return string
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "pdf/textstream/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pdf-textstream"
|
8
|
+
spec.version = Pdf::Textstream::VERSION
|
9
|
+
spec.authors = ["Michal Kulesza"]
|
10
|
+
spec.email = ["michal.kulesza@netguru.co"]
|
11
|
+
|
12
|
+
spec.summary = %q{JRuby gem converting PDF to text keeping the layout}
|
13
|
+
spec.homepage = "https://github.com/mic-kul/pdf-textstream"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdf-textstream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michal Kulesza
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.15'
|
19
|
+
name: bundler
|
20
|
+
prerelease: false
|
21
|
+
type: :development
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '10.0'
|
33
|
+
name: rake
|
34
|
+
prerelease: false
|
35
|
+
type: :development
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.0'
|
47
|
+
name: rspec
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- michal.kulesza@netguru.co
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- PDFLayoutTextStripper.java
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- build.sh
|
73
|
+
- classes/Character.class
|
74
|
+
- classes/CharacterFactory.class
|
75
|
+
- classes/PDFLayoutTextStripper.class
|
76
|
+
- classes/TextLine.class
|
77
|
+
- jars/commons-logging-1.2.jar
|
78
|
+
- jars/fontbox-2.0.6.jar
|
79
|
+
- jars/pdfbox-2.0.6.jar
|
80
|
+
- lib/pdf/textstream.rb
|
81
|
+
- lib/pdf/textstream/version.rb
|
82
|
+
- pdf-textstream.gemspec
|
83
|
+
homepage: https://github.com/mic-kul/pdf-textstream
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.6.11
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: JRuby gem converting PDF to text keeping the layout
|
107
|
+
test_files: []
|