Responsive Advertisement

HOW TO EXTRACT TEXT FROM PDF FILE IN ANDROID STUDIO USING JAVA


Follow these steps to extract or get or copy text from your pdf file using android studio with java or kotlin

Step 1 

Create New Project

Step2

Named the project as you want

Step 3

Open module level file check down image below





Step 4

Copy this dependency and paste in file
implementation 'com.itextpdf:itext-pdfa:5.5.5'

Step 5

Choose file from your mobile and send path or uri to this method

public void callExtractPdfFile(String path, String isPathOrUri) {
mLoading_pb.setVisibility(View.VISIBLE);

try {
mInputStream = this.getContentResolver().openInputStream(Uri.parse(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d(TAG, "extractPdfFile: " + e.getMessage());
}

new Thread(() -> {
try {
String fileContent = "";
StringBuilder builder = new StringBuilder();
PdfReader reader;
if (isPathOrUri.equals("uri"))
reader = new PdfReader(mInputStream);
else reader = new PdfReader(path);
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {
fileContent = PdfTextExtractor.getTextFromPage(reader, i);
builder.append(fileContent);
}
reader.close();
this.runOnUiThread(() -> {
mContent_et.setText(builder);
mLoading_pb.setVisibility(View.INVISIBLE);
});
} catch (Exception e) {
Log.d(TAG, "extractPdfFile: " + e.getMessage());
}
}).start();
}


thats it code is finished and enjoy the code

Post a Comment

0 Comments