Android-获得从PDF文本 [英] Android- Get text from PDF

查看:203
本文介绍了Android-获得从PDF文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读从SD card.How PDF文件present文字,我们可以从存储在SD卡中的PDF文件获取文本?

I want to read text from a PDF file present in SD card.How can we get text from a PDF file which is stored in sd card?

我试过这样的:

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(的test.pdf)

Note: It's working fine if the file is text file (test.txt) but not working for pdf (test.pdf)

但这里的文本不会从PDF得到,因为它是,它越来越像字节code。我怎样才能做到这一点?

But here the text is not getting from PDF as it is, it's getting like byte code. How can I achieve this?

先谢谢了。

推荐答案

PDF格式的是不是你普通的文本文件。你需要做一些更多的研究PDF文件,这是你会得到最好的答案
如何在我的Andr​​oid应用程序读取PDF?

PDF format is not your normal text file.. You need to do a little more research on PDFs this is the best answer you'll get How to read pdf in my android application?

这篇关于Android-获得从PDF文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆