2015-04-21 76 views
5

我想從存在於SD卡中的PDF文件中讀取文本。如何從存儲在SD卡中的PDF文件中獲取文本?Android-從PDF獲取文本

我想這樣的:

public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener { 

    private TextToSpeech tts; 
    private String line = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tts = new TextToSpeech(getApplicationContext(), this); 

     final TextView text1 = (TextView) findViewById(R.id.textView1); 

     findViewById(R.id.button1).setOnClickListener(new OnClickListener() { 

      private String[] arr; 

      @Override 
      public void onClick(View v) { 
       File sdcard = Environment.getExternalStorageDirectory(); 

       // Get the text file 

       File file = new File(sdcard, "test.pdf"); 

       // ob.pathh 
       // Read text from file 

       StringBuilder text = new StringBuilder(); 
       try { 
        BufferedReader br = new BufferedReader(new       FileReader(file)); 

        // int i=0; 
        List<String> lines = new ArrayList<String>(); 

        while ((line = br.readLine()) != null) { 
         lines.add(line); 
         // arr[i]=line; 
         // i++; 
         text.append(line); 
         text.append('\n'); 
        } 
        for (String string : lines) { 
         tts.speak(string, TextToSpeech.SUCCESS, null); 
        } 
        arr = lines.toArray(new String[lines.size()]); 
        System.out.println(arr.length); 
        text1.setText(text); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 

    } 

    @Override 
    public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 
      int result = tts.setLanguage(Locale.US); 
      if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e("TTS", "This Language is not supported"); 
      } else { 
       // speakOut(); 
      } 

     } else { 
      Log.e("TTS", "Initilization Failed!"); 
     } 
    } 

} 

注:它的工作很好,如果該文件是文本文件(test.txt的),但不工作的PDF(檢驗.pdf)

但這裏的文字沒有像現在這樣從PDF中獲得,它變得像字節碼。我怎樣才能做到這一點?

在此先感謝。

+2

PDF文件格式是不是純文本。您需要一個解析器庫,如[PDFBox](https://pdfbox.apache.org/)來從文件中提取文本。 – TactMayers

+0

我可以在android平臺上使用PDF格式嗎? –

回答

5

我有與iText的解決方案的最佳答案。

搖籃,

compile 'com.itextpdf:itextg:5.5.10' 

的Java,

try { 
      String parsedText=""; 
      PdfReader reader = new PdfReader(yourPdfPath); 
      int n = reader.getNumberOfPages(); 
      for (int i = 0; i <n ; i++) { 
       parsedText = parsedText+PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages 
      } 
      System.out.println(parsedText); 
      reader.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
+0

這裏的路徑格式是什麼? 我已經使用 '/ storage/emulated/0/Download/abc.pdf' 但它給出了錯誤 –

+0

你正在得到什麼錯誤?您是否添加了存儲權限? – REMITH

+0

是的我得到了解決方案,有權限錯誤 –