当我试图从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(我希望它可以帮助):
[2013 -11-23 03:03:29 - AndroidTest] Android启动! [2013-11-23 03:03:29 - AndroidTest] adb正常运行。 [2013-11-23 03:03:29 - AndroidTest]执行com.example.androidtest.MainActivity>活动启动 [2013-11-23 03:03:29 - AndroidTest]自动目标模式:启动新模拟器>兼容AVD'Tab' [2013-11-23 03:03:29 - AndroidTest]使用虚拟设备“选项卡”启动新仿真器 [2013-11-23 03:03:29 - AndroidTest]发现新仿真器:仿真器-5554 [2013-11-23 03:03:29 - AndroidTest]等待HOME('android.process.acore')被启动... [2013-11-23 03:03:57 - AndroidTest ]首页上的设备'模拟器-5554' [2013-11-23 03:03:57 - AndroidTest]上传AndroidTest.apk到设备'模拟器-5554' [2013-11-23 03:04:06 - AndroidTest]安装AndroidTest.apk ... [2013-11-23 03:04: 29 - AndroidTest]成功! [2013-11-23 03:04:29 - AndroidTest]开始活动>设备仿真器-5554上的com.example.androidtest.MainActivity [2013-11-23 03:04:30 - AndroidTest] ActivityManager:开始:意图> {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER]> cmp = com.example.androidtest/.MainActivity}
感谢您的时间!