@@ -23,43 +23,30 @@ public static void main(String[] args) {
2323 }
2424
2525 private static void generateTxtFromPDF (String filename ) throws IOException {
26- System .out .println ("Parsing text from PDF file " + filename );
27- String parsedText = null ;
28- PDFTextStripper pdfStripper ;
29- PDDocument pdDoc = null ;
30- COSDocument cosDoc = null ;
31-
32- File f = new File (filename );
33- PDFParser parser = new PDFParser (new RandomAccessFile (f , "r" ));
34-
3526 try {
27+ File f = new File (filename );
28+ String parsedText ;
29+ PDFParser parser = new PDFParser (new RandomAccessFile (f , "r" ));
3630 parser .parse ();
37- cosDoc = parser .getDocument ();
38- pdfStripper = new PDFTextStripper ();
39- pdDoc = new PDDocument (cosDoc );
31+
32+ COSDocument cosDoc = parser .getDocument ();
33+
34+ PDFTextStripper pdfStripper = new PDFTextStripper ();
35+ PDDocument pdDoc = new PDDocument (cosDoc );
36+
4037 parsedText = pdfStripper .getText (pdDoc );
41- } catch (Exception e ) {
42- System .err .println ("An exception occured in parsing the PDF Document." );
43- e .printStackTrace ();
44- try {
45- if (cosDoc != null )
46- cosDoc .close ();
47- if (pdDoc != null )
48- pdDoc .close ();
49- } catch (Exception e1 ) {
50- e .printStackTrace ();
51- }
52- }
53- System .out .println ("Writing PDF text to output text file" );
54- try {
38+
39+ if (cosDoc != null )
40+ cosDoc .close ();
41+ if (pdDoc != null )
42+ pdDoc .close ();
43+
5544 PrintWriter pw = new PrintWriter ("src/output/pdf.txt" );
5645 pw .print (parsedText );
5746 pw .close ();
5847 } catch (Exception e ) {
59- System .out .println ("An exception occured in writing the pdf text to file." );
6048 e .printStackTrace ();
6149 }
62- System .out .println ("Done." );
6350 }
6451
6552}
0 commit comments