我想從存在於SD卡中的PDF文件中讀取文本。如何從存儲在SD卡中的PDF文件中獲取文本?Android-從PDF獲取文本
我想這樣的:
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!");
}
}
}
注:它的工作很好,如果該文件是文本文件(test.txt的),但不工作的PDF(檢驗.pdf)
但這裏的文字沒有像現在這樣從PDF中獲得,它變得像字節碼。我怎樣才能做到這一點?
在此先感謝。
PDF文件格式是不是純文本。您需要一個解析器庫,如[PDFBox](https://pdfbox.apache.org/)來從文件中提取文本。 – TactMayers
我可以在android平臺上使用PDF格式嗎? –