Reputation: 21551
I want to read text from a PDF file present in SD card.How can we get text from a PDF file which is stored in sd card?
I tried like:
public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private String line = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(getApplicationContext(), this);
final TextView text1 = (TextView) findViewById(R.id.textView1);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
private String[] arr;
@Override
public void onClick(View v) {
File sdcard = Environment.getExternalStorageDirectory();
// Get the text file
File file = new File(sdcard, "test.pdf");
// ob.pathh
// Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
// int i=0;
List<String> lines = new ArrayList<String>();
while ((line = br.readLine()) != null) {
lines.add(line);
// arr[i]=line;
// i++;
text.append(line);
text.append('\n');
}
for (String string : lines) {
tts.speak(string, TextToSpeech.SUCCESS, null);
}
arr = lines.toArray(new String[lines.size()]);
System.out.println(arr.length);
text1.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
// speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
}
Note: It's working fine if the file is text file (test.txt) but not working for pdf (test.pdf)
But here the text is not getting from PDF as it is, it's getting like byte code. How can I achieve this?
Thanks in advance.
Upvotes: 11
Views: 17216
Reputation: 950
many developer this error show
java.io.IOException: /storage/emulated/0/Download/sample.pdf not found as file or resource
here problem is external file not access i have one solution find it.
Add Permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Add runtime permission handling (for API 23+):
private val PERMISSION_REQUEST_CODE = 1
private fun checkAndRequestPermissions(): Boolean {
val readPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
val writePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
val listPermissionsNeeded = ArrayList<String>()
if (readPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE)
}
if (writePermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
if (listPermissionsNeeded.isNotEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toTypedArray(), PERMISSION_REQUEST_CODE)
return false
}
return true
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
PERMISSION_REQUEST_CODE -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
openPdfPicker()
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
}
add this library in gradle file(groovy or kotlin dsl):
implementation("com.itextpdf:itextg:5.5.10")
PDF Picker and Text Extraction:
private val pickPdfLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val selectedFileUri = result.data?.data
selectedFileUri?.let { uri ->
val fileName = "pdf_sample.pdf"
copyFileToAppDirectory(this, uri, fileName)
try {
var voiceText = ""
val reader = PdfReader(File(filesDir, fileName).absolutePath)
val n = reader.numberOfPages
for (i in 1..n) {
voiceText += PdfTextExtractor.getTextFromPage(reader, i).trim() + "\n"
}
Log.d("parsedText", voiceText)
reader.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
fun copyFileToAppDirectory(context: Context, pdfUri: Uri, destinationFileName: String) {
val inputStream = context.contentResolver.openInputStream(pdfUri)
val outputFile = File(context.filesDir, destinationFileName)
val outputStream = FileOutputStream(outputFile)
inputStream?.copyTo(outputStream)
}
Button Click Listener:
button.setOnClickListener {
if (checkAndRequestPermissions()) {
openPdfPicker()
}
}
private fun openPdfPicker() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/pdf"
}
pickPdfLauncher.launch(intent)
}
Upvotes: 1
Reputation: 1107
I have got the solution with iText.
Gradle,
compile 'com.itextpdf:itextg:5.5.10'
Java,
try {
String parsedText="";
PdfReader reader = new PdfReader(yourPdfPath);
int n = reader.getNumberOfPages();
for (int i = 0; i <n ; i++) {
parsedText = parsedText+PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages
}
System.out.println(parsedText);
reader.close();
} catch (Exception e) {
System.out.println(e);
}
Upvotes: 23
Reputation: 95
PDF format is not your normal text file.. You need to do a little more research on PDFs this is the best answer you'll get How to read pdf in my android application?
Upvotes: 2