-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
325 lines (308 loc) · 15.6 KB
/
Program.cs
File metadata and controls
325 lines (308 loc) · 15.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
namespace Code
{
using System;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
//using IronOcr.AutoOcr;
class Program
{
static void Main(string[] args)
{
string[] files = ReadFileLocations();
using (System.IO.StreamWriter outFile = new System.IO.StreamWriter(@"C:\Users\cgomez\source\repos\Code\Code\output-text-1.txt"))
{
foreach (string file in files)
{
string newFile = file.Replace("\"", "").Trim();
//Console.WriteLine(newFile);
outFile.WriteLine(ExtractTextFromPdf_KeyWord1(newFile)); //sends found words to output-text-1
}
}
using (System.IO.StreamWriter outFile = new System.IO.StreamWriter(@"C:\Users\cgomez\source\repos\Code\Code\output-text-2.txt"))
{
foreach (string file in files)
{
string newFile = file.Replace("\"", "").Trim();
//Console.WriteLine(newFile);
outFile.WriteLine(ExtractTextFromPdf_KeyWord2(newFile)); //sends found words to output-text-2
}
}
using (System.IO.StreamWriter outFile = new System.IO.StreamWriter(@"C:\Users\cgomez\source\repos\Code\Code\output-text-3.txt"))
{
foreach (string file in files)
{
string newFile = file.Replace("\"", "").Trim();
//Console.WriteLine(newFile);
outFile.WriteLine(ExtractTextFromPdf_KeyWord3(newFile)); //sends found words to output-text-3
}
}
//PrintDoc();
}
public static string[] ReadFileLocations() //Reads the PathLocations text file.
{
string fileLocations = "C:/Users/cgomez/source/repos/Code/Code/Path-Locations.txt";
string[] files = File.ReadAllLines(fileLocations);
return files;
}
public static string ExtractTextFromPdf_KeyWord1(string path) //Reads every line from the pdf and grabs key information.
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
string thePage = PdfTextExtractor.GetTextFromPage(reader, i, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
string[] theLines = thePage.Split('\n');
foreach (var theLine in theLines)
{
//text.AppendLine(theLine);
//if (theLine.Contains("Alternate Keyword") && theLine.Length > 13) //&& !theLine.Contains("Some word"))
if (theLine.Contains("Keyword"))
{
string[] splitLine = theLine.Split(" ");
for (int j = Array.IndexOf(splitLine, "Keyword") + 1; j < splitLine.Length; j++)
{
text.Append(splitLine[j] + " ");
}
//if (text.ToString().Contains("Alternate word")) //All if statements below are optional to clean up outputs.
if (text.ToString().Contains("word"))
{
text.Remove(0, 9);
//text.Remove(0, 18);
}
if (text.ToString().Contains("word"))
{
text.Remove(0, 9);
}
/*if (text.ToString().Contains("word"))
{
text.Remove(text.ToString().IndexOf('w'), (text.ToString().Length - (text.ToString().IndexOf('w'))));
}
if (text.ToString().Contains("wordOne"))
{
text.Remove(0, 8);
}
if (text.ToString().Contains("wordTwo"))
{
text.Remove(0, 9);
}
if (text.ToString().Contains("wordThree"))
{
text.Remove(text.ToString().IndexOf('W'), (text.ToString().Length - (text.ToString().IndexOf('W'))));
}
if (text.ToString().Contains("wordFour") && text.ToString().Length - text.ToString().Replace("WordFour", "").Length > 4) // makes sure the same word appears twice
{
text.Remove(text.ToString().LastIndexOf('w'), (text.ToString().Length - (text.ToString().LastIndexOf('w'))));
}
if (text.ToString().Contains("•"))
{
text.Replace("•", "");
}*/
goto here;
}
}
}
here:
//file.WriteLine(text);
Console.WriteLine(text);
return text.ToString();
}
}
public static string ExtractTextFromPdf_KeyWord2(string path) //Reads every line from the pdf and grabs key information.
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
string thePage = PdfTextExtractor.GetTextFromPage(reader, i, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
string[] theLines = thePage.Split('\n');
//int lineNum = 0;
foreach (var theLine in theLines)
{
if (theLine.Contains("Keyword2"))
//if (theLine.Contains("AltKeyword"))
{
string[] splitLine = theLine.Split(" ");
for (int j = Array.IndexOf(splitLine, "Keyword2") + 1; j < splitLine.Length; j++)
//for (int j = Array.IndexOf(splitLine, "AltKeyword"); j < Array.IndexOf(splitLine, "AltKeywordEnd"); j++)
{
text.Append(splitLine[j] + " ");
if (text.ToString().Contains("AnotherWord"))
{
text.Remove(0, 17);
}
}
goto here;
}
}
}
here:
Console.WriteLine(text);
return text.ToString();
}
}
public static string ExtractTextFromPdf_KeyWord3(string path) //Reads every line from the pdf and grabs key information.
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
string thePage = PdfTextExtractor.GetTextFromPage(reader, i, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
string[] theLines = thePage.Split('\n');
//int lineNum = 0;
foreach (var theLine in theLines)
{
//text.AppendLine(theLine);
if (theLine.Contains("KeyWord3"))
//if (theLine.Contains("AltWord"))
{
string[] splitLine = theLine.Split(" ");
for (int j = Array.IndexOf(splitLine, ("KeyWord3")) + 1; j < splitLine.Length; j++)
{
text.Append(splitLine[j] + " ");
text.ToString();
}
if (text.ToString().Contains("RedundantWord1"))//If statements below are to cleanup the outputs.
{
text.Remove(text.ToString().IndexOf('R'), (text.ToString().Length - (text.ToString().IndexOf('R'))));
}
if (text.ToString().Contains("RedundantWord2"))
{
text.Remove(0, 11);
}
if (text.ToString().Contains("RedundantWord3"))
{
text.Remove(text.ToString().IndexOf('R'), (text.ToString().Length - (text.ToString().IndexOf('R'))));
//Console.WriteLine(text.ToString().IndexOf('P') + " " + (text.ToString().Length - (text.ToString().IndexOf('P'))));
}
if (text.ToString().Contains("("))
{
text.Remove(text.ToString().IndexOf('('), (text.ToString().Length - (text.ToString().IndexOf('('))));
}
if (text.ToString().Contains("-"))
{
text.Replace("-", "");
}
if (text.ToString().Contains("—"))
{
text.Replace("—", "");
}
if (text.ToString().Contains("•"))
{
text.Replace("•", "");
}
goto here;
}
if (theLine.Contains("KeyWord3")) // Or this word
//if (theLine.Contains("AltWord"))
{
string[] splitLine = theLine.Split(" ");
for (int j = Array.IndexOf(splitLine, ("KeyWord3")) + 1; j < splitLine.Length; j++)
{
text.Append(splitLine[j] + " ");
text.ToString();
}
if (text.ToString().Contains("RedundantWord1"))//If statements below are to cleanup the outputs.
{
text.Remove(text.ToString().IndexOf('R'), (text.ToString().Length - (text.ToString().IndexOf('R'))));
}
if (text.ToString().Contains("RedundantWord2"))
{
text.Remove(0, 11);
}
if (text.ToString().Contains("RedundantWord3"))
{
text.Remove(text.ToString().IndexOf('R'), (text.ToString().Length - (text.ToString().IndexOf('R'))));
//Console.WriteLine(text.ToString().IndexOf('P') + " " + (text.ToString().Length - (text.ToString().IndexOf('P'))));
}
if (text.ToString().Contains("("))
{
text.Remove(text.ToString().IndexOf('('), (text.ToString().Length - (text.ToString().IndexOf('('))));
}
if (text.ToString().Contains("-"))
{
text.Replace("-", "");
}
if (text.ToString().Contains("—"))
{
text.Replace("—", "");
}
if (text.ToString().Contains("•"))
{
text.Replace("•", "");
}
goto here;
}
if (theLine.Contains("KeyWord3")) // Or this word
//if (theLine.Contains("AltWord"))
{
string[] splitLine = theLine.Split(" ");
for (int j = Array.IndexOf(splitLine, ("KeyWord3")) + 1; j < splitLine.Length; j++)
{
text.Append(splitLine[j] + " ");
text.ToString();
}
if (text.ToString().Contains("RedundantWord1"))//If statements below are to cleanup the outputs.
{
text.Remove(text.ToString().IndexOf('R'), (text.ToString().Length - (text.ToString().IndexOf('R'))));
}
if (text.ToString().Contains("RedundantWord2"))
{
text.Remove(0, 11);
}
if (text.ToString().Contains("RedundantWord3"))
{
text.Remove(text.ToString().IndexOf('R'), (text.ToString().Length - (text.ToString().IndexOf('R'))));
//Console.WriteLine(text.ToString().IndexOf('P') + " " + (text.ToString().Length - (text.ToString().IndexOf('P'))));
}
if (text.ToString().Contains("("))
{
text.Remove(text.ToString().IndexOf('('), (text.ToString().Length - (text.ToString().IndexOf('('))));
}
if (text.ToString().Contains("-"))
{
text.Replace("-", "");
}
if (text.ToString().Contains("—"))
{
text.Replace("—", "");
}
if (text.ToString().Contains("•"))
{
text.Replace("•", "");
}
goto here;
}
}
}
here:
//file.WriteLine(text);
Console.WriteLine(text);
return text.ToString();
}
}
public static string PrintDoc() //Prints out page so you can debug.
{
string thePage = "";
using (System.IO.StreamWriter outFile = new System.IO.StreamWriter(@"C:\Users\cgomez\source\repos\Code\Code\output-text-1.txt"))
{
using (PdfReader reader = new PdfReader("C:/Some-File"))
{
//StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
thePage = PdfTextExtractor.GetTextFromPage(reader, i, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
Console.WriteLine(thePage);
outFile.WriteLine(thePage);
}
}
}
return thePage;
}
}
}