當我試圖從SD卡中讀取pdf文件並從中提取文本時,什麼也沒有發生。 沒有錯誤,沒有警告,通知,也沒有結果文件。 我將源文件和結果都存儲在設備的SD卡的根文件夾中。 你們能幫我解決這個問題嗎? 這裏是我的代碼:使用iTextG從Android上的pdf文件中提取文本
package com.example.androidtest;
import java.io.File;
...
public class MainActivity extends Activity {
private Button button;
public static final String TIMETABLE = "doc.pdf"; // The original PDF that will be parsed.
public static final String RESULT = "timetable.txt"; // The text file received after scan.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
processSource();
}
public void processSource() {
button = (Button) this.findViewById(R.id.button_add);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new MainActivity().extractText(TIMETABLE, RESULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void extractText(String pdf, String doc) throws IOException {
File sdcard = Environment.getExternalStorageDirectory(); // Load file timetable.txt from device's sdcard
File file = new File(sdcard, pdf);
File text = new File(sdcard, doc); // Save the result file in device's sdcard
InputStream is;
try {
is = new FileInputStream(file);
PdfReader reader = new PdfReader(is); // Call the source file
PrintWriter out = new PrintWriter(new FileOutputStream(text));
Rectangle rect = new Rectangle(0, 0, 600, 900); // Define the rectangle to extract text within it
RenderFilter filter = new RegionTextRenderFilter(rect);
TextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
out.println(PdfTextExtractor.getTextFromPage(reader, 1, strategy));
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Call the source file
}
}
這裏是它在控制檯選項卡中顯示,當我測試的AVD(我希望它可以幫助):
[ -11-23 03:03:29 - AndroidTest] Android啓動! [-11-23 03:03:29 - AndroidTest] adb正常運行。 [-11-23 03:03:29 - AndroidTest]執行com.example.androidtest.MainActivity>活動啓動 [-11-23 03:03:29 - AndroidTest]自動目標模式:啓動新模擬器>兼容AVD'Tab' [-11-23 03:03:29 - AndroidTest]使用虛擬設備「選項卡」啓動新仿真器 [-11-23 03:03:29 - AndroidTest]發現新仿真器:仿真器-5554 [-11-23 03:03:29 - AndroidTest]等待HOME('android.process.acore')被啓動... [-11-23 03:03:57 - AndroidTest ]首頁上的設備'模擬器-5554' [-11-23 03:03:57 - AndroidTest]上傳AndroidTest.apk到設備'模擬器-5554' [-11-23 03:04:06 - AndroidTest]安裝AndroidTest.apk ... [-11-23 03:04: 29 - AndroidTest]成功! [-11-23 03:04:29 - AndroidTest]開始活動>設備仿真器-5554上的com.example.androidtest.MainActivity [-11-23 03:04:30 - AndroidTest] ActivityManager:開始:意圖> {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER]> cmp = com.example.androidtest/.MainActivity}
感謝您的時間!