首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用iTextG从Android上的pdf文件中提取文本

使用iTextG从Android上的pdf文件中提取文本
EN

Stack Overflow用户
提问于 2013-11-23 03:27:14
回答 1查看 392关注 0票数 1

当我试图从sdcard中读取一个pdf文件并从中提取文本时,什么都没有发生。没有错误,没有警告,通知,也没有结果文件。我将源文件和结果存储在设备sdcard的根文件夹中。你们能帮我解决这个问题吗?这是我的代码:

代码语言:javascript
复制
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}

耽误您时间,实在对不起!

EN

回答 1

Stack Overflow用户

发布于 2015-10-04 02:47:29

您正在使用一个过滤器来限制要从以下位置提取文本的区域:

代码语言:javascript
复制
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)矩形之外的页面。这意味着过滤器不与页面相交,因此不提取文本。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20158142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档