I am creating an app where I can extract the text from the pdf. For this I am using PdfBox library. But when I import pdf from the file manager, app stops and it gives exception in Initialize error at line where PDFTextStripper is initialized. How can I solve this problem?
Below is the function for extracting the text from pdf.
private fun extractTextPdfFile() {
var document: PDDocument? = null
var parsedText: String? = null
try {
val inputStream: InputStream? = this.contentResolver.openInputStream(fileUri!!)
document = PDDocument.load(inputStream)
} catch (e: IOException) {
e.printStackTrace()
}
try {
val pdfStripper = PDFTextStripper()
pdfStripper.startPage = 0
if (document != null) {
pdfStripper.endPage = 1
}
parsedText="Parsed text: " pdfStripper.getText(document)
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
document?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
findViewById<TextView>(R.id.file_path).text=parsedText
}
}
I have used registerForActivityResult for getting the pdf document Uri.
private var fileUri: Uri? = null
private val getContent =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
// Handle the returned Uri
if (uri != null) {
fileUri = uri
} else {
Toast.makeText(
this,
"Cannot fetch document from the file manager",
Toast.LENGTH_SHORT
).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.Import).setOnClickListener {
getContent.launch("application/pdf")
if (fileUri != null)
extractTextPdfFile()
}
}
This is the exception which I am getting. I don't know how it is now giving null pointer exception, earlier it was giving the exception which I had mentioned.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
at com.tom_roush.pdfbox.text.PDFTextStripper.<clinit>(PDFTextStripper.java:1874)
at com.example.doctranslator.MainActivity.extractTextPdfFile(MainActivity.kt:82)
at com.example.doctranslator.MainActivity.onCreate$lambda-1(MainActivity.kt:44)
at com.example.doctranslator.MainActivity.$r8$lambda$8z4EEPr6Q6JatIakz-DO65v-zdE(Unknown Source:0)
at com.example.doctranslator.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8595)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
CodePudding user response:
Are you sure the PdfBox library is getting compiled into your code? Because if its an error which happens on initialization it seems to be a compilation error since it cannot refer to the library.
You should get some kotlin code to compile your dependencies into your built app.
CodePudding user response:
It sounds like you haven't called PDFBoxResourceLoader.init(Context)
before using PDFTextStripper. This is required to load certain files used by PdfBox-Android like BidiMirroring.txt.
I added PDFBoxResourceLoader.init(applicationContext)
to your onCreate
function and was able to extract text successfully using your code.