2013-11-23 85 views
1

當我試圖從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}

感謝您的時間!

回答

0

您使用的過濾器來限制的區域從文本中提取:

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)矩形之外的頁面。這意味着過濾器不會與頁面相交,因此不會提取文本。