-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain2.java
More file actions
164 lines (130 loc) · 6.58 KB
/
Main2.java
File metadata and controls
164 lines (130 loc) · 6.58 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
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.util.TextPosition;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main2 extends PDFTextStripper {
static List<String> lines = new ArrayList<String>();
public Main2() throws IOException {
}
public static void main(String[] args) throws IOException {
List<Invoice> invoices = new ArrayList<>();
String branch = "1000";
String fileName = "file_path";
try (PDDocument document = PDDocument.load(new File(fileName))) {
PDFTextStripper stripper = new Main2();
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(document.getNumberOfPages());
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
// Loop through the lines
for ( int i = 0; i < lines.size(); i++ ) {
Invoice invoice = new Invoice();
if ( lines.get(i).startsWith("2001-") ) {
invoice.setInvoice_number( lines.get(i) );
invoice.setType( lines.get( i + 1 ) );
invoice.setAccount( lines.get( i + 2 ) );
invoice.setName( lines.get( i + 3 ) );
invoice.setJob_num( lines.get( i + 4) );
invoice.setCashier_user( lines.get( i + 5 ) );
invoice.setSis_mat_br( branch );
invoice.setDate( lines.get( i + 6 ) );
invoice.setTotal( lines.get( i + 7 ) );
invoice.setCost( lines.get( i + 8 ) );
invoice.setGm_perc( "0" );
// Increment i to the products
i = i + 18;
if ( ! lines.get( i - 1 ).contains("GM%") ) {
for ( int k = i; k < lines.size(); k++ ) {
if ( lines.get(k).contains("GM%") ) {
i = k + 1;
break;
}
}
}
List<String[]> items = new ArrayList<>();
for ( int j = i; j < lines.size() - 20; ) {
String[] item = {
lines.get(j), // Item Number
lines.get( j + 1 ), // Description
lines.get( j + 2 ), // Quantity
lines.get( j + 3 ), // U/M
lines.get( j + 4 ), // Unit Price
lines.get( j + 5 ), // U/M
lines.get( j + 6 ), // Unit Cost
lines.get( j + 7 ) // GM%
};
// Go to next line item
// If it's another invoice, repeat the whole process and create a new invoice
if ( lines.get( j + 8 ).trim().startsWith("2001-") ) {
i = j + 8 - 1;
items.add( item );
break;
}
if ( lines.get( j + 9 ).trim().startsWith("2001-") ) {
i = j + 9 - 1;
item[1] = item[1] + lines.get( j + 8 );
items.add( item );
break;
}
if ( lines.get( j + 9 ).trim().contains("Branch") ) {
j = j + 30;
if ( lines.get( j + 1 ).trim().startsWith("2001-") ) {
items.add( item );
i = j;
break;
}
if ( lines.get( j + 2 ).trim().startsWith("2001-") ) {
items.add( item );
i = j + 1;
break;
}
if ( lines.get( j + 3 ).trim().startsWith("2001-") ) {
items.add( item );
i = j + 2;
break;
}
}
// If it doesn't start with 2001- and 2 more lines down we have the qty, that means that
// this is the next part number in the list
if ( lines.get( j + 10 ).trim().endsWith(".0000") ) {
j = j + 8;
items.add( item );
continue;
}
// If we made it this far, that means that we don't have a quantity two lines down, and
// the description was split into 2 and the other part of the description is on line 8.
item[1] = item[1] + lines.get( j + 8 );
// One more check. We could have it that the description is split into three lines.
// So we first check if the quantity is 3 lines down. If it is, great: it was only split into two
// and we can restart the process
if ( lines.get( j + 11 ).trim().endsWith(".0000") ) {
j = j + 9;
items.add( item );
continue;
}
// Otherwise, line 9 also has part of the description. So we need to add it
item[1] = item[1] + lines.get( j + 9 );
// For sure this is the next line number
j = j + 10;
items.add( item );
}
invoice.setItems( items );
}
// Add invoice to invoices List
if ( invoice.getAccount() != null ) {
invoices.add( invoice );
}
}
}
for ( Invoice invoice : invoices ) {
System.out.println( invoice );
}
}
@Override
protected void writeString(String str, List<TextPosition> textPositions) throws IOException {
lines.add(str);
}
}