Tôi muốn đọc văn bản từ tệp PDF có trong thẻ SD. Làm cách nào để nhận văn bản từ tệp PDF được lưu trữ trong thẻ sd?Android- Nhận văn bản từ PDF
tôi đã cố gắng như:
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!");
}
}
}
Lưu ý: Đó là làm việc tốt nếu các tập tin là tập tin văn bản (test.txt) nhưng không làm việc cho pdf (test.pdf)
Nhưng đây là văn bản không nhận được từ PDF như nó, nó nhận được giống như mã byte. Làm thế nào tôi có thể đạt được điều này?
Xin cảm ơn trước.
định dạng tập tin PDF không phải là văn bản đơn giản. Bạn sẽ cần một thư viện phân tích cú pháp như [PDFBox] (https://pdfbox.apache.org/) để trích xuất văn bản từ tệp. – TactMayers
Tôi có thể sử dụng hộp PDF trong nền tảng Android không ?? –