当我试图从sdcard中读取一个pdf文件并从中提取文本时,什么都没有发生。没有错误,没有警告,通知,也没有结果文件。我将源文件和结果存储在设备sdcard的根文件夹中。你们能帮我解决这个问题吗?这是我的代码:
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安卓系统发布!2013-11-23 03:29- AndroidTest亚行正常运行。2013-11-23 03:03:29 - AndroidTest Performing com.example.androidtest.MainActivity >活动启动2013-11-23 03:03:29 - AndroidTest自动目标模式:启动新的仿真器与launch 11-23 'Tab‘2013-23 03:03:29 - AndroidTest启动一个新的仿真器与虚拟设备'Tab’2013-11-23 03:03:29 -发现:模拟器-5554 2013-11-23 03: 03:03:29 - AndroidTest等待回家('android.process.acore') launch=‘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启动活动>com.example.androidtest.MainActivity -5554 2013年-11-23 03:04:04:30- AndroidTest ActivityManager: Starting >{ act=android.intent.action.MAIN cat=android.intent.category.LAUNCHER >cmp=com.example.androidtest/..MainActivity}
耽误您时间,实在对不起!
发布于 2015-10-04 02:47:29
您正在使用一个过滤器来限制要从以下位置提取文本的区域:
Rectangle rect = new Rectangle(0, 0, 600, 900);
// Define the rectangle to extract text within it
RenderFilter filter = new RegionTextRenderFilter(rect);PDF页面不需要在(0, 0)的左下角。它可以在坐标系中的任何地方。因此,A4页面可以是(0, 0, 595, 842),但也可以是(1000, 2000, 1595, 2842)。
您要从其中提取文本的PDF格式可能包含在用于筛选器的(0, 0, 600, 900)矩形之外的页面。这意味着过滤器不与页面相交,因此不提取文本。
https://stackoverflow.com/questions/20158142
复制相似问题