【问题标题】:Extract text from a pdf file on Android using iTextG使用 iTextG 从 Android 上的 pdf 文件中提取文本
【发布时间】:2013-11-23 03:27:14
【问题描述】:

当我尝试从 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] Android 发布! [2013-11-23 03:03:29 - AndroidTest] adb 运行正常。 [2013-11-23 03:03:29 - AndroidTest] 执行 com.example.androidtest.MainActivity >activity 启动 [2013-11-23 03:03:29 - AndroidTest] 自动目标模式:使用 > 兼容的 AVD 'Tab' 启动新模拟器 [2013-11-23 03:03:29 - AndroidTest] 使用虚拟设备“选项卡”启动新模拟器 [2013-11-23 03:03:29 - AndroidTest] 发现新模拟器:emulator-5554 [2013-11-23 03:03:29 - AndroidTest] 等待 HOME ('android.process.acore') >launched... [2013-11-23 03:03:57 - AndroidTest] HOME 在设备 'emulator-5554' 上启动 [2013-11-23 03:03:57 - AndroidTest] 将 AndroidTest.apk 上传到设备 'emulator-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: 开始: Intent >{ act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] >cmp=com.example.androidtest/ .MainActivity }

感谢您的宝贵时间!

【问题讨论】:

    标签: android pdf itext


    【解决方案1】:

    您正在使用过滤器来限制从中提取文本的区域:

    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) 矩形之外。这意味着过滤器不会与页面相交,因此不会提取任何文本。

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 2014-09-11
      • 2013-06-29
      相关资源
      最近更新 更多