In the last tutorial, we had discuss how to choose any file from mobile using the android built-in file chooser. if you didn't watch or read the previous tutorial let's watch it before because this tutorial is associated with it.
so let's get started.
in this tutorial, we will learn how to extract text from pdf files in android studio using java code. we will extract the text or content from a file using the iText PDF library. it's very easy to extract the text using this library and this is open source and free to use. so let's start in steps.
- Step 1
implementation 'com.itextpdf:itextg:5.5.10'
- step 2
sync the project
- step 3
Add an Edittext or text view to your activity where you want to display the extracted text or content.
- step 4
let's do a code of extracting text from the text.
public void extractPdfFile(final Uri uri) throws IOException {
try {
inputStream = MainActivity.this.getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
new Thread(() -> {
String fileContent = "";
StringBuilder builder = new StringBuilder();
PdfReader reader = null;
try {
reader = new PdfReader(inputStream);
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {
try {
fileContent = PdfTextExtractor.getTextFromPage(reader, i);
} catch (IOException e) {
e.printStackTrace();
}
builder.append(fileContent);
}
reader.close();
runOnUiThread(() -> {
exractedText_et.setText(builder.toString());
});
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
it may be possible your file size is too much so it's good for you to extract text from a pdf file using another thread
happy coding

0 Comments
If you have any doubt please let me you can also contact me for developing apps