-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfParser.scala
37 lines (30 loc) · 954 Bytes
/
PdfParser.scala
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
package pdf
import org.apache.pdfbox.ExtractText
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.util.PDFTextStripper
import scala.io.Source
import java.io.{FileReader, FileNotFoundException, IOException}
object PdfParser {
def apply(inputFilePath:String) = {
new PdfParser(inputFilePath)
}
}
class PdfParser(inputFilePath:String) {
private var pdfContent:PDDocument = null
def getText():String = {
var retVal = ""
try {
pdfContent = PDDocument.load(inputFilePath)
val pdfStripper = new PDFTextStripper
retVal = pdfStripper.getText(pdfContent)
}
catch {
case ex: FileNotFoundException => retVal = ex.toString()
case ex: IOException => retVal = ex.toString()
}
finally {
pdfContent.close()
}
retVal
}
}